PHP keywords are reserved words that have special meaning in the PHP language. They are used to define language structure such as control flow, functions, classes, variables, and more.
🔒 You cannot use PHP keywords as variable names, function names, class names, or constants.
Example Keywords
| Keyword | Usage Example |
|---|---|
if |
if ($a > $b) { ... } |
else |
else { ... } |
function |
function greet() { ... } |
class |
class User { ... } |
echo |
echo "Hello World"; |
return |
return $value; |
try/catch |
try { ... } catch(Exception $e) { ... } |
foreach |
foreach ($array as $item) { ... } |
public |
public $name; (OOP visibility) |
new |
$user = new User(); |
include |
include 'file.php'; |
File Name: php-keywords-example.php
<?php
class Student {
public $name;
function setName($name) {
$this->name = $name;
}
function getName() {
return $this->name;
}
}
$student = new Student();
$student->setName("John");
echo $student->getName(); // Output: John
?>
This code uses the following keywords: class, public, function, return, new, and echo.
Invalid Example (Don’t Do This)
$if = "Hello"; // ❌ Bad practice — 'if' is a keyword
Summary
| Type | Examples |
|---|---|
| Control Keywords | if, else, switch, while |
| Function | function, return, yield |
| Class/OOP | class, extends, implements |
| Visibility | public, private, protected |
| Error Handling | try, catch, throw |
| Include/Require | include, require, use |



