dongshijiao6890 2018-07-26 08:35 采纳率: 0%
浏览 95

PHP - 为什么这个工作而其他不工作[重复]

This question already has an answer here:

Using php, I tried to check whether a certain string contains some characters.

I used this code, which evaluates to false:

$string1 = 'Hello World!';
if (strpos($string1, 'hel')) {
    echo 'True!';
}

I did a quick google search and found this variation:

$string1 = 'Hello World!';
if (strpos($string1, 'hel') !== false) {
    echo 'True!';
}

Which works and evaluates to true.

What is the actual difference between them and why the first one evaluates as false and the second as true?

Thanks!

</div>
  • 写回答

1条回答 默认 最新

  • douquan3294 2018-07-26 08:38
    关注

    Strpos returns the position of the first letter of the needle.
    In your case that is the h in "hello" at position 0.

    Since typcasting is enabled in php, 0 is the same as false.
    Thus the first is false because the "h" is at the 0 position of the "hello world" string

    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏