Uvedba PHP ključnih besed

Ključne besede so besede, ki imajo določen pomen. Pri redni uporabi jezika PHP teh besed ni mogoče uporabiti kot konstanto, ime spremenljivke, ime metode, ime razreda itd. PHP te ključne besede, ko jih uporablja, samodejno razume. Te ključne besede PHP, če se uporabljajo z imeni spremenljivk, je mogoče zamenjati z dejanskimi ključnimi besedami. Zato se te ključne besede ne smejo uporabljati kot imena spremenljivk.

Seznam vseh ključnih besed PHP

Ključna beseda: izvleček

Koda:

<_?php
//example to demonstrate abstract keyword
abstract class Program (
abstract protected function MethodOne();
public function display() (
echo '
'.$this->MethodOne();
)
)
class ProgramOne extends Program (
protected function MethodOne() (
echo '
'.'In MethodOne';
)
)
$objOne = new ProgramOne;
$objOne->display();
?>,

Izhod:

Ključna beseda: in

Koda :

<_?php
//example to demonstrate and keyword
$a = 10;
$b = 11;
if($a ==10 and $b == 11) (
echo 'Result : True';
)
else
(
echo 'Result : False';
)
?>

Izhod:

Ključna beseda: array ()

Koda:

<_?php
//example to demonstrate array keyword
$directions = array("e" => "east", "w" => "west", "n" => "north", "s" => "south");
print_r($directions);
?>

Izhod:

Ključna beseda: as

Koda:

<_?php
//example to demonstrate array keyword
$directions = array("e" => "east", "w" => "west", "n" => "north", "s" => "south");
foreach($directions as $key=>$value) (
echo '
'. $key. '=>'.$value;
)
?>

Izhod:

Ključna beseda: odmor

Koda :

<_?php
//use of break keyword without optional argument
$arr = array("water", "sky", "try", "sand");
foreach($arr as $key=>$value) (
if($value == 'try') (
break ; // can use break 1 also
)
echo '
'.$value;
)
?>

Izhod:

Ključna beseda: primer

Koda:

<_?php
//example to demonstrate case keyword
$i = 1;
while($i<5) (
switch($i) (
case 1:
echo "
"."One";
break;
case 2:
echo "
"."Two";
break;
case 3:
echo "
"."Three";
break;
default:
echo "
"."Default";
)
$i++;
)
?>

Izhod:

Ključna beseda: ulov

Koda:

<_?php
//example to demonstrate catch keyword
function Operation()
(
try (
$num1 = 10;
$num2 = 0;
if($num1 / $num2) (
throw new Exception('Divide By Zero');
)
) catch (Exception $e) (
die($e->getMessage());
)
)
Operation();
?>

Izhod:

Ključna beseda: razred

Koda:

<_?php
//example to demonstrate class keyword
class ClassOne(
var $str = 'Hello World';
public function displayMethod() (
echo $this->str;
)
)
$obj = new ClassOne;
$obj->displayMethod();
?>

Izhod:

Ključna beseda: const

Ključna beseda const se uporablja za določitev imena z vrednostjo z uporabo operaterja dodeljevanja, kot je spodaj

const AGE = 29;

Na začetku stalnega imena ni znaka $, kot ga ima običajna spremenljivka.

Ključna beseda: privzeto

Koda:

<_?php
// example to demonstrate default keyword
$fruits = 'Cherries';
switch ($fruits) (
case 'Apple':
echo "Apple";
break;
case 'Banana':
echo "Banana";
break;
case 'Papaya':
echo "Papaya";
break;
default:
echo "Fruit is not Apple, Banana or Papaya ";
break;
)
?>

Izhod:

Ključna beseda: naredi

Koda:

<_?php
// example to demonstrate do keyword
$x = 2;
do (
if($x > 2)
(
echo 'x is greater than 2';
)
else(
echo 'x is less than 2';
)
) while ($x < 2);
?>

Izhod:

Ključna beseda: die ();

Koda :

<_?php
//example to demonstrate die keyword
$conn = mysqli_connect('localhost', 'root', '', 'dbname');
if(!$conn) (
die("Unable to connect ");
)
?>

Izhod:

Ključna beseda: odmev

Koda:

<_?php
// example to demonstrate echo keyword
echo 'Hello! ';
$name = 'John Doe';
echo 'My name is '. $name;
?>

Izhod:

Ključna beseda: drugo

Koda:

<_?php
// example to demonstrate else keyword
$a = 10;
if ($a > 5) (
echo "a is greater than 10";
) else (
echo "a is not greater than 10";
)
?>

Izhod:

Ključna beseda: elseif

Koda:

<_?php
// example to demonstrate elseif keyword
$a = 10;
if ($a > 10) (
echo "a is greater than 10";
) elseif ($a == 10) (
echo "a is equal to 10";
) else (
echo "a is smaller than 10";
)
?>

Izhod:

Ključna beseda: prazna

Koda:

<_?php
// example to demonstrate empty keyword
$str = 'Hello World!';
if(empty($str)) (
echo 'Variable is empty';
) else (
echo $str;
)
?>

Izhod:

Ključna beseda: endfor

Koda:

<_?php
// example to demonstrate endfor keyword
for($i=0;$i<5;$i++) :
echo "
".$i;
endfor;
?>

Izhod:

Ključna beseda: endif

Koda :

<_?php
// example to demonstrate endif keyword
if ($a > 10):
echo "a is greater than 10";
elseif ($a >10) :
echo "a is equal to 10";
else:
echo "a is not equal to 10";
endif;
?>

Izhod:

Ključna beseda: endforeach

Koda:

<_?php
// example to demonstrate endforeach keyword
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach ($arr as $key=>$value):
echo '
'.$value;
endforeach;
?>

Izhod:

Ključna beseda: endwitch

Koda:

<_?php
// example to demonstrate endswitch keyword
$fruits = 'Cherries';
switch ($fruits):
case 'Apple':
echo "Apple";
break;
case 'Banana':
echo "Banana";
break;
case 'Papaya':
echo "Papaya";
break;
default:
echo "Fruit is not Apple, Banana or Papaya ";
break;
endswitch;
?>

Izhod:

Ključna beseda: medtem

Koda:

<_?php
// example to demonstrate endwhile keyword
$i = 0;
while($i<5):
echo "
".$i;
$i++;
endwhile;
?>

Izhod:

Ključna beseda: eval ()

Koda:

<_?php
//example to demonstrate eval keyword
$string1 = 'World';
$string2 = 'John Doe';
$string = 'Hello $string1 . My name is $string2';
echo "
".$string;
eval("\$string = \"$string\";");
echo "
".$string;
?>

Izhod:

Ključna beseda: exit ()

Ta ključna beseda, ko se sreča v skriptu, zaključi izvajanje skripta.

Ključna beseda: extends ()

Koda:

<_?php
//example to demonstrate extends keyword
class ParentClass (
var $string = 'PHP';
public function display() (
echo $this->string;
)
)
class ExtendClass extends ParentClass (
public function display() (
echo 'Hello World!';
)
)
$obj1 = new ExtendClass;
$obj1->display();
?>

Izhod:

Ključna beseda: končno

Koda:

<_?php
//example to demonstrate final keyword
class ParentClass (
var $string = 'PHP';
final public function display() (
echo $this->string;
)
)
class ExtendClass extends ParentClass (
public function display() (
echo 'Hello World!';
)
)
$obj1 = new ExtendClass;
$obj1->display();
?>

Izhod:

Ključna beseda: ulov

Koda:

<_?php
//example to demonstrate catch keyword
try (
$num1 = 10;
$num2 = 0;
if($num1 / $num2) (
throw new Exception('Divide By Zero');
)
) catch (Exception $e) (
echo '
'.$e->getMessage();
)
?>

Izhod:

Ključna beseda: za

Koda:

<_?php
// example to demonstrate for keyword
for($i=0; $i<10; $i++) (
if($i == 5) (
break;
)
echo '
'.$i;
)
?>

Izhod:

Ključna beseda: foreach

Koda:

<_?php
// example to demonstrate foreach keyword
$array = array(10, 20, 30, 40, 50);
foreach($array as $value) (
echo '
'.$value/10;
)
?>

Izhod:

Ključna beseda: function ()

Koda:

<_?php
function calSum($a, $b) (
$c = $a + $b;
return $c;
)
$result = calSum(10, 20);
echo '
The sum : '.$result;
?>

Izhod:

Ključna beseda 34: globalna

Koda:

<_?php
//example to demonstrate global keyword
$a = 10;
$b = 20;
function fun() (
global $a;
global $b;
$result = $a + $b;
return $result;
)
$f = fun();
echo 'The result is '.$f;
?>

Izhod:

Ključna beseda: če

Koda:

<_?php
// example to demonstrate if keyword
$sum = 10;
if($sum == 10) (
echo 'Sum is 10';
) else (
echo 'Sum is not 10';
)
?>

Izhod:

Ključna beseda: tools

Koda:

<_?php
//example to demonstrate interface keyword
interface One
(
public function first();
)
class MainClass implements One (
public function first() (
echo 'This is the First function';
)
)
$obj = new MainClass;
echo $obj->first();
?>

Izhod:

Ključna beseda: vključuje

Koda:

file.php

<_?php
//example to demonstrate include keyword
$a = 'The Earth';
$b = 'Round';
?>

index.php

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

Ključna beseda: include_once

Koda:

file.php

<_?php
//example to demonstrate include_once keyword
$a = 'The Earth';
$b = 'Round';
?>

index.php

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

Izhod:

Ključna beseda: instanceOf

Koda:

<_?php
//example to demonstrate instanceOf keyword
class MainClass
(
public function MainCLassMethod()(
echo 'Hello World!';
)
)
class ExtendedClass extends MainClass
(
public function ExtendedClassMethod()(
echo 'Have a Nice Day!';
)
)
$obj1 = new ExtendedClass;
var_dump($obj1 instanceOf ExtendedClass);
?>

Izhod:

Ključna beseda: Vmesnik

Koda:

<_?php
//example to demonstrate interface keyword
interface One
(
public function one();
)
interface Two
(
public function two();
)
class MainClass implements One, Two (
public function one() (
echo '
This is the one function';
)
public function two() (
echo '
This is the two function';
)
)
$obj = new MainClass;
echo $obj->one();
echo $obj->two();
?>

Izhod:

Ključna beseda: isset

Koda:

<_?php
//example to demonstrate isset keyword
$stringOne = '';
var_dump(isset($stringOne));
$stringTwo = NULL;
var_dump(isset($stringTwo));
?>

Izhod:

Ključna beseda: seznam

Koda:

<_?php
//example to demonstrate list keyword
$names = array('Ram', 'Mohan', 'Raghav');
list($person1, $person2, $person3) = $names;
echo "$person1, $person2 and $person3 are friends";
?>

Izhod:

Ključna beseda: novo

Koda:

<_?php
//example to demonstrate new keyword
class Student
(
public function score($name, $subject, $marks) (
echo "$name scored $marks marks in $subject";
)
)
$obj = new Student;
$obj->score('Sunil', 'Maths', 90);
?>

Izhod:

Ključna beseda: ali

Koda:

<_?php
//example to demonstrate or keyword
$a = 10;
$b = 11;
if($a ==10 or $b == 12) (
echo 'Result : True';
)
else
(
echo 'Result : False';
)
?>

Izhod:

Ključna beseda: tisk

Koda:

<_?php
//example to demonstrate print keyword
$str = "PHP Programming";
print($str);
$stringOne = "Shyam, ";
$stringTwo = "How are you?";
print "
"."Hello $stringOne $stringTwo";
?>

Izhod:

Ključna beseda: zasebno

Koda:

<_?php
//example to demonstrate private keyword
class MainClass
(
private $str = 'Private';
function PrivateMethod()
(
echo 'In '. $this->str. ' Method';
)
)
$obj = new MainClass();
$obj->PrivateMethod(); //Shows Private Method
?>

Izhod:

Ključna beseda: javna

Koda:

<_?php
//example to demonstrate public keyword
class MainClass
(
public $str = 'Public';
function PublicMethod()
(
echo 'In '. $this->str. ' Method';
)
)
$obj = new MainClass();
$obj->PublicMethod(); //Shows Public Method
?>

Izhod:

Ključna beseda: zaščitena

Koda:

<_?php
//example to demonstrate protected keyword
class MainClass
(
protected $str = 'Protected';
function ProtectedMethod()
(
echo 'In '. $this->str. ' Method';
)
)
$obj = new MainClass();
$obj->ProtectedMethod(); //Shows Protected Method
?>

Izhod:

Ključna beseda: vrnitev

Koda:

<_?php
//example to demonstrate return keyword
function sum() (
$a = 10;
$b = 20;
$c = $a +$b;
return $c;
)
$result = sum();
echo 'Sum : ' . $result;
?>

Izhod:

Ključna beseda: preklop

Koda:

<_?php
//example to demonstrate switch keyword
$i= 3;
switch($i) (
case 1:
echo "
"."One";
break;
case 2:
echo "
"."Two";
break;
case 3:
echo "
"."Three";
break;
default:
echo "
"."Default";
)
?>

Izhod:

Ključna beseda: metati

Koda:

<_?php
//example to demonstrate throw keyword
function division($x, $y) (
try (
if($y == 0) (
throw new Exception('Divide By Zero');
)
)
catch (Exception $e) (
echo '
'.$e->getMessage();
)
)
division(10, 0);
?>

Izhod:

Ključna beseda: Poskusite

Koda:

<_?php
//example to demonstrate try keyword
try(
$arr = array();
$arr_length = count($arr);
if($arr_length == 0) (
throw new Exception('Error : Empty Array!');
)
else (
echo 'Array found';
print_r($arr);
)
)
catch(Exception $e) (
echo '
'.$e->getMessage();
)
?>

Izhod:

Ključna beseda: unset

Koda:

<_?php
//example to demonstrate unset keyword
echo 'Hello World!'.'
';
$a = 10;
echo $a;
unset($a);
// echo $a; //this line when uncommented shows error : Undefined variable, as the variable is unset
?>

Izhod:

Ključna beseda: var

Koda:

<_?php
//example to demonstrate var keyword
class MainClass
(
var $str = 'PHP Programming';
public function displayMsg() (
echo $this->str;
)
)
$obj = new MainClass;
$obj->displayMsg();
?>

Izhod:

Ključna beseda: medtem

Koda:

<_?php
//example to demonstrate while keyword
$i = 0;
while ($i<10) (
echo '
'. $i;
$i++;
)
?>

Izhod:

Zaključek

V tem članku boste izvedeli o ključnih besedah ​​v PHP s primeri. Ti primeri pojasnjujejo uporabo vsake ključne besede v PHP-ju.

Priporočeni članki

To je vodnik za ključne besede PHP. Tukaj razpravljamo o Uvodu v PHP Ključne besede Seznam vseh ključnih besed PHP, skupaj s ključnimi besedami in rezultati. Obiščite lahko tudi druge naše predlagane članke, če želite izvedeti več -

  1. PHP prevajalnik
  2. Kapsulacija v C
  3. Razvrščanje v C
  4. Temeljna vprašanja o intervjuju za PHP
  5. C Ključne besede
  6. C # ključne besede
  7. Kapsulacija v JavaScript
  8. Končna ključna beseda v Javi
  9. Vrzi ključno besedo v Javo