Jump to content

Archived

This topic is now archived and is closed to further replies.

cburwell

Using ? and :

Recommended Posts

I posted this somewhere else, but I figured this might be a better place.

I have been looking around for what the use of ?, and : mean in PHP, such as in the following examples

$data = (file_exists($data)) ? $this->parse($data) : $data;

and

$func = ( $regType == 'P' ) ? 'preg_match' : 'ereg' . $case;

Could someone point me in the right direction of some documentation on this? I have tryed searching, but I can't seem to search for the uses of ? and : properly.

Thank you!

Share this post


Link to post
Share on other sites
A little way down the page [url=http://www.php.net/manual/en/language.operators.comparison.php]here[/url]. The ternary operator.

Share this post


Link to post
Share on other sites
To make a long story short:

(mixed return type) = condition ? (what to do if it's true) : (what to do if it's false);

The statement returns whatever it evaluates to; for example,

[code]$condition = true;
$value = $condition ? "three" : 19;[/code]

$value will now contain (string) (5) "three".

[code]$condition = false;
$value = $condition ? "three" : 19;[/code]

$value now contains (int) 19.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.