PHP Keywords In English -

PHP Keywords In English

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 NameUses
abstractTo 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
andUsed as a logical operator
asWorks with foreach loops
breakTo break the structure of the loop (for loop or while loop)
caseWorks with switch
catchHandles errors
classDefines a class
cloneMakes a clone of an object
constDefines a constant variable in a class
continueSkips the current iteration of the loop
dieExits the script execution; equivalent to exit
exitExits the script execution; equivalent to die
doWorks with while loops; executes the script, then checks the condition
ifExecutes if the given condition is true
elseWorks with the if statement; executes if the condition is not met
elseifWorks with if; executes if the previous condition is not met
emptyChecks if a variable has a value or not
endifEnds the if-else loop
foreachIterates through array variables
endforeachEnds the foreach loop
forIterates the loop of given instructions
endswitchEnds the switch loop
endwhileEnds the while loop
extendsInherits a class
finalDefines a final method, preventing child classes from overriding it
finallyWorks with try; the finally block runs every time
functionDefines a function
globalDefines a global variable
gotoJumps to another section of the program
implementsIndicates that a class implements an interface
includeIncludes another file in the current file
include_onceIncludes a file only once
interfaceDefines an interface
instanceofChecks if a variable is an instance of a class
isset()Checks whether a variable is defined or not
namespaceDefines a namespace
newCreates an object of a class
orLogical operator
privateDefines private variables or functions in a class
protectedDefines protected variables or functions in a class
publicDefines public variables or functions in a class
requireIncludes a file and produces a fatal error if the file does not exist
require_onceIncludes a file only once; produces a fatal error if the file does not exist
staticDefines static properties in a class
switchStarts a switch loop
traitDefines a trait
tryHandles exceptions
unset()Unsets a given variable
varDefines a variable with public visibility in a class
whileStarts a while loop
LINECurrent line number
FILEReturns the full file path with the file name
DIRReturns the directory of the file
FUNCTIONReturns the function name or {closure} for anonymous functions
CLASSReturns the class name
TRAITReturns the trait name, which may include a namespace
METHODReturns the method name
NAMESPACEReturns 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.

Leave a Comment