Keywords in PHP
? Keywords in PHP
In PHP, keywords are reserved words that have special meaning to the PHP parser and cannot be used as identifiers (e.g., variable names, function names, class names, etc.). They are used to define the structure and behavior of the PHP language.
PHP has many keywords that are used for various purposes, including control structures, data types, error handling, and class or function definitions.
1. List of PHP Keywords
Here is a list of the most commonly used PHP keywords:
Language Constructs and Control Structures:
if Used for conditional statements.else Used in conjunction withifto define an alternative block of code.elseif Used to define an alternative condition in anifblock.endif Used to end anifblock when using alternative syntax.while Used to create a while loop.do Used withwhileto define a do-while loop.for Used to define a for loop.foreach Used to iterate over arrays.switch Used to define a switch statement for multiple conditions.case Used in conjunction withswitchto specify possible conditions.break Terminates a loop or switch statement.continue Skips the current iteration of a loop and proceeds with the next one.goto Used to jump to another section in the program (not commonly used).return Returns a value from a function.exit Ends the execution of the script.
Variables and Data Types:
null Represents a variable with no value.true Boolean true value.false Boolean false value.int,float,string,bool,array,object,resource Data types in PHP.
Function and Class Definitions:
function Used to define a function.class Used to define a class.extends Used to inherit a class.implements Used to implement an interface.abstract Defines an abstract class or method.final Used to define a class or method that cannot be extended or overridden.public,private,protected Access modifiers for class properties and methods.static Declares class methods and properties that belong to the class rather than an instance.const Used to declare class constants.new Used to instantiate a class.this Refers to the current object instance.self Refers to the current class in a static context.parent Refers to the parent class.__construct Constructor method.__destruct Destructor method.
Exception Handling:
try Used to begin a block of code that may throw exceptions.catch Used to handle exceptions thrown in atryblock.finally A block of code that is always executed after atry/catchblock, regardless of whether an exception was thrown.throw Used to throw an exception.
Namespaces and Autoloading:
namespace Used to define a namespace for organizing code.use Imports classes or namespaces into the current file.
Global Scope and Superglobals:
global Used to access global variables from within a function.GLOBALS A superglobal array that contains all global variables.$_GET,$_POST,$_REQUEST,$_SESSION,$_COOKIE,$_FILES,$_SERVER Superglobal arrays for accessing various types of input data.
Other Keywords:
echo,print Output statements.isset Checks if a variable is set and is notnull.unset Unsets a variable.empty Checks if a variable is empty.include Includes and evaluates a specified file.require Includes and evaluates a specified file (causes a fatal error if the file is not found).include_once Includes and evaluates a file only once.require_once Includes and evaluates a file only once (causes a fatal error if the file is not found).eval Evaluates a string as PHP code.exit Terminates the execution of the script.die Similar toexit, it terminates the script.
2. Examples of Using Keywords in PHP
Using if, else, and elseif:
<?php$number = 10;if ($number > 0) { echo "Positive number.";} elseif ($number < 0) { echo "Negative number.";} else { echo "Zero.";}?>Using foreach:
<?php$fruits = array("apple", "banana", "cherry");foreach ($fruits as $fruit) { echo $fruit . "<br>";}?>Using function and return:
<?phpfunction addNumbers($a, $b) { return $a + $b;}echo addNumbers(5, 10); // Output: 15?>Using try, catch, and throw:
<?phptry { $value = 10; if ($value > 5) { throw new Exception("Value is too high."); }} catch (Exception $e) { echo 'Caught exception: ' . $e->getMessage();}?>3. Keywords for OOP (Object-Oriented Programming)
In PHP, object-oriented programming (OOP) features several keywords related to classes, inheritance, and access control.
Example of Using OOP Keywords:
<?phpabstract class Animal { public function sound() { echo "Some sound<br>"; }}class Dog extends Animal { public function sound() { echo "Bark<br>"; }}$dog = new Dog();$dog->sound(); // Output: Bark?>Explanation:
abstract: Defines an abstract class that cannot be instantiated directly.extends: Inherits from the parent classAnimal.public: Defines a public method that can be accessed from anywhere.
4. Reserved Keywords and Future PHP Versions
PHP has reserved keywords, which may not be used as identifiers (e.g., variable names or function names). Additionally, future PHP versions may introduce new keywords, so its important to avoid using reserved keywords for variables, functions, or class names.
PHP also has reserved keywords for future use (e.g., yield, readonly), which can be found in the PHP Manual.
5. Summary
PHP Keywords: Reserved words that have special meanings in the language and cannot be used as variable, function, or class names.
Common Keywords: Include control structures (
if,else,foreach), OOP keywords (class,extends,public), data types (int,string,bool), and exception handling keywords (try,catch,throw).Avoid Using Reserved Keywords: For future-proof code, avoid using PHP keywords or reserved words as identifiers.