PHP 中的運算子優先級

WBOY
發布: 2024-08-29 12:57:21
原創
550 人瀏覽過

PHP 運算子優先權實際上指定了兩個表達式如何緊密綁定在一起並首先求值。運算子優先順序還決定何時以及如何使用不同的括號類型對運算子進行分組。運算子優先權可以是較高優先權、較低優先權或相等優先權。它們也是 PHP 程式語言中的許多內建數學運算符,它們根據運算符的類型具有不同類型的運算符優先級。 PHP 程式語言的運算子優先順序對於輕鬆執行數學計算有很大幫助。如果任何編碼語言都沒有運算子優先順序的概念,那麼程式的邏輯就會變得混亂。

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

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

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

運算子優先順序在 PHP 中如何運作?

PHP 程式語言的運算子優先順序是基於運算符的類型。運算符可以是數學運算符或任何類似特殊字元的運算符。對於數學運算符,PHP 語言的運算子優先權類似於 BOD-MAS(括號、順序、除法、乘法、加法和減法)。所有計算機和編碼語言始終遵循這種數學運算符優先級,以便輕鬆執行多種計算。

在這裡,我們將看到字元的運算符優先級,從較高的運算子優先級到較低的運算子優先級。

「[ ]」運算子被賦予第 1st 優先權,

然後 ++、—、~、()、@ 取得第 2 個 運算子優先級,

然後「instanceof」獲得第三優先級,

第四一個是“!”,

第 5 其中一個是「 * , / , % 」,

第 6 優先權賦予 +、– 和 。 ,

>>和 優先級,

>、=、 優先級,

==、===、!=、!==、取得第 9 優先級,

並獲得了第 10 個

^ 得到第 11

|得到第 12

&& 得到 13,

||得到了 14,

?: 得到第 15 ,

=、*=、/=、%=、+=、-=、=、&=、^=、|=、>=、=>已取得第16 個

並獲得了第 17 個

異或得到第18

或取得第 19

和「,」取得第 20 運算子優先權。

PHP 中運算子優先權的範例

以下是下面提到的範例

範例#1

在下面的運算子優先權範例中,首先根據 BODMAS 計算原理,首先計算大括號內的數值元素。因此,對於第一個 echo 語句,將計算 (40-4)/9 並將結果保留為「4」。對於第二個 echo 語句,首先計算 4/9,結果為「0.44444444444」。然後將計算“5*8”,然後用該結果減去 4/9 結果,得到“39.5555555556”。

代碼:

<?php
echo "This is the mathematical calculation by giving higher precedence
to the elements which are inside of the brackets:: <br>";
echo (((5*8)-4)/9);
echo "<br>";
echo "Mathematical calculation done by using BOD-MAS Rule::<br>";
echo (5*8-4/9);
?>
登入後複製

輸出:

PHP 中的運算子優先級

範例#2

在下面的例子中,基於BODMAS原理完成了3個變數值的計算。首先,透過分配一些數值來建立 $n11、$n12、$n13 變數。然後以兩種不同的方式計算這些變數值的加法和乘法。一種計算方法通常是在變數之間分配運算符。第二種計算方法是透過提及大括號和它們之間的運算。將首先計算大括號之間的值。在第一個 $ans1 變數中,將 n12 和 n13 變數相乘,然後加上 n11 的值。在第二個 $ans1 中,首先計算 n11 和 n22 變數值,然後與 n13 變數值相乘。

代碼:

<?php
echo "Program to know how the mathematical operator precedence works :: <br>";
$n11 = 10;
$n12 = 5;
$n13 = 2;
$ans1 = $n11 + $n12 * $n13;
echo "The result of ";
echo "$n11 + $n12 * $n13 = $ans1<br />";
$ans1 = ($n11 + $n12) * $n13;
echo "The result of ";
echo "($n11 + $n12) * $n13 = $ans1<br />";
?>
登入後複製

輸出:

PHP 中的運算子優先級

Example #3

In the below examples output, the value of x++, ++x, – – y values are shown to know what are the values of calculation. So the result will be calculated using “4+6/4*3”. Based on the BODMAS principle, 6/4 is calculated first and leaves 1.5 as the answer. Then 1.5*3 is calculated and leaves 4.5 and then 4+4.5 is calculated and leaves the result as 8.5. This illustration will let you know how the BODMAS principle is used.

Code:

<?php
$x = 4; $y = 5;
$a = 4; $b = 5;
echo "First time x++ value :: ";
echo $a++;
echo "<br>";
echo "Second time ++x value :: ";
echo ++$a;
echo "<br>";
echo "First time - - y value :: ";
echo --$b;
echo "<br>";
echo "Second time - - y value :: ";
echo --$b;
echo "<br>";
$result = $x++ + ++$x / --$y * --$y;
echo "The result of (x++)+(++x)/(- - y)*(- - y) :: ";
echo  "{$result} <br/>";
?>
登入後複製

Output:

PHP 中的運算子優先級

Example #4

In the below example, to know the operator precedence, different types of operator symbols are used for the result variable. The var_dump(result) will be true only if both the elements in the braces are TRUE. You can know what are the values of – – a, a – -, ++b, – – c are shown in the output for better understanding. Now the values of those are used to calculate whether the conditions of the result variable satisfies or not. If satisfied and both the braces conditions are TRUE then the var_dump() function will leave the result as TRUE.

Code:

<?php
$a = 2; $b = 8; $c = 8;
$d = 2; $e = 8; $f = 8;
echo "Actual and Original 'a' variable value :: $a <br>";
echo "Actual and Original 'b' variable value :: $b <br>";
echo "Actual and Original 'c' variable value :: $c <br>";
echo "The value of - - a ::". --$d." <br>";
echo "The value of a - - ::". $d--." <br>";
echo "The value of ++ b ::". ++$e." <br>";
echo "The value of - - a ::". --$f." <br>";
$result = ($a * $a <= $b * $a) && (--$a * $a-- !== ++$b - --$c);
echo  "After the completion of the above result statement 'a' value = {$a} <br/>";
echo  "After the completion the above result statement 'b' value = {$b} <br/>";
echo  "After the completion the above result statement 'c' value = {$c} <br/>";
var_dump($result);
?>
登入後複製

Output:

PHP 中的運算子優先級

以上是PHP 中的運算子優先級的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!