Learn PHP Keywords List Reserved Words in 2023

Php All Keywords List| All Php Keywords List

PHP Keywords: PHP, a keyword is a reserved word that has a specific meaning in the programming language. These words cannot be used as variables, function names, or class names.

Learn PHP Keywords List Reserved Words in 2023
Learn PHP Keywords List Reserved Words in 2023

    Here is a list of some of the most commonly used PHP keywords:

    abstractandarray
    asbreakcase
    catchclassclone
    continuedeclaredefault
    diedoecho
    elseelseifempty
    enddeclareendforendforeach
    endifendswitchendwhile
    evalexitextends
    finalforforeach
    functionglobalgoto
    ifimplementsinclude
    include_onceinstanceofinsteadof
    interfaceissetlist
    namespacenewor

    Here is a list of some of the most commonly used PHP

    abstract Keyword: The abstract keyword is a modifier that can be used to define abstract classes and methods in PHP.

    An abstract class is a class that cannot be instantiated, and serves as a base class for one or more derived classes.

    abstract class Animal 

    {

      // Abstract method

      abstract public function makeSound();

      

      // Concrete method

      public function eat() 

    {

        echo 'Eating...';

      }

    }


    and Keyword: The and keyword is a logical operator in PHP that combines two expressions and returns true if both expressions are true, and false otherwise. It is a short-circuit operator, meaning that if the first expression evaluates to false, the second expression is not evaluated.

    if ($a > 0 and $b > 0) 

    {

      echo 'Both $a and $b are positive';

    }


    array Keyword: The array keyword is a language construct in PHP that creates an array.

    An array is a data structure that stores a collection of values, and allows you to access them using a numeric index. Arrays in PHP are implemented as ordered maps, which means that you can also access array elements using string keys.

    $fruits = array(

      'a' => 'apple',

      'b' => 'banana',

      'o' => 'orange',

    );


    echo $fruits['a']; // prints 'apple'

    echo $fruits['b']; // prints 'banana'

    echo $fruits['o']; // prints 'orange'


    as Keyword: The as keyword is used in PHP to specify an alias for a class, interface, or namespace.

    NamespaceName as npn;


    // You can now use the alias "npn" to refer to the namespace "NamespaceName"

    npn\MyClass::someMethod();


    break Keyword: The break keyword is used in PHP to exit a loop prematurely.

    for ($i = 0; $i < 10; $i++) 

    {

      if ($i == 5) 

    {

        break;

      }

      echo $i;

    }


    case Keyword: The case keyword is used in PHP to specify a block of code to be executed when a value matches a case in a switch statement.

    $x = 2;


    switch ($x) {

      case 1:

      case 2:

        echo 'Case 1 or 2';

        break;

      case 3:

        echo 'Case 3';

        break;

      default:

        echo 'Default';

    }


    catch Keyword: The catch keyword is used in PHP to handle exceptions thrown by a try block.

    Exceptions are errors that occur during the execution of a program, and can be thrown using the throw keyword.

    try {

      // Code that may throw an exception

      throw new Exception('An exception occurred');

    } catch (InvalidArgumentException $e) {

      // Code to handle InvalidArgumentException

    } catch (Exception $e) {

      // Code to handle other exceptions

      echo 'Exception: ' . $e->getMessage();

    }


    class Keyword: The class keyword is used in PHP to define a class.

    A class is a blueprint for creating objects. It defines the properties and methods that objects created from the class will have.

    class Car {

      public $make;

      public $model;

      public $year;


      public function honk() {

        echo 'Honk honk';

      }

    }


    clone Keyword: The clone keyword is used in PHP to create a copy of an object.

    When you create a copy of an object using the clone keyword, a new object is created with the same properties and methods as the original object. However, the new object is independent of the original object, and any changes made to the properties or methods of the new object will not affect the original object.

    class Car {

      public $make;

      public $model;

      public $year;


      public function __construct($make, $model, $year) {

        $this->make = $make;

        $this->model = $model;

        $this->year = $year;

      }

    }


    $car1 = new Car('Toyota', 'Camry', 2022);

    $car2 = clone $car1;


    $car2->year = 2023;


    echo $car1->year; // Outputs 2022

    echo $car2->year; // Outputs 2023


    continue Keyword: The continue keyword is used in PHP to skip the rest of the current iteration of a loop and proceed to the next iteration.

    for ($i = 0; $i < 10; $i++) {

      if ($i % 2 == 0) {

        continue;

      }

      echo $i;

    }


    declare Keyword: The declare statement is used to set execution directives for a block of code. It can be used to set the following directives:

    ticks: Specifies the number of statements that should be executed before triggering a tick handler.

    encoding: Specifies the encoding to be used in the script.

    declare(ticks=1) {

      // code block

    }


    default Keyword: The default keyword is used in a switch statement to specify a block of code to be executed if none of the case statements match the value of the switch expression.

    switch ($i) {

      case 0:

        echo "i is 0";

        break;

      case 1:

        echo "i is 1";

        break;

      default:

        echo "i is not 0 or 1";

    }


    die Keyword: The die function is used to terminate the execution of a script. It can be used to display a message and stop the script, or just stop the script without displaying a message.

    die("An error has occurred");


    die();


    Why do we use :: in PHP?

    do Keyword: The do keyword is used to create a loop that will execute a block of code at least once, regardless of whether the loop condition is initially true or false.

    $i = 1;

    do {

      echo $i;

      $i++;

    } while ($i <= 10);


    echo Keyword: The echo statement is used to output a string or other data to the screen in PHP. It is similar to the print statement, but it is faster and has a slightly different syntax.

    echo "Welcome To Learncoding";


    else Keyword: The else keyword is used in conjunction with an if statement to specify a block of code to be executed if the condition in the if statement is false.

    if ($x > 0) {

      echo "x is positive";

    } else {

      echo "x is not positive";

    }


    elseif Keyword: The elseif keyword is used in conjunction with an if statement to specify additional conditions to be tested if the initial condition is false. It allows you to test multiple conditions in an if statement.

    if ($x > 0) {

      echo "x is positive";

    } elseif ($x < 0) {

      echo "x is negative";

    } else {

      echo "x is 0";

    }


    empty Keyword: The empty function is used to determine whether a variable is empty or not. A variable is considered empty if it is:

    1. "" (an empty string)

    2. 0 (0 as an integer)

    3. 0.0 (0 as a float)

    4. "0" (0 as a string)

    5. NULL

    6.FALSE

    if (empty($var)) {

      echo "The variable is empty";

    } else {

      echo "The variable is not empty";

    }


    enddeclare Keyword: enddeclare is a control structure in PHP that is used in conjunction with the declare statement to create a block of code in which a directive is in effect. The declare statement is used to set a directive for a block of code, and the enddeclare statement marks the end of the block.

    declare(strict_types=1) {

        // code in this block will be executed with strict typing enabled

    }


    // code outside the declare block will not have strict typing enabled


    endfor Keyword: endfor is a control structure in PHP that is used to mark the end of a for loop. The for loop is a type of loop that allows you to execute a block of code a specified number of times.

    for ($i = 0; $i < 10; $i++) {

        // code in this block will be executed 10 times

    }


    // the endfor statement is not needed, but you can use it to explicitly mark the end of the loop

    endfor;


    endforeach Keyword: endforeach is a control structure in PHP that is used to mark the end of a foreach loop. The foreach loop is a type of loop that allows you to iterate over the elements of an array or an object.

    $colors = array('red', 'green', 'blue');


    foreach ($colors as $color) {

        // code in this block will be executed once for each element in the $colors array

        echo $color;

    }


    // the endforeach statement is not needed, but you can use it to explicitly mark the end of the loop

    endforeach;


    endif Keyword: endif is a control structure in PHP that is used to mark the end of an if statement. The if statement is a type of conditional statement that allows you to execute a block of code only if a certain condition is met.

    if ($a > $b) {

        // code in this block will be executed only if $a is greater than $b

    }


    // the endif statement is not needed, but you can use it to explicitly mark the end of the if statement

    endif;


    endswitch Keyword: endswitch is a control structure in PHP that is used to mark the end of a switch statement. The switch statement is a type of conditional statement that allows you to execute a block of code based on the value of a particular expression.

    $fruit = 'apple';


    switch ($fruit) {

        case 'apple':

            // code in this block will be executed if $fruit is equal to 'apple'

            break;

        case 'banana':

            // code in this block will be executed if $fruit is equal to 'banana'

            break;

        default:

            // code in this block will be executed if $fruit is not equal to 'apple' or 'banana'

            break;

    }


    // the endswitch statement is not needed, but you can use it to explicitly mark the end of the switch statement

    endswitch;


    endwhile Keywordendwhile is a control structure in PHP that is used to mark the end of a while loop. The while loop is a type of loop that allows you to execute a block of code as long as a certain condition is met.

    $i = 0;


    while ($i < 10) {

        // code in this block will be executed as long as $i is less than 10

        $i++;

    }


    // the endwhile statement is not needed, but you can use it to explicitly mark the end of the loop

    endwhile;


    eval Keyword: The eval() function in PHP allows you to execute PHP code stored in a string.

    $string = 'echo "Hello, world!";';

    eval($string);


    exit Keyword: The exit statement in PHP terminates the current script and optionally prints a message. 

    exit('An error has occurred');


    extends Keyword: PHP, the extends keyword is used to create a class that is a child of another class. This is known as inheritance.

    class Animal {

      public function makeSound() {

        echo "Some generic animal sound";

      }

    }


    class Dog extends Animal {

      public function makeSound() {

        echo "Woof!";

      }

    }


    $dog = new Dog();

    $dog->makeSound();  // Outputs "Woof!"


    How to use final keyword in PHP?

    final Keyword: PHP, the final keyword can be used to prevent a class or method from being overridden.

    class Animal {

      final public function makeSound() {

        echo "Some generic animal sound";

      }

    }


    class Dog extends Animal {

      public function makeSound() {  // This will cause an error

        echo "Woof!";

      }

    }


    for Keyword: The for loop in PHP is used to execute a block of code a specified number of times.

    for ($i = 1; $i <= 10; $i++) {

      echo $i . "\n";

    }


    foreach Keyword: The foreach loop in PHP is used to iterate over arrays and objects.

    $array = array(1, 2, 3, 4, 5);

    foreach ($array as $value) {

      echo $value . "\n";

    }


    function Keyword: PHP, the function keyword is used to define a function. A function is a block of code that can be called by name and that can perform a specific task.

    function add($x, $y) {

      return $x + $y;

    }


    global Keyword: PHP, the global keyword is used to access variables that are defined outside the current function or method.

    $x = 10;


    function foo() {

      global $x;

      $y = 20;

      echo $x + $y;

    }


    foo();  // Outputs 30


    goto Keyword: The goto statement in PHP allows you to transfer control to a different part of your code. 

    $i = 0;


    loop:

      echo $i . "\n";

      $i++;

      if ($i < 10) {

        goto loop;

      }


    if Keyword: PHP, the if keyword is used to execute a block of code only if a certain condition is true.

    if ($num > 0) {

      echo "$num is a positive number.";

    } else {

      echo "$num is not a positive number.";

    }


    implements Keyword: PHP, the implements keyword is used in class declarations to specify that a class implements one or more interfaces. An interface is a set of methods that a class must implement.

    interface Animal {

      public function makeSound();

    }


    class Dog implements Animal {

      public function makeSound() {

        echo "Woof!";

      }

    }


    include Keyword: PHP, the include keyword is used to include a specific file in a PHP script. The included file can contain PHP code, HTML code, or any other text.

    include "header.php";


    include_once Keyword: PHP, the include_once keyword is used to include a specific file in a PHP script, but only if it has not already been included. The included file can contain PHP code, HTML code, or any other text.

    include_once "header.php";


    instanceof Keyword: PHP, the instanceof keyword is used to check if an object is an instance of a particular class or implements a particular interface. It returns a boolean value indicating whether the object is an instance of the class or interface.

    $obj = new MyClass();

    if ($obj instanceof MyClass) 

    {

      // $obj is an instance of MyClass

    }


    What is new keyword in PHP?

    new Keyword: PHP, the new keyword is used to create a new instance of a class. It is used in conjunction with the class keyword to create an object.

    class MyClass {

      public $property;

      

      public function __construct() {

        $this->property = "Hello, world!";

      }

    }


    $obj = new MyClass();

    echo $obj->property; // outputs "Hello, world!"


    Post a Comment

    0 Comments