PHP Syntax & Basics for Beginners

    Atul Kabra3 min readUpdated

    PHP Syntax & Basics

    PHP code lives inside special tags, runs on the server, and is written as a series of statements that each end with a semicolon. Once you learn the basic syntax rules below, you can read and write simple PHP scripts confidently.

    PHP tags

    All PHP code goes between an opening <?php tag and a closing ?> tag. Anything outside those tags is treated as plain text or HTML and passed straight through to the browser.

    <?php
    // This is PHP code and runs on the server.
    echo "Hello from PHP";
    ?>
    <p>This is plain HTML, sent as-is to the browser.</p>
    

    If a file contains only PHP, it is common practice to leave off the closing ?> tag to avoid accidental whitespace in the output.

    Statements and the semicolon

    Each instruction in PHP is a statement, and statements end with a semicolon (;). Forgetting the semicolon is one of the most common beginner errors.

    <?php
    $name = "Priya";   // a statement
    echo $name;        // another statement
    ?>
    

    Outputting text with echo and print

    PHP gives you two main ways to send text to the page:

    <?php
    // echo can print one or more values.
    echo "Learning PHP step by step";
    
    // echo with multiple values separated by commas.
    echo "Year: ", date("Y"), "\n";
    
    // print works similarly but always returns 1, so it can be used in expressions.
    print "This also appears on the page";
    ?>
    

    For everyday use, echo is the most common choice.

    Comments

    Comments are notes for humans; PHP ignores them. They make your code easier to understand.

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

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    This is also a single-line comment.

    /* This is a multi-line comment. Use it to explain a block of code. */

    echo "Comments help future you understand the code."; ?>

    
    ## Case sensitivity
    
    This trips up many beginners:
    
    - **Variable names are case-sensitive.** `$name` and `$Name` are two different variables.
    - **Function names and keywords are not case-sensitive.** `echo`, `ECHO`, and `Echo` all work, though lowercase is the convention.
    
    ```php
    <?php
    $Name = "Aarav";
    $name = "Ishita";
    
    echo $Name; // prints Aarav
    echo $name; // prints Ishita
    ?>
    

    Embedding PHP inside HTML

    A real web page usually mixes HTML and small bits of PHP. You can open and close PHP tags as many times as you like.

    <!DOCTYPE html>
    <html>
    <body>
        <h1>Today's Date</h1>
        <p>
            <?php
                // Print the current date in a readable format.
                echo date("d M Y");
            ?>
        </p>
    </body>
    </html>
    

    When this file is served, the browser receives finished HTML with the date already filled in.

    Common mistakes

    • Missing semicolons at the end of statements, which causes a parse error.
    • Forgetting the opening <?php tag, so the code prints as plain text instead of running.
    • Treating variable names as case-insensitive. $total and $Total are different.
    • Adding extra spaces or blank lines outside PHP tags, which can break things like redirects that must run before any output.

    FAQ

    Should I always use the closing ?> tag? In files that contain only PHP, it is best to omit the closing tag. In mixed HTML/PHP files, you need it to switch back to HTML.

    Is echo or print better? Use echo for most output. print is slightly slower and rarely needed for beginners.

    Why does whitespace matter? Any text outside PHP tags, including spaces, is sent to the browser. This can interfere with headers and redirects.

    Keep learning

    Head back to the PHP & MySQL hub, revisit What is PHP?, or continue with Variables & Data Types in PHP.

    Want hands-on practice with feedback? Join the waitlist for the PHP course at Infoplanet in Jalgaon and learn by building.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides