PHP Syntax Guide: Learn the Basics with Code Examples

Last updated 4 months ago | 291 views 75     5

Tags:- PHP

Introduction: Why Understanding PHP Syntax Matters

Learning any programming language starts with mastering its syntax. In PHP, a proper understanding of syntax ensures your scripts run without errors and behave as expected.

PHP (Hypertext Preprocessor) is a server-side scripting language designed for creating dynamic web pages. Whether you want to build a blog, develop an e-commerce site, or automate backend operations, PHP syntax is your entry point into the action.

This article provides a complete breakdown of PHP syntax with simple, real-world examples.


Getting Started: PHP Tags

Every PHP script starts with PHP opening and closing tags:

<?php
// PHP code goes here
?>

✅ PHP code must be written inside these tags. HTML outside of these tags is sent directly to the browser.


PHP Syntax Basics: Step-by-Step

✅ 1. Comments

Comments make your code readable.

// This is a single-line comment

# Another way to write single-line comments

/*
 This is a 
 multi-line comment
*/

✅ 2. Variables

PHP variables start with a $ sign and are case-sensitive.

<?php
$name = "Alice";
$age = 25;
echo "Name: $name, Age: $age";
?>

✅ 3. Data Types

PHP supports several data types:

Data Type Example
String $name = "John";
Integer $age = 30;
Float $price = 9.99;
Boolean $isActive = true;
Array $fruits = array("Apple", "Banana");
Object class Person {}
NULL $data = NULL;

✅ 4. Constants

Constants hold values that cannot change during execution.

define("SITE_NAME", "StudyZone4U");
echo SITE_NAME;

✅ 5. Operators

✅ Arithmetic Operators

$a = 10;
$b = 5;
echo $a + $b; // Output: 15

✅ Comparison Operators

$a == $b   // Equal
$a != $b   // Not Equal
$a > $b    // Greater than

✅ Logical Operators

($a == 10 && $b == 5)  // true
($a == 10 || $b == 3)  // true

✅ 6. Conditional Statements

$score = 80;
if ($score >= 75) {
    echo "Pass";
} else {
    echo "Fail";
}

✅ 7. Loops

for loop

for ($i = 0; $i < 5; $i++) {
    echo $i . "<br>";
}

while loop

$i = 0;
while ($i < 5) {
    echo $i . "<br>";
    $i++;
}

✅ 8. Functions

function greet($name) {
    return "Hello, $name!";
}

echo greet("Vinay");

✅ 9. Arrays

$colors = array("Red", "Green", "Blue");
echo $colors[0]; // Red

Looping through arrays

foreach ($colors as $color) {
    echo $color . "<br>";
}

Complete Functional Example

Here’s a simple script that demonstrates PHP syntax in action.

<?php
define("WEBSITE", "StudyZone4U");

$name = "John";
$age = 30;
$skills = array("PHP", "HTML", "CSS");

function introduce($name, $age, $skills) {
    echo "<h2>Welcome to " . WEBSITE . "</h2>";
    echo "Name: $name<br>";
    echo "Age: $age<br>";
    echo "Skills:<br>";
    foreach ($skills as $skill) {
        echo "- $skill <br>";
    }
}

introduce($name, $age, $skills);
?>

⚠️ Tips & Common Pitfalls

✅ Tips

  • Always end statements with a semicolon (;)

  • Use indentation and comments to improve readability

  • Use var_dump() for debugging variables

  • Stick to lowercase variable names to avoid confusion

❌ Common Pitfalls

  • Omitting semicolons

  • Not using <?php ?> properly

  • Variables are case-sensitive

  • Forgetting quotes for strings

  • Using = instead of == in conditions


PHP Syntax Cheat Sheet

Concept Syntax Example
Start tag <?php ... ?>
Variable $name = "Alice";
Echo echo "Hello";
If-else if ($x > 0) { ... } else { ... }
Function function greet($name) { ... }
Array $arr = array("A", "B");
Loop for ($i=0; $i<3; $i++) { ... }

Conclusion & Best Practices

Mastering PHP syntax is your first real step toward building dynamic web applications. Once you're comfortable with the basics, you can move on to PHP forms, database handling, and OOP in PHP.

Key Takeaways

  • Always structure your code for readability

  • Avoid syntax errors by understanding PHP rules

  • Practice writing small scripts to build muscle memory