</>
Skip to content
PHP lessons (4/49)

PHP — Syntax

PHP tags

PHP code lives between <?php and ?>:

<?php
// PHP code here
echo "Hello";
?>

In pure PHP files, omit the closing tag:

<?php
// No closing tag needed — this is preferred
echo "Hello";

Statements end with semicolons

<?php
$x = 5;
echo "Hello\n";
$y = $x + 1;
?>

Every complete instruction ends with a semicolon. Missing semicolons cause parse errors.

Embedding PHP in HTML

PHP was designed to mix with HTML:

<?php
$name = "Ada";
$age = 36;
?>
<!DOCTYPE html>
<html>
<body>
    <h1>Hello, <?php echo $name; ?>!</h1>
    <p>You are <?php echo $age; ?> years old.</p>
</body>
</html>

The <?php ... ?> tags switch between HTML and PHP. The server executes the PHP and sends the result to the browser.

Short echo tags

<?php
$name = "Ada";
?>
<p>Hello, <?= $name ?>!</p>

<?= $name ?> is shorthand for <?php echo $name ?>. It's always available and widely used.

Variables

PHP variables start with $:

<?php
$age = 25;          // integer
$name = "Ada";      // string
$pi = 3.14;         // float
$active = true;     // boolean
$items = [1, 2, 3]; // array
$data = null;       // null
?>

Variable names are case-sensitive: $name ≠ $Name.

Naming rules

<?php
$student_name = "Ada";     // good: snake_case
$studentName = "Ada";      // legal, but PHP convention is snake_case
$2cool = "x";              // ERROR: can't start with a digit
$my-name = "x";            // ERROR: dash means subtraction
?>

PHP convention uses snake_case for variables and functions, PascalCase for classes.

Comments

<?php
// Single-line comment

# Also a single-line comment

/*
   Multi-line comment
   spans several lines
*/

echo "Code runs";
?>

Blocks

Use curly braces to group statements:

<?php
if ($age >= 18) {
    echo "Adult";
    echo "Can vote";
}
?>

Semicolons in arrays

<?php
$fruits = [
    "apple",
    "banana",
    "cherry",  // trailing comma is fine
];

$person = [
    "name" => "Ada",
    "age" => 36,  // trailing comma is fine
];
?>

Trailing commas are allowed and encouraged — they make diffs cleaner.

Strict vs loose comparison

<?php
// Loose comparison (==) — type coercion
echo 10 == "10";    // true
echo 0 == "";       // true
echo null == false;  // true

// Strict comparison (===) — no coercion
echo 10 === "10";   // false (different types)
echo 0 === "";      // false (different types)
echo null === false; // false (different types)
?>

Always use === and !== to avoid unexpected type coercion.

Error handling

<?php
// Display errors (development only)
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Try-catch
try {
    throw new Exception("Something went wrong");
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
?>

Include files

<?php
// Include a file (warning on failure)
include 'config.php';

// Require a file (fatal error on failure)
require 'database.php';

// Include once
include_once 'helpers.php';

// Require once
require_once 'config.php';
?>

Use require for files your script can't run without. Use include for optional files.

Constants

<?php
// Define a constant
define('APP_NAME', 'Codenatomy');
define('MAX_USERS', 1000);

echo APP_NAME;   // Codenatomy
echo MAX_USERS;  // 1000

// Can't reassign
// APP_NAME = "New"; // ERROR
?>

Constants don't use $ and can't be changed after definition.

Mini Practice

  1. Create a PHP file that embeds variables in HTML output
  2. Use the short echo tag <?= to output a variable
  3. Compare == and === with different types — observe the differences
  4. Create an array of five items and print each one
  5. Define three constants and use them in a formatted string

Next: variables and data types →

Related Topics

Frequently Asked Questions about Syntax

What is Syntax in PHP?

Syntax is a fundamental concept in PHP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Syntax?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Syntax.

Why is Syntax important in PHP?

Syntax is essential for PHP development. Understanding this concept will help you write better code and solve real-world problems more effectively.