首頁 > 後端開發 > php教程 > PHP 關鍵字

PHP 關鍵字

WBOY
發布: 2024-08-29 12:33:36
原創
841 人瀏覽過

關鍵字是具有一定意義的字。在PHP語言的常規使用中,這些單字不能用作常數、變數名、方法名、類別名稱等。這些關鍵字在使用時會被PHP自動理解。這些 PHP 關鍵字與變數名稱一起使用時,可能會與實際關鍵字混淆。因此,這些關鍵字不應該用作變數名。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

所有 PHP 關鍵字列表

以下是列表:

PHP 關鍵字

關鍵字:摘要

代碼:

<?php
//example to demonstrate abstract keyword
abstract class Program {
abstract protected function MethodOne();
public function display() {
echo '<br>'.$this->MethodOne();
}
}
class ProgramOne extends Program {
protected function MethodOne() {
echo '<br>'.'In MethodOne';
}
}
$objOne = new ProgramOne;
$objOne->display();
?>,
登入後複製

輸出:

PHP 關鍵字

關鍵字:和

代碼:

<?php
//example to demonstrate and keyword
$a = 10;
$b = 11;
if($a ==10  and  $b == 11) {
echo 'Result :  True';
}
else
{
echo 'Result : False';
}
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:陣列()

代碼:

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

輸出:

PHP 關鍵字

關鍵字:as

代碼:

<?php
//example to demonstrate array keyword
$directions  = array("e" => "east", "w" => "west", "n" => "north", "s" => "south");
foreach($directions as $key=>$value) {
echo '<br>'. $key. '=>'.$value;
}
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:break

代碼:

<?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 '<br>'.$value;
}
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:案例

代碼:

<?php
//example to demonstrate case keyword
$i = 1;
while($i<5) {
switch($i) {
case 1:
echo "<br>"."One";
break;
case 2:
echo "<br>"."Two";
break;
case 3:
echo "<br>"."Three";
break;
default:
echo "<br>"."Default";
}
$i++;
}
?>
登入後複製

輸出: 

PHP 關鍵字

關鍵字:  catch

代碼:

<?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();
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:類別

代碼:

<?php
//example to demonstrate class keyword
class ClassOne{
var $str = 'Hello World';
public function displayMethod() {
echo $this->str;
}
}
$obj = new ClassOne;
$obj->displayMethod();
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:const

const 關鍵字用於使用賦值運算子來定義名稱和值,如下所示

const AGE= 29;

常數名稱的開頭沒有像普通變數的 $ 符號。

關鍵字:預設

代碼:

<?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;
}
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:做

代碼:

<?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);
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:die();

代碼:

<?php
//example to demonstrate die keyword
$conn = mysqli_connect('localhost','root','','dbname');
if(!$conn) {
die("Unable to connect ");
}
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:echo

代碼:

<?php
// example to demonstrate echo keyword
echo 'Hello! ';
$name = 'John Doe';
echo 'My name is '. $name;
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:else

代碼:

<?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";
}
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:elseif

代碼:

<?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";
}
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:空

代碼:

<?php
// example to demonstrate empty keyword
$str = 'Hello World!';
if(empty($str)) {
echo 'Variable is empty';
} else {
echo $str;
}
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:endfor

代碼:

<?php
// example to demonstrate endfor keyword
for($i=0;$i<5;$i++) :
echo "<br/>".$i;
endfor;
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:endif

代碼:

<?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;
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:endforeach

 代碼:

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

輸出:

PHP 關鍵字

關鍵字:endswitch

代碼:

<?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;
?>
登入後複製

輸出:

PHP 關鍵字

關鍵字:endwhile

代碼:

<?php
// example to demonstrate endwhile keyword
$i = 0;
while($i<5):
echo "<br>".$i;
$i++;
endwhile;
?>
登入後複製

輸出:

PHP 關鍵字

Keyword: eval()

Code:

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

Output:

PHP 關鍵字

Keyword: exit()

This keyword when encountered in a script, terminates the execution of the script.

Keyword: extends()

Code:

<?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();
?>
登入後複製

Output:

PHP 關鍵字

Keyword: final

Code:

<?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();
?>
登入後複製

Output :

PHP 關鍵字

Keyword: catch

Code:

<?php
//example to demonstrate catch keyword
try {
$num1 = 10;
$num2 = 0;
if($num1 / $num2) {
throw new Exception('Divide By Zero');
}
} catch (Exception $e) {
echo '<br>'.$e->getMessage();
}
?>
登入後複製

Output:

PHP 關鍵字

Keyword: for

Code:

<?php
// example to demonstrate for keyword
for($i=0; $i<10; $i++) {
if($i == 5) {
break;
}
echo '<br>'.$i;
}
?>
登入後複製

Output :

PHP 關鍵字

Keyword: foreach

Code:

<?php
// example to demonstrate foreach keyword
$array = array(10,20,30,40,50);
foreach($array as $value) {
echo '<br>'.$value/10;
}
?>
登入後複製

Output:

PHP 關鍵字

Keyword: function()

Code:

<?php
function calSum($a , $b) {
$c = $a + $b;
return $c;
}
$result = calSum(10 , 20);
echo '<br> The sum  :  '.$result;
?>
登入後複製

Output:

PHP 關鍵字

Keyword 34: global

Code:

<?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;
?>
登入後複製

Output:

PHP 關鍵字

Keyword: if

Code:

<?php
// example to demonstrate if keyword
$sum = 10;
if($sum == 10) {
echo 'Sum is 10';
} else {
echo 'Sum is not 10';
}
?>
登入後複製

Output:

PHP 關鍵字

Keyword: implements

Code:

<?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();
?>
登入後複製

Output :

PHP 關鍵字

Keyword: include

Code:

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';
?>
登入後複製

Keyword  : include_once

Code:

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';
?>
登入後複製

Output:

PHP 關鍵字

Keyword: instanceOf

Code:

<?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);
?>
登入後複製

Output:

PHP 關鍵字

Keyword: Interface

Code:

<?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 '<br> This is the one function';
}
public function two() {
echo '<br> This is the two function';
}
}
$obj = new MainClass;
echo $obj->one();
echo $obj->two();
?>
登入後複製

Output:

PHP 關鍵字

Keyword: isset

Code:

<?php
//example to demonstrate isset keyword
$stringOne = '';
var_dump(isset($stringOne));
$stringTwo = NULL;
var_dump(isset($stringTwo));
?>
登入後複製

Output :

PHP 關鍵字

Keyword: list

Code:

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

Output:

PHP 關鍵字

Keyword: new

Code:

<?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);
?>
登入後複製

Output:

PHP 關鍵字

Keyword: or

Code:

<?php
//example to demonstrate or keyword
$a = 10;
$b = 11;
if($a ==10  or $b == 12) {
echo 'Result :  True';
}
else
{
echo 'Result : False';
}
?>
登入後複製

Output :

PHP 關鍵字

Keyword: print

Code:

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

Output :

PHP 關鍵字

Keyword: private

Code:

<?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
?>
登入後複製

Output:

PHP 關鍵字

Keyword: public

Code:

<?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
?>
登入後複製

Output:

PHP 關鍵字

Keyword: protected

Code:

<?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
?>
登入後複製

 Output:

PHP 關鍵字

Keyword: return

Code:

<?php
//example to demonstrate return keyword
function sum() {
$a = 10;
$b = 20;
$c = $a +$b;
return $c;
}
$result = sum();
echo 'Sum : ' . $result;
?>
登入後複製

Output:

PHP 關鍵字

Keyword: switch

Code:

<?php
//example to demonstrate switch keyword
$i= 3;
switch($i) {
case 1:
echo "<br>"."One";
break;
case 2:
echo "<br>"."Two";
break;
case 3:
echo "<br>"."Three";
break;
default:
echo "<br>"."Default";
}
?>
登入後複製

Output:

PHP 關鍵字

Keyword: throw

Code:

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

Output:

PHP 關鍵字

Keyword: Try

Code:

<?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 '<br>'.$e->getMessage();
}
?>
登入後複製

Output:

PHP 關鍵字

Keyword: unset

Code:

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

Output:

PHP 關鍵字

Keyword: var

Code:

<?php
//example to demonstrate var keyword
class MainClass
{
var $str = 'PHP Programming';
public function displayMsg() {
echo $this->str;
}
}
$obj = new MainClass;
$obj->displayMsg();
?>
登入後複製

Output:

PHP 關鍵字

Keyword: while

Code: 

<?php
//example to demonstrate while keyword
$i = 0;
while ($i<10) {
echo '<br>'. $i;
$i++;
}
?>
登入後複製

 Output:

PHP 關鍵字

Conclusion

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

以上是PHP 關鍵字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板