Skip to content

PHP Syntax

PHP scripts are typically embedded within HTML documents. The PHP interpreter executes the code blocks found within special tags, and the output (usually HTML) is sent to the browser.

A PHP script block can be placed anywhere in an HTML document. The standard and recommended tag is <?php ... ?>.

<?php
// PHP code goes here
echo "This part is processed by PHP.";
?>

Short echo tags <?= ... ?> are available since PHP 5.4 and are useful for outputting simple expressions. They are equivalent to <?php echo ... ?>.

<p>Your username is: <?= htmlspecialchars($username, ENT_QUOTES, 'UTF-8'); ?></p>

Avoid using short tags <? ... ?> and ASP-style tags <% ... %> as they might be disabled or removed in future PHP versions and harm portability.

The default file extension for files containing PHP code is .php.

A typical PHP file contains HTML markup mixed with PHP code blocks:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My First PHP Page</title>
</head>
<body>
<h1>Welcome!</h1>
<p>Today's date is:
<?php
// Use the recommended DateTimeImmutable class
$today = new DateTimeImmutable();
echo $today->format('Y-m-d');
?>
</p>
<?php
// Define a variable
$message = "Hello from PHP!";
// Output the variable
echo "<p>" . htmlspecialchars($message, ENT_QUOTES, 'UTF-8') . "</p>";
?>
</body>
</html>

Note: PHP statements typically end with a semicolon (;). The closing tag ?> automatically implies a semicolon for the last statement in a block, but it’s good practice to include it explicitly. If a file contains only PHP code, the closing tag ?> should preferably be omitted to prevent accidental whitespace output.

Comments are parts of the code ignored by the PHP interpreter. They are essential for explaining code logic, documenting functionality, or temporarily disabling code.

Use comments to:

  • Explain why the code is written a certain way, not just what it does.
  • Document functions, classes, and complex algorithms.
  • Leave notes for yourself or other developers.
  • Temporarily disable lines of code during debugging.

PHP supports C-style and shell-style comments:

<?php
// This is a single-line comment
# This is also a single-line comment (less common)
/*
This is a multi-line comment block.
It can span several lines.
*/
// Use comments to explain complex parts or leave notes:
$discount = $price * 0.1; // Apply a 10% discount
/* Temporarily disable code:
if ($userIsAdmin) {
// grantAccess();
}
*/
$x = 5 + /* 15 */ + 5; // Comments can appear within statements
echo $x; // Outputs 10
// Tip: Use PHPDoc blocks (/** ... */) for documenting functions and classes
/**
* Adds two numbers.
*
* @param int|float $a The first number.
* @param int|float $b The second number.
* @return int|float The sum of the two numbers.
*/
function add($a, $b) {
return $a + $b;
}
?>

PHP’s case sensitivity rules can sometimes confuse beginners:

  • Case-insensitive: Language constructs (e.g., if, else, while, echo, return), function names (built-in and user-defined), class names, method names, keywords, and type names (e.g. string, int, array).
  • Case-sensitive: Variable names (including superglobals like $_GET, $_POST), constants (by convention, usually defined in uppercase). Class properties are also case-sensitive.

Example:

<?php
// Function names are case-insensitive
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
// Variable names ARE case-sensitive
$color = "red";
$Color = "blue";
echo "The car is " . $color . "<br>"; // Outputs: The car is red
echo "The sky is " . $Color . "<br>"; // Outputs: The sky is blue
// echo "The boat is " . $COLOR . "<br>"; // This would cause an Undefined variable error
// Constants are case-sensitive
define("GREETING", "Hello there!");
echo GREETING; // Outputs: Hello there!
// echo greeting; // This would cause an error (assumes 'greeting' is a string literal)
?>

Best Practice: Although function and class names are case-insensitive, it’s crucial to follow consistent coding standards for readability and maintainability. The widely adopted PSR standards recommend:

  • camelCase for function and method names (myFunction, calculateTotal).
  • PascalCase (or UpperCamelCase) for class names (MyClass, UserController).
  • UPPER_CASE with underscores for constants (MAX_USERS, DEFAULT_TIMEOUT).
  • camelCase or snake_case for variable names (stick to one style within a project, camelCase is common in modern PHP: $userName, $totalAmount).

Adhering to these conventions makes your code easier for others (and your future self) to understand.