Home  >  Article  >  Backend Development  >  关于php中strtok的1点疑问

关于php中strtok的1点疑问

WBOY
WBOYOriginal
2016-06-13 10:33:51828browse

关于php中strtok的一点疑问
请看以下代码

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $string = "Hello world. Beautiful day today."; $token = strtok($string, " "); while ($token !== false) { echo "$token<br />"; $token = strtok(" "); } ?>

循环终止的条件应该是$token === false,可是strtok返回的是string类型的值,这两者怎么会===呢?
已知'' === false是不成立的,也就是说$token === ''也是不成立的,那么最终$token的值到底是什么呢?

------解决方案--------------------
php 是一种弱类型语言,我们来看这个例子

$str='';

$token=strtok($str,'');

var_dump($token);

这个时候返回的是 bool(false)

看到了吗?所以是用 !==FALSE;

------解决方案--------------------
正常情况下strtok确实返回字符串,但查到最后一个之后了就没有了,这时候返回值不是字符串也不是NULL而是boolean型的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