php - if 语句和函数

标签 php web

我有时会看到这样的代码:

if(!Somefunction()){
    // ...
}else{
    // ...
}

我更像是一名 Java 开发人员,我知道上述声明成立的唯一方式是函数最后返回 true 还是 false。

以这种格式使用的每个函数都必须返回 true 或 false 吗?还是说函数执行成功了?

最佳答案

不,不一定是 TrueFalse。在 PHP 中,任何变量都可以转换为 boolean .

当转换为 bool 值时,以下值被认为是False:

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags

关于php - if 语句和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27773845/

相关文章:

php - 将日期时间转换为碳实例

php - 将 JSON 解码为 PHP 数组,删除相同的条目并编码回 PHP

javascript - 在这种情况下我可以使用 JS 变量吗?如何使用?

xml - 配置 Perl 脚本为超大网站自动生成 XML 站点地图

html - 悬停某个图像时显示某些信息

java - Selenium 用户名密码错误

php-curl 不支持 url 中的 utf-8

php - 在 MySQL 数据库中存储日期和时间以便以后用 PHP 显示的最佳方法是什么?

PHP 代码无法在远程服务器上运行

html - 我的 Bootstrap 网站在右侧显示一个空的可滚动空白区域