This chapter introduces the fundamental rules and structure required to write any valid PHP script.
1. PHP Tags (The Essential Rule)
All PHP code must be contained within specific start and end tags. The web server uses these tags to identify the code that needs to be processed by the PHP interpreter. Anything outside these tags is treated as standard HTML.
The standard and most recommended tags are:
| Tag Type | Code | Description |
| Opening Tag | <?php | Marks the beginning of a block of PHP code. |
| Closing Tag | ?> | Marks the end of a block of PHP code. |
Example:
PHP
<!DOCTYPE html>
<html>
<body>
<h1>This is HTML</h1>
<?php
echo "<h2>This is PHP output!</h2>"; // PHP starts here
?>
<p>This is HTML again.</p>
</body>
</html>
2. Statements and Semicolons
In PHP, every single line of code that performs an action (a statement) must be terminated with a semicolon (;).
- Correct:
$name = "Alice"; - Incorrect:
$name = "Alice"(This will cause an error.)
Example:
PHP
<?php
$x = 5; // First statement
$y = 10; // Second statement
echo $x + $y; // Third statement
?>
3. Case Sensitivity
PHP is partially case-sensitive. It is critical to know what must be matched exactly and what can be written in any case.
| Element | Case-Sensitive? | Example |
| Variables | YES (Crucial!) | $name and $NAME are two completely different variables. |
| Keywords | NO | echo, if, else, while can be written as ECHO, Echo, or echo. (It’s best practice to use all lowercase). |
| Function Names | NO | strtoupper() and Strtoupper() are the same function. (Best practice is lowercase). |
Best Practice: Always treat everything in PHP as case-sensitive to avoid errors, especially variables.
4. Variables (Containers for Data)
Variables are used to store data, like numbers, text, or dates. All PHP variables must follow these three simple rules:
- A variable must start with a dollar sign (
$). - It must be followed by the variable’s name.
- The variable name cannot start with a number.
Example:
PHP
<?php
// Valid variables
$firstName = "John"; // Text data
$age = 35; // Numeric data
$_isValid = true; // Boolean data
// Outputting variables
echo "Hello, " . $firstName . "!";
?>
5. PHP Comments
Comments are lines in your code that are completely ignored by the PHP interpreter. They are used to help document the code for yourself or other developers.
PHP supports two main types of commenting styles:
| Style | Syntax | Used For |
| Single-Line | // This is a comment | Brief notes, or commenting out a single line of code. |
| Single-Line | # This is also a comment | An older style, still supported. |
| Multi-Line | /* This is a comment that can span multiple lines. */ | Detailed explanations or commenting out large blocks of code. |
Example:
PHP
<?php
// Set up two variables for a calculation
$num1 = 10;
$num2 = 5;
/*
The calculation below adds the two numbers
and displays the result to the browser.
*/
echo $num1 + $num2;
?>
Next Steps
In the next chapter, we will delve deeper into variables by exploring the different PHP Data Types (Strings, Integers, Floats, Arrays, etc.) and learning how to work with them.
