PHP Basic Syntax: Script Tags, Statements, HTML Embedding & Case Rules

Mastering the fundamental syntax of PHP is the critical first step toward writing clean, bug-free, and high-performance server-side web scripts. PHP syntax is heavily influenced by the C, C++, and Java programming languages, making it instantly familiar to Computer Science and BCA students. In this lesson, you will learn standard script delimiters, statement termination rules, HTML embedding techniques, and PHP's unique case sensitivity mechanics.

Standard PHP Tags: Opening & Closing Delimiters

Because PHP files often interweave markup and backend code, the server needs a way to distinguish PHP code from raw HTML. It does this using PHP Delimiter Tags:

<?php
    // Server processes everything inside here as PHP code
    echo "Welcome to PHP Syntax Mastery on Veewom!";
?>

The Golden Pure-PHP Rule: Omitting the Closing Tag

When a PHP file contains only PHP code (such as an API controller, a database configuration file, or an Object-Oriented class), it is strongly recommended by the official PHP-FIG PSR-12 standard to omit the closing ?> tag entirely at the end of the file.

Why omit the closing tag? If a closing tag is followed by accidental trailing whitespace or newline characters, PHP will prematurely flush HTTP response headers to the browser, causing notorious errors like "Warning: Cannot modify header information - headers already sent" during cookie or session initialization!

Embedding PHP Inside HTML Pages

One of PHP's greatest strengths is its ability to seamlessly inject dynamic server calculations directly into static HTML layouts:

<!DOCTYPE html>
<html>
<head>
    <title>Dynamic Student Profile</title>
</head>
<body>
    <h1>Student Dashboard</h1>
    <?php
        $studentName = "Anjali Sharma";
        $course = "BCA Semester IV";
        $enrollmentStatus = true;
    ?>
    <div class="student-card">
        <p>Student Name: <b><?php echo $studentName; ?></b></p>
        <p>Current Course: <b><?php echo $course; ?></b></p>
        <p>Status: <span><?php echo $enrollmentStatus ? 'Active' : 'Pending'; ?></span></p>
    </div>
</body>
</html>

The Echo Short Tag: <?= $variable ?>

When you only need to output a single variable inside HTML, PHP provides a clean shorthand tag. Instead of writing <?php echo $studentName; ?>, you can simply write <?= $studentName ?>. This short tag is permanently enabled in all modern PHP versions.

Statement Termination: Semicolons (;)

In PHP, every standalone instruction must terminate with a semicolon (;). Omitting a semicolon produces a Parse error: syntax error, unexpected token.

<?php
    $x = 25;        // Correct: statement terminated
    $y = 50;        // Correct: statement terminated
    $sum = $x + $y; // Correct: statement terminated
    echo $sum;      // Correct: statement terminated
?>

PHP Case Sensitivity Rules: What Is & Isn't Sensitive?

Students often ask: "Is PHP case-sensitive?" The answer is: partially yes, partially no. Here is the definitive rulebook:

PHP Element Case Sensitive? Example Demonstration
Variable Names YES - Strictly Sensitive $score, $Score, and $SCORE are three completely separate variables in memory.
Constants YES - Strictly Sensitive define("MAX_SIZE", 100); must be referenced as MAX_SIZE.
Keywords (if, else, while, for) NO - Case Insensitive IF ($x > 0) and if ($x > 0) are identical to the Zend engine.
Function Names NO - Case Insensitive count($arr), Count($arr), and COUNT($arr) call the exact same function.
Class & Method Names NO - Case Insensitive $obj->getData() and $obj->getdata() invoke the same method.

BCA / CS University Exam Corner

High-Frequency Exam Question:

Q: Identify the syntax error in the following snippet:
<?php $color = "Red"; echo "My car is " . $Color; ?>
A: The script will trigger a Warning: Undefined variable $Color. Because PHP variable identifiers are strictly case-sensitive, $Color does not resolve to the previously defined variable $color.

Interactive Student Practice Session

Exercise 3.1: Create a webpage with an HTML table. Use PHP loop tags to render 5 rows of dynamic product IDs and product names.

Exercise 3.2: Write a script with 3 variables: $username, $Username, and $USER_NAME. Assign different values to each and verify that PHP stores them separately.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!