Home php教程 php手册 php中(等于和全等于)== 和 === 的区别和应用

php中(等于和全等于)== 和 === 的区别和应用

May 25, 2016 pm 04:48 PM
and

在php中等于和全等于在表达试上== 和 ===了,从简单层面来说就是一个简单等于,一个是精确定很高,下面我来给各位同学介绍== 和 === 的区别和应用,各位同学可参考.

在php中的符号等于'=='和全等于'==='的区别,从表面上看它们都是判断等号左右两把是否相等的功能,只是一个是两个等号,一个是三个等号.

我们都应该知道,php是一种弱类型的语句,他的变量、字符定义其实并不十分规范,比如 $a = '4′;我们可以说这个$a是一个字符,但是他又可以直接进行数字的加减乘除;而且我们还知道一句话,叫做,非零即真,那么这句好就是对于==这个符号来说的.

实例代码如下:

if(4!=false){echo 'ok'},
Copy after login

这个判断是成立的,他会输出ok;因为4不是零,那么就是真,真就不等于false,所以判断成立,就会输出ok,其实这里4在判断比较的时候,因为其对比对象的原因,所以这个4被转化为了布尔值ture,所以判断成立.当然如果这里用===也是成立的.

实例代码如下:

if(4!==false){echo 'ok'}
Copy after login

他也会输出ok,因为4本来就不是false,他们不是同一个字符.但是两者有个很重要的区别,就是,==或!=是会因为比对的对象而转化字符类型的,而===或!==是强制他不进行转化而原样进行对比,比如下面一个例子:

实例代码如下:

if(0!=false){echo 'ok'}
Copy after login

这个就不会输出ok,因为0就是false,判断不成立,但是如果你写

实例代码如下:

if(0!==false){echo 'ok'}
Copy after login

这个判断就成立,会输出ok,因为0如果不转化,那他就不是false,0就是0;

实例代码如下:

if('4′!=4){echo 'ok'}
Copy after login

这个就不会输出ok,因为他转化后相等,判断不成立,但是如果是

实例代码如下:

if('4'!==4){echo 'ok'};
Copy after login

这个就会输出ok,判断成立,因为在恒等面前,有单引号的4和没有单引号的4并不是同一个4,一个是数字,一个是个字符.所以这样看来他们的对比就很简单了.他们有什么实际应用呢,就是一些比较特殊的时候会用到.比如下面:

实例代码如下:

if(strpos('abc','a')!=false){echo 'ok'};
Copy after login

一段代码来看,实例代码如下:

<?php
$a = 0;
$b = false;
if ($a == $b) {
    echo "等于";
}
if ($a != $b) {
    echo "不等于";
}
?>
Copy after login

我们知道数值0也是代表假的.所以这个结果会输出"等于".如果我们把程序改下,用全等于的呢

实例代码如下:

<?php
$a = 0;
$b = false;
if ($a === $b) { //改成全等于
    echo "等于";
}
if ($a !== $b) {
    echo "不等于";
}
?>
Copy after login

这时候我们突然发现他会输出"不等于".这是为什么呢.

因为:等号只判断它们左右两边的最终结果值,而不判断数据类型.而全等于首先判断等号两边的数据类型是否一致.接着还要判断两边的最终结果值是否一致.如果都一致才会返回true呢.所以在例子中换成全等于他会判断两边的数据类型,$a 是数值型.而$b是布尔型,所以他会返回假值.==' 和 '!=='也是同样的原理.


               
               

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ASUS motherboard options compatible with R55600 (including R55600u and 5600h) ASUS motherboard options compatible with R55600 (including R55600u and 5600h) Jan 02, 2024 pm 05:32 PM

ASUS motherboard options compatible with R55600 (including R55600u and 5600h)

Which one is better, Celeron g4900 or i36100? (Which one is better, Celeron g4900 or i34170?) Which one is better, Celeron g4900 or i36100? (Which one is better, Celeron g4900 or i34170?) Jan 01, 2024 pm 06:01 PM

Which one is better, Celeron g4900 or i36100? (Which one is better, Celeron g4900 or i34170?)

How to turn off the mouse and keyboard lights after turning off the Gigabyte motherboard GA78LMTS2? (The mouse light cannot be turned off after the Gigabyte a88 motherboard is shut down) How to turn off the mouse and keyboard lights after turning off the Gigabyte motherboard GA78LMTS2? (The mouse light cannot be turned off after the Gigabyte a88 motherboard is shut down) Dec 26, 2023 pm 07:58 PM

How to turn off the mouse and keyboard lights after turning off the Gigabyte motherboard GA78LMTS2? (The mouse light cannot be turned off after the Gigabyte a88 motherboard is shut down)

Which one is better, Huntkey s980 or Bauhaus (Huntkey Apollo or Lianli Bauhaus o11) Which one is better, Huntkey s980 or Bauhaus (Huntkey Apollo or Lianli Bauhaus o11) Jan 12, 2024 am 11:06 AM

Which one is better, Huntkey s980 or Bauhaus (Huntkey Apollo or Lianli Bauhaus o11)

Find maximum sum of two array elements in Java Find maximum sum of two array elements in Java Sep 11, 2023 pm 02:13 PM

Find maximum sum of two array elements in Java

Tokens to be listed: $DOGEL, $BRAIN, $BTD, $PINU and $PIMEME Tokens to be listed: $DOGEL, $BRAIN, $BTD, $PINU and $PIMEME Mar 03, 2025 pm 08:03 PM

Tokens to be listed: $DOGEL, $BRAIN, $BTD, $PINU and $PIMEME

What is the difference between Lexus ES300h Premium Edition and Executive Edition? What is the difference between Lexus ES300h Premium Edition and Executive Edition? Dec 30, 2023 am 08:21 AM

What is the difference between Lexus ES300h Premium Edition and Executive Edition?

How big is the difference between high and low motherboards of oppoa5? How big is the difference between high and low motherboards of oppoa5? Jan 03, 2024 pm 03:16 PM

How big is the difference between high and low motherboards of oppoa5?

See all articles