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 |
break | callable | case | catch |
class | clone | cost | continue |
declare | default | die() | do |
echo | else | elseif | empty |
enddeclare | endfor | endforeach | endif |
endswitch | endwhile | eval() | exit() |
extends | final | finally | for |
foreach | function | global | goto |
if | implements | include | include_once |
instanceof | insteadof | interface | isset() |
list() | namespace() | new | or |
private | protected | public | |
require | require_once | return | static |
switch | throw | trait | try |
unset() | use | var | while |
yield | yield_from | xor |
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(); ?>,
and Keyword:
<?php $a = 20; $b = 21; if($a ==20 and $b == 21) { echo 'Output : True';} else { echo 'Output : False';} ?>
array():
<?php $directions = array("a" => "sun", "b" => "set", "c" => "in", "d" => "west"); print_r($directions); ?>
as keyword:
<?php $directions = array("a" => "east", "b" => "west", "c" => "north", "d" => "south"); foreach($directions as $key=>$value) { echo '<br>'. $key. '=>'.$value; }?>
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;} ?>
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++;} ?>
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(); ?>
class Keyword:
<?php class Class1{ var $str = 'List of all PHP Keywords'; public function displayMethod() { echo $this->str; }} $obj = new Class1; $obj->displayMethod(); ?>
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; } ?>
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); ?>
die() Keyword
<?php $conn = mysqli_connect('localhost','root','','dbname'); if(!$conn) { die("Unable to connect ");} ?>
echo Keyword
<?php echo 'This is softcodeon! '; $name = 'Prince Rauf'; echo 'My name is '. $name; ?>
else Keyword
<?php $a = 5; if ($a > 10) { echo "a is greater than 20";} else { echo "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"; } ?>
empty Keyword:
<?php $str = 'List of all PHP Keywords'; if(empty($str)) { echo 'This is empty';} else { echo $str;} ?>
endfor Keyword:
<?php for($i=0;$i<3;$i++) : echo "<br/>".$i; endfor; ?>
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; ?>
endforeach Keyword:
<?php $arr = array(10,9,8,7,6,5,4,3,2,1); foreach ($arr as $key=>$value): echo '<br>'.$value; endforeach; ?>
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; ?>
endwhile keyword:
<?php $i = 1; while($i<4): echo "<br>".$i; $i++; endwhile; ?>
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; ?>
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(); ?>
catch Keyword:
<?php try { $num1 = 20; $num2 = 5; if($num1 / $num2) { throw new Exception('Divide By Five');} } catch (Exception $e) { echo '<br>'.$e->getMessage();} ?>
for Keyword:
<?php for($i=0; $i<10; $i++) { if($i == 5) { break;} echo '<br>'.$i;} ?>
1
2
3
4
foreach Keyword:
<?php $array = array(10,20,30,40,50); foreach($array as $value) { echo '<br>'.$value/10;} ?>
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; ?>
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; ?>
if Keyword:
<?php $sum = 20; if($sum == 20) { echo 'Sum is 20'; } else { echo 'Sum is not 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(); ?>
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); ?>
isset Keyword:
<?php $stringOne = ''; var_dump(isset($stringOne)); $stringTwo = NULL; var_dump(isset($stringTwo)); ?>
list Keyword:
<?php $names = array('Rauf','Ali','Ahmad'); list($person1, $person2, $person3) = $names; echo "$person1, $person2 and $person3 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); ?>
print Keyword:
<?php $str = "List of PHP Keyword"; print($str); $stringOne = "Rauf, "; $stringTwo = "How are you?"; print "<br>"."Hello $stringOne $stringTwo"; ?>
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(); ?>
public Keyword:
<?php class MyClass { public $str = 'Public'; function PublicMethod() { echo 'In '. $this->str. ' Method'; }} $obj = new MyClass(); $obj->PublicMethod(); ?>
protected Keyword:
<?php class MyClass { protected $str = 'Protected'; function ProtectedMethod() { echo 'In '. $this->str. ' Method'; }} $obj = new MyClass(); $obj->ProtectedMethod(); ?>
return Keyword:
<?php function sum() { $a = 20; $b = 30; $c = $a +$b; return $c; } $result = sum(); echo 'Sum : ' . $result; ?>
var Keyword:
<?php class MyClass { var $str = 'PHP Programming'; public function displayMsg() { echo $this->str; } } $obj = new MyClass; $obj->displayMsg(); ?>
while Keyword:
<?php $i = 0; while ($i<5) { echo '<br>'. $i; $i++; } ?>
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.
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!
Some truly interesting details you have written.Aided me a lot, just what I was searching for.