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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.