Back to Roadmap
5:00

PHP Syntax, Variables & Data Types

Learn the fundamentals of PHP syntax, how variables work, and the different data types used in PHP programming

5 MIN READ VERIFIED CURRICULUM

PHP syntax provides the structure for writing PHP programs, while variables and data types help developers store and manipulate data efficiently.

Understanding these concepts is essential for building dynamic web applications using PHP.

Understanding PHP Syntax

PHP code is written inside special PHP tags that start with <?php and end with ?>.

The PHP engine processes the code on the server and returns the generated HTML output to the browser.

Basic PHP Program

The echo statement is commonly used to display output in PHP applications.

<?php
 echo 'Hello World';
?>
php

PHP Statements

Each PHP statement usually ends with a semicolon, which indicates the end of an instruction.

<?php
 $name = 'John';
 echo $name;
?>
php

Using Comments in PHP

Comments are used to explain code and improve readability for developers.

<?php
 // Single-line comment
 # Another single-line comment
 /* Multi-line comment */
?>
php

What are Variables?

Variables are containers used to store data values such as text, numbers, and arrays.

In PHP, every variable starts with the dollar sign symbol followed by the variable name.

Declaring Variables

Variables are automatically created when values are assigned to them.

<?php
 $username = 'Rahul';
 $age = 25;
?>
php

Rules for Variable Names

Variable names must start with a letter or underscore and cannot begin with a number.

PHP variable names are case-sensitive, so $name and $Name are treated as different variables.

Displaying Variables

Variables can be displayed using echo or print statements.

<?php
 $city = 'Delhi';
 echo $city;
?>
php

Introduction to Data Types

Data types define the type of data that a variable can store.

PHP supports multiple built-in data types for handling different kinds of information.

String Data Type

Strings are sequences of characters enclosed in single or double quotes.

<?php
 $message = 'Welcome to PHP';
 echo $message;
?>
php

Integer Data Type

Integers are whole numbers without decimal values.

<?php
 $marks = 95;
 echo $marks;
?>
php

Float Data Type

Float values are numbers that contain decimal points.

<?php
 $price = 199.99;
 echo $price;
?>
php

Boolean Data Type

Boolean values represent either true or false.

<?php
 $isLoggedIn = true;
?>
php

Array Data Type

Arrays are used to store multiple values in a single variable.

<?php
 $colors = ['Red', 'Blue', 'Green'];
 print_r($colors);
?>
php

Dynamic Typing in PHP

PHP is a dynamically typed language, meaning variables do not require explicit data type declarations.

PHP automatically determines the variable type based on the assigned value.

Checking Data Types

Functions like var_dump() and gettype() are used to inspect variable data types.

<?php
 $number = 100;
 var_dump($number);
?>
php

Variable Scope

Variable scope defines where a variable can be accessed within a PHP program.

PHP supports local, global, and static variable scopes.

Best Practices

Use meaningful variable names and proper formatting to make code easier to read and maintain.

Always follow PHP syntax rules carefully to avoid common programming errors.

Common Beginner Mistakes

Missing semicolons, incorrect variable naming, and mismatched quotes are common syntax mistakes in PHP.

Understanding syntax fundamentals helps developers debug applications more efficiently.

Summary

PHP syntax defines how PHP programs are written, while variables and data types allow developers to store and manage information effectively.

Mastering these concepts builds a strong foundation for backend development and advanced PHP programming.