首頁  >  文章  >  後端開發  >  PHP指令

PHP指令

王林
王林原創
2024-08-29 12:34:20329瀏覽

PHP 代表超文本處理器,被設計為用於開發 Web 應用程式的伺服器端腳本語言。 PHP 程式碼主要與 HTML 語法結合或嵌入,但它可以用於 Web 應用程式的任何模板系統或可用的 Web 框架。

基本 PHP 指令

有許多 PHP 命令可用於各種環境,特別是用於準備一個 Web 應用程式或使用 HTML 語法嵌入整個伺服器端程式碼庫,並且對於普通開發人員來說非常容易學習。下面提到了一些基本的 PHP 指令:

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

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

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

1. PHP 變數

  • 變數類型:變數在任何一種程式語言中始終扮演著重要的角色。 PHP 也使用變數宣告來賦值。 PHP變數的主要特點之一是不需要宣告變數的類型。由於 PHP 是一種每週類型的語言,因此根據分配的值來考慮類型來聲明變數。 PHP 通常接受任何變數的各種類型,如字串、整數、浮點數、布林值、物件、資源、陣列或 NULL。
  • 變數名稱:PHP 中的變數名稱總是以 $ 開頭,後面跟著任何文字或特定字母和 _。 PHP 變數名稱區分大小寫,因此任何同名的大寫字母變數都應被視為新變數。
  • 變數的範圍:最大變數位於局部範圍內。函數內部宣告的變數在函數外不可用,同樣,函數外部宣告的變數在函數內部不可用。在 PHP 中可以宣告一個全域變量,在這種情況下,需要專門將該變數宣告為全域變量,或透過全域數組存取該變數。

2. PHP 運算子

  • 賦值運算子:PHP 通常使用一個常見的賦值運算符,它等於(‘=’)。等號左邊是變數名,右邊是賦值。
  • 算術運算運算子:以下運算子用於在 PHP 中執行算術運算。運算子有‘+’、‘-’、‘*’、‘/’、‘%’、‘++’、‘–’。
  • 組合運算子:它基本上是算術運算子和賦值運算子的組合。組合運算子有‘+=’、‘-=’、‘*=’、‘/=’、‘%=’。
  • 比較運算子:比較運算子有 '=='、'!='、'>'、'>='、'
  • 邏輯表達式的運算子:PHP 中的邏輯運算子有‘||’、‘&&’、‘and’、‘or’、‘xor’、‘!’。

3. PHP 如果否則

  • 條件判斷:對於程式邏輯中的任何類型的條件要求,PHP 像任何其他程式語言一樣使用「if else」功能。 PHP 的「IF ELSE」語句的基本語法是:
IF [SPECIFIC CONDITION]{
[CODE]
}ELSE IF [SPECIFIC CONDITION 2]{
[CODE]
}ELSE {
[CODE]
}

4。 PHP 開關

PHP 也使用 switch case,就像其他程式語言一樣,以避免多個「IF ELSE」的巢狀定義。考慮多種情況的切換情況,定義預設值是可選的。定義 switch case 的程式碼結構如下:

SWITCH($var){
CASE 'val 1'
[CODE]
Break;
CASE 'val 2'
[CODE]
Break;
CASE 'val 3'
[CODE]
Break;
DEFAULT
[CODE]
}

5. PHP 循環

  • While Loop:在 PHP 中,可以執行 while 循環,直到提及表達式被視為 true。
WHILE [condition or expression]{
[CODE]
}
  • FOR 迴圈:For 迴圈用於執行相同的程式碼指定次數。
For(exp 1, exp 2, exp 3){
[CODE]
}
  • Do While Loop:與 while 迴圈類似,程式碼會一直執行,直到 while 運算式中得到 true 值。與 while 的主要區別是,do 裡面提到的程式碼無論表達式為真與否都至少執行一次,但 while 不保證相同。
DO {
[CODE]
}WHILE (condition)
  • FOREACH 迴圈:此迴圈接受數組作為變量,並考慮執行程式碼直到數組的最後一個元素。
FOREACH ($arr_var as $val){
[CODE]
}

中級 PHP 指令

還有其他幾個流行的 PHP 命令,它們也被 PHP 開發人員使用,它們不是非常基本的命令,但更多地與 PHP 配合使用。下面列出了一些中間 PHP 命令類型:

1. PHP Include

In PHP, INCLUDE is mainly using for appending define code in an external file with the current working file.

INCLUDE ('name of the external file')

2. PHP Functions

Maximum business logic can be defined within this PHP function.

Function "name of the function" (argument1, argument2 …){
[CODE]
Return "expected result";
}

3. PHP Array

Array is mainly holding multiple related information in a single variable. Three kinds of arrays PHP normally supported.

  •  Indexed Array: $student = array(“A”, “B”, “C”);
  • Associative Array: $score = array(“A”=>80, “B”=>90, “C”=>85);
  • Multidimensional Array: $stu_score = array($student, $score);

4. PHP FORM

It is similar to the HTML form.

<form action="" name="" type="post">

Advanced Commands

However, some of the critical tasks often need to be performed by the users of the PHP command. These tasks also have some advanced commands to execute, such as storing the cookie value, redirecting the page to some relevant pages or connecting to the database. Those advance kind of PHP commands are below:

1. PHP Cookies

A cookie is mainly using storing some of the user type value in their own system so that it can come automatically for the same website.

setCiookie(ckname, ckvalue, ckexpire, ckpath, ckdomain, cksecure) >>> creating Cookie
$_COOKIE['cookie name'] >>> get cookies value

2. PHP Redirect

Redirecting to a new page by below command:

Header("Location:'URL to redirect'");

Tips and Tricks to Use PHP Commands

Some common users who are very frequently using PHP commands, they normally use some of the tips and tricks for utilizing PHP commands output in a proper way. Those kinds of tricks normally solving some user-specific queries and display execution output for understanding the same properly. Some of the very commonly used key tricks are:

1. Avoiding multiple ‘IF-ELSE’ statements

For little critical complexity of business logic, sometimes developer using huge ‘IF-ELSE’ condition which creates a real problem of understanding the logic and final review. So one of the popular operators in PHP for avoiding the same is the Ternary operator. It is something like if conditions are true then doing something, else fetching some default value.

$value = (!empty($_GET['val'])? $_GET['val']: 'ABC');

2. Autoloading of class

Somehow requirement of using some common files in multiple pages, In that case rather than mention those common files in every page, a developer can make one common header and mention those common classes on that header page.

Conclusion

PHP is now a very popular programming language which used by the maximum common web application. If an organization are not a big concern with the site security or code vulnerability then PHP will be always a good option. The population of a page in PHP is very fast rather than any programming language.

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 中的註釋下一篇:PHP 中的註釋