PHP Syntax
Learning the rules and structure of PHP code
📝 PHP Syntax Basics
PHP syntax defines how you write code. PHP scripts start with <?php and end with ?>. Statements end with semicolons. PHP is case-sensitive for variables but not for keywords.
<?php
// Basic PHP syntax
echo "Hello World!";
?>
Output:
Hello World!
PHP Syntax Rules
PHP Tags
PHP code must be enclosed in <?php and ?> tags. These tell the server where PHP code begins and ends in your file.
<?php
echo "PHP code here";
?>
Semicolons
Every PHP statement must end with a semicolon (;). This tells PHP where one instruction ends and another begins.
<?php
echo "Statement 1";
echo "Statement 2";
?>
Case Sensitivity
Variables are case-sensitive ($name ≠ $Name), but keywords and function names are not (ECHO = echo = Echo).
<?php
$name = "John";
ECHO $name; // Works
?>
Whitespace
PHP ignores extra spaces, tabs, and line breaks. Use whitespace to make your code readable and organized.
<?php
echo "Hello" ;
echo "World";
?>
🔹 PHP Opening and Closing Tags
Different ways to use PHP tags in your code:
🔸 Standard PHP Tags (Recommended)
<?php
echo "This is the standard way";
?>
🔸 Short Echo Tags
<!-- Useful for outputting variables in HTML -->
<p>Welcome, <?= $username ?></p>
<!-- Equivalent to: -->
<p>Welcome, <?php echo $username; ?></p>
🔸 PHP-Only Files
<?php
// For files with only PHP code, omit closing tag
// This prevents accidental whitespace issues
echo "No closing tag needed";
// No ?> at the end
Best Practice: For pure PHP files (no HTML), omit the closing ?> tag to avoid whitespace problems.
🔹 PHP Statements
Understanding how to write PHP statements correctly:
<?php
// Single statement
echo "Hello";
// Multiple statements
echo "First line";
echo "Second line";
echo "Third line";
// Statement spanning multiple lines (still needs one semicolon)
echo "This is a very long statement " .
"that spans multiple lines " .
"for better readability";
// Multiple statements on one line (not recommended)
echo "One"; echo "Two"; echo "Three";
?>
Output:
HelloFirst lineSecond lineThird lineThis is a very long statement that spans multiple lines for better readabilityOneTwoThree
🔹 Mixing PHP with HTML
PHP can be embedded anywhere in HTML documents:
<!DOCTYPE html>
<html>
<head>
<title><?php echo "Dynamic Title"; ?></title>
</head>
<body>
<h1>Welcome to My Site</h1>
<?php
$username = "Alice";
$loginTime = date("h:i A");
?>
<p>Hello, <?php echo $username; ?>!</p>
<p>You logged in at <?= $loginTime ?></p>
<?php
// More PHP code
for ($i = 1; $i <= 3; $i++) {
echo "<p>Item $i</p>";
}
?>
</body>
</html>
Output:
Welcome to My Site
Hello, Alice!
You logged in at 02:30 PM
Item 1
Item 2
Item 3
🔹 Case Sensitivity Examples
Understanding what is and isn't case-sensitive in PHP:
<?php
// Keywords are NOT case-sensitive
ECHO "Hello";
echo "Hello";
Echo "Hello"; // All work the same
// Function names are NOT case-sensitive
STRLEN("text");
strlen("text");
StrLen("text"); // All work the same
// Variables ARE case-sensitive
$name = "John";
$Name = "Jane";
$NAME = "Jack";
echo $name; // Outputs: John
echo $Name; // Outputs: Jane
echo $NAME; // Outputs: Jack
// Class names are NOT case-sensitive
class MyClass {}
$obj1 = new MyClass();
$obj2 = new myclass(); // Works
$obj3 = new MYCLASS(); // Works
?>
Output:
HelloHelloHelloJohnJaneJack
🔹 PHP Output Methods
Different ways to display output in PHP:
🔸 echo Statement
<?php
// echo can output multiple strings
echo "Hello", " ", "World!";
// echo with concatenation
echo "Hello" . " " . "World!";
// echo without parentheses (common)
echo "Hello World!";
// echo with parentheses (also works)
echo("Hello World!");
?>
🔸 print Statement
<?php
// print outputs one string
print "Hello World!";
// print returns 1 (can be used in expressions)
$result = print "Hello"; // $result = 1
?>
🔸 Difference Between echo and print
- echo: Faster, no return value, can take multiple parameters
- print: Slower, returns 1, takes only one parameter
- Recommendation: Use echo for most cases (it's more common)
🔹 Common Syntax Errors
Avoid these common mistakes when writing PHP:
❌ Missing Semicolon
<?php
echo "Hello" // ERROR: Missing semicolon
echo "World";
?>
✅ Correct Version
<?php
echo "Hello"; // Semicolon added
echo "World";
?>
❌ Missing PHP Tags
echo "Hello"; // ERROR: No PHP tags
✅ Correct Version
<?php
echo "Hello";
?>
❌ Wrong Variable Case
<?php
$name = "John";
echo $Name; // ERROR: Undefined variable $Name
?>
✅ Correct Version
<?php
$name = "John";
echo $name; // Correct case
?>
🔹 Practice Example
A complete example demonstrating proper PHP syntax:
<?php
// Proper PHP syntax example
// Variables (case-sensitive)
$firstName = "John";
$lastName = "Doe";
$age = 30;
// Output with echo
echo "<h2>User Profile</h2>";
// Concatenation
echo "<p>Name: " . $firstName . " " . $lastName . "</p>";
// Variable in string
echo "<p>Age: $age years old</p>";
// Calculation
$nextAge = $age + 1;
echo "<p>Next year: $nextAge years old</p>";
// Multiple parameters with echo
echo "<p>", "Status: ", "Active", "</p>";
?>
Output:
User Profile
Name: John Doe
Age: 30 years old
Next year: 31 years old
Status: Active