Labs ICT
โญ Pro Login

PHP Syntax

PHP syntax is simple once you get the hang of it. Every PHP file is just HTML with PHP sprinkled in. The PHP engine scans the file, runs whatever it finds between PHP tags, and leaves the rest as plain HTML.

Think of PHP tags as doorways. You're walking through HTML and every time you see <?php, you step into PHP-land. The ?> door takes you back out to HTML-land. You can open and close these doors as many times as you want.


<!DOCTYPE html>
<html>
<body>

<h1><?php echo "My PHP Page"; ?></h1>
<p>This is regular HTML.</p>

<?php echo "This came from PHP!"; ?>

</body>
</html>
    
Try it Yourself โ†’

Echo vs Print

You'll use echo constantly in PHP. It outputs text to the page. print does the same thing, but echo is slightly faster and you can pass multiple arguments to it.


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

Both work. I use echo out of habit. You'll probably end up doing the same.

Semicolons Are Not Optional

Every PHP statement ends with a semicolon. Forget one, and PHP will throw an error. This trips up everyone at some point. If you see a "Parse error" on line 12 but line 12 looks fine, check line 11 โ€” you probably missed the semicolon there.


<?php
echo "This is fine";
echo "This will break"  // missing semicolon
?>
    

One exception: the semicolon is optional before the closing ?> tag. PHP inserts one automatically. But honestly, just always use semicolons. It's a good habit.

Case Sensitivity

Here's where it gets interesting. PHP keywords (echo, if, while, etc.) and function names (strlen(), array_push(), etc.) are case-insensitive. You can write ECHO or Echo and it still works. But variable names are case-sensitive. $name and $Name are completely different variables.


<?php
ECHO "This works fine! ";  // keywords are case-insensitive
$name = "Alice";
echo $name;  // outputs Alice
echo $Name;  // error โ€” $Name is not defined
?>
    

My advice: just stick with lowercase for everything. It's cleaner, consistent, and you won't confuse yourself later.

๐Ÿงช Quick Quiz

Which statement outputs text to the browser in PHP?