search
HomeBackend DevelopmentPHP Tutorial请教一个小白问题:为什么php常常用!==false表示真的,为啥不直接用===true?

比如下面的代码:

<code>$str4="php-class,mysql-class,div-css,0,dreamweaver";
$s=strtok($str4,',');
while($s!==false){
    echo $s."<br>";
    $s=strtok(',');
}</code>

while($s===true){}为啥就无法显示呢?
true在什么地方使用呢~~
初学者 请大家关爱 O(∩_∩)O~
新年开心~

我自己的理解是:!==false 就要满足1.值不等于 2.类型不等于,当strtok分解结束时返回null(蒙的哈)出现$s=false 使循环语句不成立;但如果使用===true时,也要满足:1.值等于 2.类型等于,那么第一个被分解的字符串都不是布尔值,所以循环直接停止,但如果使用'=='那么遇到'0'时,(不等于true) !=ture,那么此时循环也会停止。
使用!==false,就意味着值不等于,类型也不等于,只有返回false才停止
试了试,貌似对的,但是这么复杂的逻辑光想就得5分钟,脑子乱了。。。。。

回复内容:

比如下面的代码:

<code>$str4="php-class,mysql-class,div-css,0,dreamweaver";
$s=strtok($str4,',');
while($s!==false){
    echo $s."<br>";
    $s=strtok(',');
}</code>

while($s===true){}为啥就无法显示呢?
true在什么地方使用呢~~
初学者 请大家关爱 O(∩_∩)O~
新年开心~

我自己的理解是:!==false 就要满足1.值不等于 2.类型不等于,当strtok分解结束时返回null(蒙的哈)出现$s=false 使循环语句不成立;但如果使用===true时,也要满足:1.值等于 2.类型等于,那么第一个被分解的字符串都不是布尔值,所以循环直接停止,但如果使用'=='那么遇到'0'时,(不等于true) !=ture,那么此时循环也会停止。
使用!==false,就意味着值不等于,类型也不等于,只有返回false才停止
试了试,貌似对的,但是这么复杂的逻辑光想就得5分钟,脑子乱了。。。。。

PHP 是一门弱类型语言,弱类型语言最重要的原因:隐式类型转换。

最主要的表现是赋值、计算和比较:
赋值:

$a = 1; // int
$a = '1'; // string

上面例子中 $a 的类型是随着赋值的类型改变而改变的,你肯定知道这在 C 语言中是不行的,因为 C 语言中变量类型都是声明的时候确定的,确定下来之后就不能改变。

计算:

$a = 1;
$b = '2';
echo $a + $b; // 结果是 3

类似的代码在 Python 中也是不行的,你会得到一个这样的错误:

<code>TypeError: unsupported operand type(s) for +: 'int' and 'str'</code>

Python 也是强类型语言,不会对变量的类型进行推断,所以就直接抛了错误给你。但是 PHP 是怎么做的呢?
Python 中连接字符串也是使用 +,但是 PHP 需要用 .。在 PHP 内部,当你使用 + 的时候,符号两边的变量都会先被转换成数字类型(浮点、整型),同理,使用 . 的时候,符号两边实际上都会先被转换成字符串。因为 Python 不需要进行转换,遇到数字计算,遇到字符串连接就行了。

这个转换过程也是很有意思的:

$a = 1;
$b = '2';
echo $a + $b; // 3
$b = 'a2';
echo $a + $b; // 1
$b = '2a';
echo $a + $b; // 3

看到上面你应该已经猜出来了:字符串是从前往后搜索直到遇到一个非数字字符为止。

下面我们来说这里会遇到的情况:比较。

===== 包括 !=!== 的区别其实你已经猜到了,一个会比较类型,一个不会比较类型。在 PHP 内部的描述中用 equalidentical 这两个单词来描述。感受一下区别。

准确的顺序是:===!== 是先判断类型是否一样,再比较具体的值。如果类型都不一样,那也没必要继续比较了。这意味着即使是 1.0 === 1 得到的也会是 false,因为类型不同。

其实 == 也是要检查类型的,不过动作却是:它会先根据操作符两边变量类型的情况做出判断对变量先做隐式的转换然后再进行比较!这里我不告诉你转换的顺序,但是基本的一些你应该知道:

0 == false; // true
'1' == 1; // true
null == false; // true
null != 'null'; // true

还有你可能不知道的,比如:

123 == '123abc'; // true
'0e123' == '0e456'; // true

到这里这个问题基本上清楚了。因为 '0' == false 是成立的,这就是要用 !== 的原因。

话说回来 strtok 其实本身是个奇怪的函数,这是个自带迭代的函数。如果使用 != 来判断, 就无法准确的取出 strtok('hello world 0', ' ') 这个分解的第三段 0

http://cn2.php.net/manual/zh/function.strtok.php

有些函数默认返回值为intstring 类型,只有在失败的时候才返回false。所以进行判断的时候使用!==false。比如strtok来说,如果执行结果为期望结果,则返回string否则返回false。so~~

更新一下

再比如 strpos 之类的函数,返回在$str$find在哪个位置,比如 在123中寻找1的位置,那将返回int(0)。只有在未找到的时候,返回false

然而, if (0) 的结果是false,if中的逻辑将不会执行。所以判断 if ($result !== false) 才是正确的方法。

关于 !==!= =====的判定区别,自己Google一下吧。

<code>'something' !== false 
'something' !== true</code>

这两个表达式都为真

我自己的理解是:!==false 就要满足1.值不等于 2.类型不等于,当strtok分解结束时返回null(蒙的哈)出现$s=false 使循环语句不成立;但如果使用===true时,也要满足:1.值等于 2.类型等于,那么第一个被分解的字符串都不是布尔值,所以循环直接停止,但如果使用'=='那么遇到'0'时,(不等于true) !=ture,那么此时循环也会停止。
使用!==false,就意味着值不等于,类型也不等于,只有返回false才停止
试了试,貌似对的,但是这么复杂的逻辑光想就得5分钟,脑子乱了。。。。。

<code>0 !== false  // true
0 === true // false</code>

因为大多数调用,失败都返回false

成功时返回资源类型 不成功返回false

也许只有在失败的时候返回false,成功并没有返回值。

假如某接口返回的resource类型,你用true怎么比较?

这个和返回值有关系啊,如果函数返回值是true你就可以这么做啊。。不过就PHP内置函数而言,一般是错误返回flase,所以用false来判断是否出错。

Statement
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
How can you prevent session fixation attacks?How can you prevent session fixation attacks?Apr 28, 2025 am 12:25 AM

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

How do you implement sessionless authentication?How do you implement sessionless authentication?Apr 28, 2025 am 12:24 AM

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

What are some common security risks associated with PHP sessions?What are some common security risks associated with PHP sessions?Apr 28, 2025 am 12:24 AM

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

How do you destroy a PHP session?How do you destroy a PHP session?Apr 28, 2025 am 12:16 AM

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How can you change the default session save path in PHP?How can you change the default session save path in PHP?Apr 28, 2025 am 12:12 AM

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft