Introduzione delle parole chiave PHP
Le parole chiave sono le parole che hanno un significato. Nell'uso regolare del linguaggio PHP, queste parole non possono essere usate come una costante, un nome di variabile, un nome di metodo, un nome di classe, ecc. Queste parole chiave quando utilizzate sono comprese automaticamente da PHP. Queste parole chiave PHP, se utilizzate con nomi di variabili, possono essere confuse con le parole chiave effettive. Pertanto, queste parole chiave non devono essere utilizzate come nomi di variabili.
Elenco di tutte le parole chiave PHP
Parola chiave: astratto
Codice:
<_?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();
?>,
Produzione:
Parola chiave: e
Codice :
<_?php//example to demonstrate and keyword
$a = 10;
$b = 11;
if($a ==10 and $b == 11) (
echo 'Result : True';
)
else
(
echo 'Result : False';
)
?>
Produzione:
Parola chiave: array ()
Codice:
<_?php//example to demonstrate array keyword
$directions = array("e" => "east", "w" => "west", "n" => "north", "s" => "south");
print_r($directions);
?>
Produzione:
Parola chiave: come
Codice:
<_?php//example to demonstrate array keyword
$directions = array("e" => "east", "w" => "west", "n" => "north", "s" => "south");
foreach($directions as $key=>$value) (
echo '
'. $key. '=>'.$value;
)
?>
Produzione:
Parola chiave: pausa
Codice :
<_?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;
)
?>
Produzione:
Parola chiave: caso
Codice:
<_?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++;
)
?>
Produzione:
Parola chiave: cattura
Codice:
<_?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();
?>
Produzione:
Parola chiave: classe
Codice:
<_?php//example to demonstrate class keyword
class ClassOne(
var $str = 'Hello World';
public function displayMethod() (
echo $this->str;
)
)
$obj = new ClassOne;
$obj->displayMethod();
?>
Produzione:
Parola chiave: const
la parola chiave const viene utilizzata per definire il nome con un valore utilizzando l'operatore di assegnazione come di seguito
const AGE = 29;
Non c'è un segno $ all'inizio di un nome costante come ha una normale variabile.
Parola chiave: impostazione predefinita
Codice:
<_?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;
)
?>
Produzione :
Parola chiave: do
Codice:
<_?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);
?>
Produzione:
Parola chiave: die ();
Codice :
<_?php//example to demonstrate die keyword
$conn = mysqli_connect('localhost', 'root', '', 'dbname');
if(!$conn) (
die("Unable to connect ");
)
?>
Produzione:
Parola chiave: eco
Codice:
<_?php// example to demonstrate echo keyword
echo 'Hello! ';
$name = 'John Doe';
echo 'My name is '. $name;
?>
Produzione:
Parola chiave: altro
Codice:
<_?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";
)
?>
Produzione:
Parola chiave: elseif
Codice:
<_?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";
)
?>
Produzione :
Parola chiave: vuota
Codice:
<_?php// example to demonstrate empty keyword
$str = 'Hello World!';
if(empty($str)) (
echo 'Variable is empty';
) else (
echo $str;
)
?>
Produzione:
Parola chiave: endfor
Codice:
<_?php// example to demonstrate endfor keyword
for($i=0;$i<5;$i++) :
echo "
".$i;
endfor;
?>
Produzione:
Parola chiave: endif
Codice :
<_?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;
?>
Produzione:
Parola chiave: endforeach
Codice:
<_?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;
?>
Produzione:
Parola chiave: interruttore finale
Codice:
<_?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;
?>
Produzione:
Parola chiave: intanto
Codice:
<_?php// example to demonstrate endwhile keyword
$i = 0;
while($i<5):
echo "
".$i;
$i++;
endwhile;
?>
Produzione:
Parola chiave: eval ()
Codice:
<_?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;
?>
Produzione:
Parola chiave: exit ()
Questa parola chiave quando incontrata in uno script, termina l'esecuzione dello script.
Parola chiave: extends ()
Codice:
<_?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();
?>
Produzione:
Parola chiave: finale
Codice:
<_?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();
?>
Produzione :
Parola chiave: cattura
Codice:
<_?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();
)
?>
Produzione:
Parola chiave: per
Codice:
<_?php// example to demonstrate for keyword
for($i=0; $i<10; $i++) (
if($i == 5) (
break;
)
echo '
'.$i;
)
?>
Produzione :
Parola chiave: foreach
Codice:
<_?php// example to demonstrate foreach keyword
$array = array(10, 20, 30, 40, 50);
foreach($array as $value) (
echo '
'.$value/10;
)
?>
Produzione:
Parola chiave: function ()
Codice:
<_?phpfunction calSum($a, $b) (
$c = $a + $b;
return $c;
)
$result = calSum(10, 20);
echo '
The sum : '.$result;
?>
Produzione:
Parola chiave 34: globale
Codice:
<_?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;
?>
Produzione:
Parola chiave: if
Codice:
<_?php// example to demonstrate if keyword
$sum = 10;
if($sum == 10) (
echo 'Sum is 10';
) else (
echo 'Sum is not 10';
)
?>
Produzione:
Parola chiave: attrezzi
Codice:
<_?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();
?>
Produzione :
Parola chiave: include
Codice:
file.php
<_?php//example to demonstrate include keyword
$a = 'The Earth';
$b = 'Round';
?>
index.php
<_?phpinclude 'file.php';
echo $a . ' is '. $b. ' in Shape';
?>
Parola chiave: include_once
Codice:
file.php
<_?php//example to demonstrate include_once keyword
$a = 'The Earth';
$b = 'Round';
?>
index.php
<_?phpInclude_once 'file.php';
echo $a . ' is '. $b. ' in Shape';
?>
Produzione:
Parola chiave: instanceOf
Codice:
<_?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);
?>
Produzione:
Parola chiave: interfaccia
Codice:
<_?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();
?>
Produzione:
Parola chiave: isset
Codice:
<_?php//example to demonstrate isset keyword
$stringOne = '';
var_dump(isset($stringOne));
$stringTwo = NULL;
var_dump(isset($stringTwo));
?>
Produzione :
Parola chiave: elenco
Codice:
<_?php//example to demonstrate list keyword
$names = array('Ram', 'Mohan', 'Raghav');
list($person1, $person2, $person3) = $names;
echo "$person1, $person2 and $person3 are friends";
?>
Produzione:
Parola chiave: nuova
Codice:
<_?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);
?>
Produzione:
Parola chiave: o
Codice:
<_?php//example to demonstrate or keyword
$a = 10;
$b = 11;
if($a ==10 or $b == 12) (
echo 'Result : True';
)
else
(
echo 'Result : False';
)
?>
Produzione :
Parola chiave: stampa
Codice:
<_?php//example to demonstrate print keyword
$str = "PHP Programming";
print($str);
$stringOne = "Shyam, ";
$stringTwo = "How are you?";
print "
"."Hello $stringOne $stringTwo";
?>
Produzione :
Parola chiave: privato
Codice:
<_?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
?>
Produzione:
Parola chiave: pubblica
Codice:
<_?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
?>
Produzione:
Parola chiave: protetta
Codice:
<_?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
?>
Produzione:
Parola chiave: ritorno
Codice:
<_?php//example to demonstrate return keyword
function sum() (
$a = 10;
$b = 20;
$c = $a +$b;
return $c;
)
$result = sum();
echo 'Sum : ' . $result;
?>
Produzione:
Parola chiave: interruttore
Codice:
<_?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";
)
?>
Produzione:
Parola chiave: lanciare
Codice:
<_?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);
?>
Produzione:
Parola chiave: prova
Codice:
<_?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();
)
?>
Produzione:
Parola chiave: non impostata
Codice:
<_?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
?>
Produzione:
Parola chiave: var
Codice:
<_?php//example to demonstrate var keyword
class MainClass
(
var $str = 'PHP Programming';
public function displayMsg() (
echo $this->str;
)
)
$obj = new MainClass;
$obj->displayMsg();
?>
Produzione:
Parola chiave: while
Codice:
<_?php//example to demonstrate while keyword
$i = 0;
while ($i<10) (
echo '
'. $i;
$i++;
)
?>
Produzione:
Conclusione
In questo articolo imparerai le parole chiave in PHP con esempi. Questi esempi spiegano l'uso di ciascuna parola chiave in PHP.
Articoli consigliati
Questa è una guida alle parole chiave di PHP. Qui discutiamo la Introduzione alle parole chiave PHP Elenco di tutte le parole chiave PHP insieme a parole chiave e risultati. Puoi anche consultare i nostri altri articoli suggeriti per saperne di più–
- Compilatore PHP
- Incapsulamento in C
- Ordinamento in C
- Domande chiave per l'intervista a PHP
- Parole chiave
- Parole chiave C #
- Incapsulamento in JavaScript
- Parola chiave finale in Java
- Lancia la parola chiave in Java