PHP Keywords
In PHP, keywords are predefined words that have specific meanings and purposes within the language. You cannot use these keywords as variable names, constants, class names, traits, namespaces, etc., because it can create conflicts between your defined names and the actual PHP keywords, leading to errors.
Example of Using a PHP Keyword Incorrectly
<?php
class ECHO {
function __construct() {
echo 'Hello Constructor';
}
}
// Create a new object of the class
new ECHO();
?>
Output:
Parse error: syntax error, unexpected 'ECHO' (T_ECHO), expecting identifier (T_STRING) in C:\xampp\htdocs\test\test.php on line 2
In this example, we tried to create a class named ECHO
, which is not allowed because echo
is a PHP keyword used to print values. Using keywords in this way will cause errors.
Best Practices
When working on a project, ensure that your variables, constants, namespaces, class names, and traits are meaningful and do not conflict with PHP’s predefined keywords. This practice enhances code readability and helps with error handling.
List of PHP Keywords and Their Uses
Keyword Name | Uses |
---|---|
abstract | To define an abstract class |
echo / echo() | To print the value assigned in a variable |
print / print() | To print the formatted value assigned in a variable |
array() | To define an array |
and | Used as a logical operator |
as | Works with foreach loops |
break | To break the structure of the loop (for loop or while loop) |
case | Works with switch |
catch | Handles errors |
class | Defines a class |
clone | Makes a clone of an object |
const | Defines a constant variable in a class |
continue | Skips the current iteration of the loop |
die | Exits the script execution; equivalent to exit |
exit | Exits the script execution; equivalent to die |
do | Works with while loops; executes the script, then checks the condition |
if | Executes if the given condition is true |
else | Works with the if statement; executes if the condition is not met |
elseif | Works with if; executes if the previous condition is not met |
empty | Checks if a variable has a value or not |
endif | Ends the if-else loop |
foreach | Iterates through array variables |
endforeach | Ends the foreach loop |
for | Iterates the loop of given instructions |
endswitch | Ends the switch loop |
endwhile | Ends the while loop |
extends | Inherits a class |
final | Defines a final method, preventing child classes from overriding it |
finally | Works with try; the finally block runs every time |
function | Defines a function |
global | Defines a global variable |
goto | Jumps to another section of the program |
implements | Indicates that a class implements an interface |
include | Includes another file in the current file |
include_once | Includes a file only once |
interface | Defines an interface |
instanceof | Checks if a variable is an instance of a class |
isset() | Checks whether a variable is defined or not |
namespace | Defines a namespace |
new | Creates an object of a class |
or | Logical operator |
private | Defines private variables or functions in a class |
protected | Defines protected variables or functions in a class |
public | Defines public variables or functions in a class |
require | Includes a file and produces a fatal error if the file does not exist |
require_once | Includes a file only once; produces a fatal error if the file does not exist |
static | Defines static properties in a class |
switch | Starts a switch loop |
trait | Defines a trait |
try | Handles exceptions |
unset() | Unsets a given variable |
var | Defines a variable with public visibility in a class |
while | Starts a while loop |
LINE | Current line number |
FILE | Returns the full file path with the file name |
DIR | Returns the directory of the file |
FUNCTION | Returns the function name or {closure} for anonymous functions |
CLASS | Returns the class name |
TRAIT | Returns the trait name, which may include a namespace |
METHOD | Returns the method name |
NAMESPACE | Returns the namespace name |
Magic Constants in PHP
PHP provides some magic constants that return values depending on where they are used within the code. These constants can be helpful for debugging and understanding the flow of your script.
Understanding keywords and avoiding their misuse is crucial for writing clean and error-free PHP code.