PHP Keywords
Reserved words and language constructs in PHP
🔑 What are PHP Keywords?
PHP keywords are reserved words with special meanings in the language. You cannot use them as variable names, function names, or class names because they control program flow and structure.
<?php
// Keywords in action
if (true) {
echo "This uses keywords!";
}
?>
Output:
Keyword Categories
Control Flow
Direct program execution path
Loops
Repeat code blocks
Structure
Define code organization
Special
Unique language features
🔹 Control Flow Keywords
Control flow keywords determine which code blocks execute based on conditions. They are fundamental to creating dynamic, decision-making programs.
<?php
$age = 18;
// if, else, elseif
if ($age < 13) {
echo "Child";
} elseif ($age < 18) {
echo "Teenager";
} else {
echo "Adult";
}
echo "\n\n";
// switch, case, default
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of week";
break;
case "Friday":
echo "End of week";
break;
default:
echo "Midweek";
}
?>
Output:
Start of week
🔹 Loop Keywords
Loop keywords allow you to repeat code multiple times. Each loop type serves different purposes, from counting iterations to processing arrays.
<?php
// for loop
for ($i = 1; $i <= 3; $i++) {
echo "Count: $i\n";
}
echo "\n";
// while loop
$x = 1;
while ($x <= 3) {
echo "While: $x\n";
$x++;
}
echo "\n";
// foreach loop
$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
echo "Color: $color\n";
}
?>
Output:
Count: 2
Count: 3
While: 1
While: 2
While: 3
Color: red
Color: green
Color: blue
🔹 Function Keywords
Function keywords help you create reusable code blocks. The return keyword sends values back from functions, while function defines them.
<?php
// function and return
function greet($name) {
return "Hello, " . $name . "!";
}
echo greet("Alice");
echo "\n\n";
// Function with multiple returns
function checkAge($age) {
if ($age >= 18) {
return "Adult";
}
return "Minor";
}
echo checkAge(25);
echo "\n";
echo checkAge(15);
?>
Output:
Adult
Minor
🔹 Class and Object Keywords
Object-oriented programming keywords define classes, create objects, and manage inheritance. These are essential for building structured, reusable code.
<?php
// class, new, public, private
class Car {
public $brand;
private $speed = 0;
public function __construct($brand) {
$this->brand = $brand;
}
public function accelerate() {
$this->speed += 10;
return $this->speed;
}
}
$myCar = new Car("Toyota");
echo "Brand: " . $myCar->brand . "\n";
echo "Speed: " . $myCar->accelerate() . " km/h";
?>
Output:
Speed: 10 km/h
🔹 Break and Continue Keywords
Break exits a loop immediately, while continue skips the current iteration and moves to the next. These control loop execution flow precisely.
<?php
// break - exit loop
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
break; // Stop at 3
}
echo "Break: $i\n";
}
echo "\n";
// continue - skip iteration
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue; // Skip 3
}
echo "Continue: $i\n";
}
?>
Output:
Break: 2
Continue: 1
Continue: 2
Continue: 4
Continue: 5
🔹 Include and Require Keywords
These keywords insert code from other files into your script. Require stops execution if the file is missing, while include continues with a warning.
<?php
// include - continues on error
include 'header.php';
// require - stops on error
require 'config.php';
// include_once - includes only once
include_once 'functions.php';
// require_once - requires only once
require_once 'database.php';
echo "All files loaded!";
?>
When to use each:
- include: Optional files (headers, footers)
- require: Critical files (config, database)
- _once: Prevent duplicate inclusions
🔹 Try, Catch, Finally Keywords
Exception handling keywords manage errors gracefully. Try contains risky code, catch handles errors, and finally runs cleanup code regardless of errors.
<?php
function divide($a, $b) {
if ($b == 0) {
throw new Exception("Cannot divide by zero!");
}
return $a / $b;
}
try {
echo "Result: " . divide(10, 2) . "\n";
echo "Result: " . divide(10, 0) . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
} finally {
echo "Calculation complete.";
}
?>
Output:
Error: Cannot divide by zero!
Calculation complete.
🔹 Common PHP Keywords List
Complete Keyword Reference:
- abstract
- and
- array
- as
- break
- callable
- case
- catch
- class
- clone
- const
- continue
- declare
- default
- do
- echo
- else
- elseif
- empty
- enddeclare
- endfor
- endforeach
- endif
- endswitch
- endwhile
- eval
- exit
- extends
- final
- finally
- fn
- for
- foreach
- function
- global
- goto
- if
- implements
- include
- include_once
- instanceof
- insteadof
- interface
- isset
- list
- match
- namespace
- new
- or
- private
- protected
- public
- readonly
- require
- require_once
- return
- static
- switch
- throw
- trait
- try
- unset
- use
- var
- while
- xor
- yield