PHP Syntax

PHP is a server-side scripting language used for web development. Below is the basic syntax you need to get started.

1. PHP Script Structure

A PHP script starts with <?php and ends with ?>. The script is executed on the server before sending the output to the browser.

    <?php
        echo "Hello, World!";
    ?>
    

Try It Now

2. PHP Comments

Comments in PHP are ignored by the interpreter and are used to explain code.

    <?php
        // This is a single-line comment

        # This is also a single-line comment

        /*
        This is a multi-line comment.
        It can span multiple lines.
        */
    ?>
    

Try It Now

3. PHP Variables

Variables in PHP start with a $ sign and do not require explicit type declaration.

    <?php
        $greeting = "Hello, PHP!";
        echo $greeting;
    ?>
    

Try It Now

4. PHP Case Sensitivity

PHP is case-sensitive for variable names but not for function names.

    <?php
        $Name = "John";
        echo $name; // This will cause an error
    ?>
    

Try It Now

5. PHP Echo & Print

Both echo and print can be used to output data.

    <?php
        echo "Hello, World!";
        print "Welcome to PHP!";
    ?>
    

Try It Now

6. PHP Semicolons

Each PHP statement must end with a semicolon (;).

    <?php
        echo "This is a PHP statement";
        echo "Another statement";
    ?>
    

Try It Now

Conclusion

Now you understand the basic syntax of PHP. You can start writing PHP scripts and build dynamic websites.