PHP ยท Chapter 3 of 44

PHP Syntax

A PHP script starts with <?php and ends with ?>. Anything outside these tags is treated as plain HTML and sent directly to the browser. Each PHP statement ends with a semicolon, just like many other C-style languages.

PHP is loosely typed, meaning you don't need to declare a variable's type in advance, and PHP is mostly case-insensitive for keywords and function names, but variable names are case-sensitive.

Syntax
<?php
  // PHP code here
?>

PHP tags

The standard way to write PHP is <?php ... ?>. You can embed multiple PHP blocks inside a single HTML file, switching freely between HTML and PHP.

Case sensitivity

Keywords like if, echo and function are case-insensitive, but variables such as $Name and $name are treated as two completely different variables.

Example 1 (php)
<?php
  $x = 5;
  $y = 10;
  echo $x + $y;
?>
Output
15

PHP code between the tags is executed, and the result is sent to the browser.

Example 2 (php)
<!DOCTYPE html>
<html>
<body>

<?php
  echo "This is inside HTML!";
?>

</body>
</html>
Output
This is inside HTML!

PHP can be embedded anywhere inside a regular HTML document.

Key points

  • PHP code is wrapped in <?php ... ?> tags.
  • Each statement ends with a semicolon.
  • PHP variables are case-sensitive, but keywords are not.
  • PHP can be mixed freely with HTML markup.
๐Ÿ’ก Note: Modern PHP files often omit the closing ?> tag at the end of a pure-PHP file to avoid accidental whitespace output.

๐Ÿ“ Quick Quiz

1. How does a PHP block start?

2. Which of these is case-sensitive in PHP?

3. What character ends a PHP statement?