PHP问号

标签 php

$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? 'style="display:none;"' : '';

任何人都可以向我解释一下这行代码中问号的作用吗?非常感谢!

最佳答案

这被称为 Ternary Operator ,并且它对多种语言都很常见,包括 PHP、Javascript、Python、Ruby...

$x = $condition ? $trueVal : $falseVal;

// same as:

if ($condition) {
    $x = $trueVal;
} else {
    $x = $falseVal;
}

在 PHP 中使用三元组时需要注意的一点是:

Note: Please note that the ternary operator is a statement, and that it doesn't evaluate to a variable, but to the result of a statement. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions. source

关于PHP问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6583673/

相关文章:

php - 数据表ajax处理中的复选框

php - 从三个变量创建插入语句

php - 为什么 srand(time()) 是坏种子?

php - Magento 类别产品重新索引错误

php - 如何执行大型 PHP 脚本?

php - 如何在 phpmyadmin 中将字符串字段设置为 NULL?

javascript - 如何使用 javascript 更新 HTML 内容但使用 PHP 获取内容

javascript - PHP CodeIgniter - cookie 过期但页面仍然存在,该怎么办?

php - MySQL查询以显示基于三个表的格式

PHP:Mysql 查询不返回任何值