Skip to content

Syntax and Variables in PHP

PHP is a server-side scripting language used to create dynamic web applications. Understanding PHP’s syntax and variables is fundamental for writing efficient scripts.


1. Basic PHP Syntax

1.1 PHP Script Structure

A PHP script is enclosed within <?php … ?> tags. The PHP code inside these tags is executed on the server, and the output is sent as HTML to the browser.

<?php

    echo “Hello, World!”;

?>

1.2 PHP File Extension

  • PHP files are saved with the .php extension (e.g., index.php).
  • A PHP file can contain PHP code, HTML, JavaScript, and CSS.

1.3 Embedding PHP in HTML

PHP can be written inside HTML to create dynamic pages.

<!DOCTYPE html>

<html>

<head>

    <title>PHP Syntax Example</title>

</head>

<body>

    <h1><?php echo “Welcome to My Website”; ?></h1>

</body>

</html>

  • The <?php echo “text”; ?> syntax outputs text inside HTML.

2. PHP Statements and Comments

2.1 Ending PHP Statements

  • Every PHP statement must end with a semicolon (;).

<?php

    echo “Hello, World!”; // Correct

?>

2.2 PHP Comments

PHP supports single-line and multi-line comments.

<?php

    // Single-line comment

    echo “Hello, PHP!”;

    # Another single-line comment

    /*

       Multi-line comment

       This comment spans multiple lines

    */

?>

  • Comments are ignored by the PHP interpreter and are used for documentation.

3. Variables in PHP

3.1 What is a Variable?

A variable in PHP is used to store data (strings, numbers, arrays, etc.). Variables start with a dollar sign ($) and are case-sensitive.

<?php

    $name = “John”;

    $age = 25;

    echo “My name is ” . $name . ” and I am ” . $age . ” years old.”;

?>

Output:

My name is John and I am 25 years old.

3.2 Rules for Naming Variables

  • Must start with a $ sign ($name).
  • Must begin with a letter or underscore ($var1, $_var).
  • Cannot contain spaces ($user_name instead of $user name).
  • Case-sensitive ($var is different from $Var).

4. Variable Types in PHP

PHP is a loosely typed language, meaning variables do not need to be declared with a type.

4.1 String Variables

A string stores text and must be enclosed in single (”) or double (“”) quotes.

<?php

    $str1 = “Hello, World!”;

    $str2 = ‘PHP is awesome!’;

    echo $str1 . ” ” . $str2;

?>

  • Double-quoted strings allow variable interpolation (“Hello, $name” will replace $name with its value).
  • Single-quoted strings do not allow interpolation (‘Hello, $name’ will output $name as text).

4.2 Integer Variables

An integer is a whole number (positive or negative).

<?php

    $x = 100;

    $y = -50;

    echo $x + $y; // Output: 50

?>

4.3 Float (Decimal) Variables

A float (or double) is a number with a decimal point.

<?php

    $price = 9.99;

    echo “The price is $” . $price;

?>

4.4 Boolean Variables

A boolean can only have two values: true or false.

<?php

    $is_logged_in = true;

    echo $is_logged_in; // Output: 1 (true is displayed as 1)

?>

4.5 Array Variables

An array stores multiple values in a single variable.

<?php

    $fruits = array(“Apple”, “Banana”, “Cherry”);

    echo “I like ” . $fruits[0]; // Output: I like Apple

?>

4.6 Null Variables

A null variable has no value.

<?php

    $x = null;

    echo $x; // Output: (empty)

?>


5. Variable Scope in PHP

Variable scope defines where a variable can be accessed.

5.1 Local Scope

Variables declared inside a function are local and cannot be used outside.

<?php

function myFunction() {

    $num = 10; // Local variable

    echo $num;

}

myFunction();

echo $num; // Error: Undefined variable

?>

5.2 Global Scope

Variables declared outside functions are global but cannot be accessed inside functions directly.

<?php

    $message = “Hello, World!”; // Global variable

    function displayMessage() {

        global $message; // Access global variable

        echo $message;

    }

    displayMessage(); // Output: Hello, World!

?>

5.3 Static Variables

Static variables retain their value across function calls.

<?php

function counter() {

    static $count = 0;

    $count++;

    echo $count;

}

counter(); // Output: 1

counter(); // Output: 2

?>


6. Variable Manipulation in PHP

6.1 Checking Variable Type

PHP provides functions to check a variable’s type.

<?php

    $num = 10;

    echo gettype($num); // Output: integer

?>

6.2 Type Casting

You can convert a variable’s type.

<?php

    $x = “100”;

    $y = (int) $x; // Cast string to integer

    echo gettype($y); // Output: integer

?>

6.3 isset() and empty() Functions

  • isset($var): Checks if a variable is set and not null.
  • empty($var): Checks if a variable is empty (0, null, false, “”).

<?php

    $var1 = “Hello”;

    $var2 = “”;

    echo isset($var1); // Output: 1 (true)

    echo empty($var2); // Output: 1 (true)

?>


7. Conclusion

  • PHP variables store different data types and are loosely typed.
  • PHP supports local, global, and static variable scopes.
  • Using isset() and empty() helps check variable values.
  • Understanding PHP syntax and variables is essential for dynamic web development.