List of all PHP Keywords with the Outputs

List of all PHP Keywords with the Outputs. Keywords are words that operate some meaning. In the general use of PHP language, these words cannot be used as a constant, a class name, a variable name, a method name, etc. These keywords when used are automatically interpreted by PHP. These PHP keywords, when utilized with variable names, can be blended with the actual keywords. Therefore, these keywords should not be used as variable names.

List of all PHP keywords

abstract and Array()as
breakcallablecasecatch
classclonecostcontinue
declaredefaultdie()do
echoelseelseifempty
enddeclareendforendforeachendif
endswitchendwhileeval()exit()
extendsfinalfinallyfor
foreachfunctionglobalgoto
ifimplementsincludeinclude_once
instanceofinsteadofinterfaceisset()
list()namespace()newor
printprivateprotectedpublic
requirerequire_oncereturnstatic
switchthrowtraittry
unset()usevarwhile
yieldyield_fromxor

Abstract Keyword:

<?php
abstract class Code {
abstract protected function Methodlive();
public function show() {
echo '<br>'.$this->Methodlive();}}
class CodeOne extends Code {
protected function Methodlive() {
echo '<br>'.'This is abstract keyword';}}
$objOne = new CodeOne;
$objOne->show();
?>,
Output
This is abstract keyword

and Keyword:

<?php
$a = 20;
$b = 21;
if($a ==20  and  $b == 21) {
echo 'Output  :  True';}
else
{
  echo 'Output  : False';}
?>
Output
Output : True

array():

<?php
$directions  = array("a" => "sun", "b" => "set", "c" => "in", "d" => "west");
print_r($directions);
?>
Output
Array ( [a] => sun [b] => set [c] => in [d] => west )

as keyword:

<?php
$directions  = array("a" => "east", "b" => "west", "c" => "north", "d" => "south");
foreach($directions as $key=>$value) {
echo '<br>'. $key. '=>'.$value;
}?>
Output
a=>east
b=>west
c=>north
d=>south

break keyword:

<?php
$arr = array("Teacher", "Student", "Table", "Chair");
foreach($arr as $key=>$value) {
if($value  == 'Table') {
break ; }
echo '<br>'.$value;}
?>
Output
Teacher
Student

case Keyword:

<?php
$i = 0;
while($i<4) {
switch($i) {
case 0:
echo "<br>"."Zero";
break;
case 1:
echo "<br>"."One";
break;
case 2:
echo "<br>"."Two";
break;
default:
echo "<br>"."Default";}
$i++;}
?>
Output
Zero
One
Two
Default

catch Keyword:

<?php
function myFunction(){
try {
$num1 = 20;
$num2 = 5;
if($num1 / $num2) {
throw new Exception('Divide By Five');}
} catch (Exception $e) {
die($e->getMessage());
}}
myFunction();
?>
Output
Divide By Five

class Keyword:

<?php
class Class1{
var $str = 'List of all PHP Keywords';
public function displayMethod() {
echo $this->str;
}}
$obj = new Class1;
$obj->displayMethod();
?>
Output
List of all PHP Keywords

const keyword:

The const keyword is applied to determine the name with a value using an assignment operator like here.
const AGE= 39;
There is no $ sign at the opening of a constant name like a normal variable has.

default Keyword:

<?php
$fruits = 'Orange';
switch ($fruits) {
case 'Grapes':
echo "Grapes";
break;
case 'Guava':
echo "Guava";
break;
case 'Strawberry':
echo "Strawberry";
break;
default:
echo "Fruit is not Grapes, Guava or Strawberry ";
break;
}
?>
Output
Fruit is not Grapes, Guava or Strawberry

do Keyword:

<?php
$a = 5;
do { if($a > 5) {
echo 'a is greater than 5';}
else{ echo 'a is less than 5';}}
while ($a < 5);
?>
Output
a is less than 5

die() Keyword

<?php
$conn = mysqli_connect('localhost','root','','dbname');
if(!$conn) {
die("Unable to connect ");}
?>
Output
PHP Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /home/gygrX5/prog.php:2 Stack trace: #0 {main} thrown in /home/gygrX5/prog.php on line 2

echo Keyword

<?php
echo 'This is softcodeon! ';
$name = 'Prince Rauf';
echo 'My name is '. $name;
?>
Output
This is softcodeon! My name is Prince Rauf

else Keyword

<?php
$a = 5;
if ($a > 10) {
echo "a is greater than 20";}
else {
echo "a is not greater than 20";}
?>
Output
a is not greater than 20

elseif keyowrd:

<?php
$a = 10;
if ($a > 20) {
echo "a is greater than 20";
} elseif ($a == 20) {
echo "a is equal to 20";
} else {
echo "a is smaller than 20";
}
?>
Output
a is smaller than 20

empty Keyword:

<?php
$str = 'List of all PHP Keywords';
if(empty($str)) {
echo 'This is empty';} 
else {
echo $str;}
?>
Output
This is Soft CodeOn!

endfor Keyword:

<?php
for($i=0;$i<3;$i++) :
echo "<br/>".$i;
endfor;
?>
Output
0
1
2

endif keyword:

<?php
if ($a > 10):
echo "a is greater than 20";
elseif ($a >20) :
echo "a is equal to 20";
else:
echo "a is not equal to 20";
endif;
?>
Output
a is not equal to 20

endforeach Keyword:

<?php
$arr = array(10,9,8,7,6,5,4,3,2,1);
foreach ($arr as $key=>$value):
echo '<br>'.$value;
endforeach;
?>
Output
10
9
8
7
6
5
4
3
2
1

endswitch Keyowrd:

<?php
$fruits = 'Orange';
switch ($fruits):
case 'Grapes':
echo "Grapes";
break;
case 'Strawberry':
echo "Strawberry";
break;
case 'Guava':
echo "Guava";
break;
default:
echo "Fruit is not Grapes, Strawberry or Guava ";
break;
endswitch;
?>
Output
Fruit is not Grapes, Strawberry or Guava

endwhile keyword:

<?php
$i = 1;
while($i<4):
echo "<br>".$i;
$i++;
endwhile;
?>
Output
1
2
3

eval() keyword:

<?php
$string1 = 'List of all PHP Keywords';
$string2 = 'Prince Rauf';
$string = 'Softcodeon $string1 .  My name is $string2';
echo "<br>".$string;
eval("\$string = \"$string\";");
echo "<br>".$string;
?>
Output
List of all PHP Keywords$string1 . My name is $string2 Softcodeon Softcodeon . My name is Prince Rauf

exit() Keyword:

This keyword when found in a script, ends the execution of the script.

 final Keyword:

<?php
class MyClass {
var $string = 'Learn PHP';
final public function display() {
echo $this->string;
}}
class ExtendClass extends MyClass {
public function display() {
echo 'Softcodeon!';
}}
$obj1 = new ExtendClass;
$obj1->display();
?>
Output
PHP Fatal error: Cannot override final method MyClass::display() in /home/6gQY2c/prog.php on line 10

catch Keyword:

<?php
try {
$num1 = 20;
$num2 = 5;
if($num1 / $num2) {
throw new Exception('Divide By Five');}
} catch (Exception $e) {
echo '<br>'.$e->getMessage();}
?>
Output
Divide By Five

for Keyword:

<?php
for($i=0; $i<10; $i++) {
if($i == 5) {
break;}
echo '<br>'.$i;}
?>
Output
0
1
2
3
4

foreach Keyword:

<?php
$array = array(10,20,30,40,50);
foreach($array as $value) {
echo '<br>'.$value/10;}
?>
Output
1
2
3
4
5

function() Keyword:

<?php
function calSum($a , $b) {
$c = $a + $b;
return $c;
}
$result = calSum(20 , 30);
echo '<br> The sum  :  '.$result;
?>
Output
The sum : 50

global Keyword:

<?php
$a = 5;
$b = 10;
function fun() {
global $a;
global $b;
$sum = $a + $b;
return $sum;
}
$f = fun();
echo 'The sum is '.$f;
?>
Output
The sum is 15

if Keyword:

<?php
$sum = 20;
if($sum == 20) {
echo 'Sum is 20';
} else {
echo 'Sum is not 20';
}
?>
Output
Sum is 20

implements Keyword:

<?php
interface One
{
public function first();
}
class MyClass implements One {
public function first() {
echo 'List of all PHP Keywords, This is the First function';
}
}
$obj = new MyClass;
echo $obj->first();
?>
Output
This is the First function

include Keyword:

file.php

<?php
$a = 'Earth';
$b = 'Sky';
?>

index.php

<?php
include 'file.php';
echo $a . ' is '. $b. ' in Shape';
?>

instanceOf Keyword:

<?php
class MyClass
{
public function MyClassMethod(){
echo 'List of all PHP Keywords!';
}}
class ExtendedClass extends MyClass
{
public function ExtendedClassMethod(){
echo 'Have a Nice Day!';
}}
$obj1 = new ExtendedClass;
var_dump($obj1 instanceOf ExtendedClass);
?>
Output
bool(true)

isset Keyword:

<?php
$stringOne = '';
var_dump(isset($stringOne));
$stringTwo = NULL;
var_dump(isset($stringTwo));
?>
Output
bool(true) bool(false)

list Keyword:

<?php
$names = array('Rauf','Ali','Ahmad');
list($person1, $person2, $person3) = $names;
echo "$person1, $person2 and $person3 are friends";
?>
Output
Rauf, Ali and Ahmad are friends

 new Keyword:

<?php
class MyClass
{
public function score($name, $subject, $marks) {
echo "$name scored $marks marks in $subject";
}}
$obj = new MyClass;
$obj->score('Rauf','Computer',90);
?>
Output
Rauf scored 90 marks in Computer

print Keyword:

<?php
$str = "List of PHP Keyword";
print($str);
$stringOne = "Rauf, ";
$stringTwo = "How are you?";
print "<br>"."Hello $stringOne $stringTwo";
?>
Output
List of PHP Keyword
Hello Rauf, How are you?

 private Keyword:

<?php
class MyClass
{
private $str = 'Private';
function PrivateMethod()
{
echo 'In '. $this->str. ' Method';
}}
$obj = new MyClass();
$obj->PrivateMethod(); 
?>
Output
In Private Method

public Keyword:

<?php
class MyClass
{
public $str = 'Public';
function PublicMethod()
{
echo 'In '. $this->str. ' Method';
}}
$obj = new MyClass();
$obj->PublicMethod(); 
?>
Output
In Public Method

protected Keyword:

<?php
class MyClass
{
protected $str = 'Protected';
function ProtectedMethod()
{
echo 'In '. $this->str. ' Method';
}}
$obj = new MyClass();
$obj->ProtectedMethod(); 
?>
Output
In Protected Method

return Keyword:

<?php
function sum() {
$a = 20;
$b = 30;
$c = $a +$b;
return $c;
}
$result = sum();
echo 'Sum : ' . $result;
?>
Output
Sum : 50

var Keyword:

<?php
class MyClass
{
var $str = 'PHP Programming';
public function displayMsg() {
echo $this->str;
}
}
$obj = new MyClass;
$obj->displayMsg();
?>
Output
PHP Programming

while Keyword:

<?php
$i = 0;
while ($i<5) {
echo '<br>'. $i;
$i++;
}
?>
Output
0
1
2
3
4

Conclusion

In this article, you have to learn about keywords in PHP with outputs. These examples explain the usage of each of the keyword in PHP.

2 thoughts on “List of all PHP Keywords with the Outputs

  1. This is very interesting, You’re a very skilled blogger.
    I have joined your rss feed and look forward List of all PHP Keywords with the Outputs seeking more of your wonderful post.

    Also, I’ve shared your site in my social networks!

Leave a Reply

Your email address will not be published. Required fields are marked *