Jump to content

Using ? and :


cburwell

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!
Link to comment
https://forums.phpfreaks.com/topic/21902-using-and/
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.
Link to comment
https://forums.phpfreaks.com/topic/21902-using-and/#findComment-97845
Share on other sites

Archived

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

×
×
  • Create New...

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.