PHP syntax question: What does the question mark and the colon mean?

综合编程 2018-02-08 阅读原文

Possible Duplicate:

quick php syntax question

return $add_review ? FALSE : $arg;

What do question mark and colon mean?


This is the PHP ternary operator
(also known as a conditional operator) - if first operand evaluates true, evaluate as second operand, else evaluate as third operand.

Think of it as an "if" statement you can use in expressions. Can be very useful in making concise assignments that depend on some condition, e.g.

$param = isset($_GET['param']) ? $_GET['param'] : 'default';

There's also a shorthand version of this (in PHP 5.3 onwards). You can leave out the middle operand. The operator will evaluate as the first operand if it true, and the third operand otherwise. For example:

$result = $x ?: 'default';

It is worth mentioning that the above code when using i.e. $_GET or $_POST variable will throw undefined index notice and to prevent that we need to use a longer version, with isset
or a null coalescing operator
which is introduced in PHP7:

$param = $_GET['param'] ?? 'default';
Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!


PHP – Problem with sending e-mail with form ... I having some trouble sending e-mail with attachment from the php MAIL function. The error checking is working but I can...
Perl Script Output in PHP I have a PHP file that runs a Perl script, using popen and the perl script outputs the following back to the php { 'Ci...
(CLOSED) PHP Textarea returns th... i've asked something similar here, but i cannot find any solution. As i've already told, i'ma beginner with PHP, and wha...
写爬虫时PHP解析HTML最高效的方法那就是用DomCrawler!... 需求来源,需要用PHP解析HTML提取我想要的数据 用PHP写网站爬虫的时候,需要把爬取的网页进行解析,提取里面想要的数据,这个过程叫做网页HTML中数据结构化。 很多人应该知道用phpQuery像JQuery一样的语法进行网页处理...
Array merges two arrays with a different number of... I am trying to merge two arrays that I have in order to end up with one array. Here is an example of my first array: A...