PHP 不等於

王林
發布: 2024-08-29 12:35:34
原創
469 人瀏覽過

PHP中的比較運算子之一是不等於,用符號!=或表示。每當我們想要比較兩個給定值的資料類型時,我們都會在 PHP 中使用不等於運算子。如果給定的兩個值的資料類型相同,即使兩個變數中儲存的值不同,PHP 中的不等於運算子也會傳回true;如果給定的資料類型相同,PHP 中的不等於運算子也會回傳false即使兩個變數中儲存的值相同,兩個值也不相同。

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

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

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

文法:

$variable1 != $variable2;
$variable1 <> $variable2;
登入後複製

其中variable1表示要與variable2表示的變數進行資料型態比較的變數的值。

PHP 中不等於運算子的工作

  • PHP中的比較運算子之一不等於,用符號!=或表示。
  • 每當我們想要比較兩個給定值的資料類型,無論兩個值是否相等時,我們都會在 PHP 中使用不等於運算子。
  • 如果給定兩個值的資料類型相同,即使兩個變數中儲存的值不相同,不等於運算子也會傳回 true。
  • 如果給定的兩個值的資料類型不相同,即使兩個變數中儲存的值相同,PHP 中的不等於運算子也會傳回 false。

PHP 不等於的範例

下面給出的是提到的例子:

範例#1

PHP 程式示範不等於運算子的工作原理,以比較給定兩個值的資料類型並傳回輸出。

代碼:

<?php
//an integer value is stored in a variable called variable1
$variable1 = 10;
echo "The value stored in the first variable is: $variable1<br/>";
//a string value is stored in a variable called variable2
$variable2 = "10";
echo "The value stored in the second variable is: $variable2<br/>";
//not equal operator is used to compare the data type of the given two variables and return the result
echo "The result returned after using not equal operator is: ";
var_dump($variable1 != $variable2);
?>
登入後複製

輸出:

PHP 不等於

在上面的程式中,一個整數值儲存在名為variable1的變數中。然後,一個字串值儲存在名為「variable2」的變數中。兩個變數中儲存的值是相同的。然後我們使用不等於運算子來比較兩個變數的資料類型。不等於運算子傳回 false,因為即使變數中儲存的值相同,但兩個變數的資料類型不同。

範例#2

PHP 程式示範不等於運算子的工作原理,以比較給定兩個值的資料類型並傳回輸出。

代碼:

<?php
//an integer value is stored in a variable called variable1
$variable1 = "Hello";
echo "The value stored in the first variable is: $variable1<br/>";
//a string value is stored in a variable called variable2
$variable2 = "Welcome";
echo "The value stored in the second variable is: $variable2<br/>";
//not equal operator is used to compare the data type of the given two variables and return the result
echo "The result returned after using not equal operator is: ";
var_dump($variable1 != $variable2);
?>
登入後複製

輸出:

PHP 不等於

在上面的程式中,一個字串值儲存在名為variable1的變數中。然後,一個字串值儲存在另一個名為「variable2」的變數中。兩個變數中儲存的值不同。然後我們使用不等於運算子來比較兩個變數的資料類型。不等於運算子傳回 true,因為即使變數中儲存的值不同,但兩個變數的資料類型相同。

範例#3

PHP 程式示範不等於運算子的工作原理,以比較給定兩個值的資料類型並傳回輸出。

代碼:

<?php
//an integer value is stored in a variable called variable1
$variable1 = 2.9999;
echo "The value stored in the first variable is: $variable1<br/>";
//a string value is stored in a variable called variable2
$variable2 = 10.55555;
echo "The value stored in the second variable is: $variable2<br/>";
//not equal operator is used to compare the data type of the given two variables and return the result
echo "The result returned after using not equal operator is: ";
var_dump($variable1 != $variable2);
?>
登入後複製

輸出:

PHP 不等於

在上面的程式中,一個字串值儲存在名為variable1的變數中。然後,一個字串值儲存在另一個名為「variable2」的變數中。兩個變數中儲存的值不同。然後我們使用不等於運算子來比較兩個變數的資料類型。不等於運算子傳回 true,因為即使變數中儲存的值不同,但兩個變數的資料類型相同。

範例#4

PHP 程式示範不等於運算子的工作原理,以比較給定兩個值的資料類型並傳回輸出。

代碼:

<?php
//an integer value is stored in a variable called variable1
$variable1 = "ffffff";
echo "The value stored in the first variable is: $variable1<br/>";
//a string value is stored in a variable called variable2
$variable2 = ffffff;
echo "The value stored in the second variable is: $variable2<br/>";
//not equal operator is used to compare the data type of the given two variables and return the result
echo "The result returned after using not equal operator is: ";
var_dump($variable1 != $variable2);
?>
登入後複製

輸出:

PHP 不等於

在上面的程式中,一個整數值儲存在名為variable1的變數中。然後,一個字串值儲存在名為「variable2」的變數中。兩個變數中儲存的值是相同的。然後我們使用不等於運算子來比較兩個變數的資料類型。不等於運算子傳回 false,因為即使變數中儲存的值相同,但兩個變數的資料類型不同。

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

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