简体   繁体   中英

PHP syntax question

I found this line of code and I'm trying to comprehend what it's doing. The part I'm not familiar with is the question mark and the colon. What are these characters used for?

$string = $array[1] . ($array[0] === 47 ? '' : ' word');

That's a ternary operator; basically a short-hand conditional.

It's the same as:

$string = $array[1];

if ($array[0] !== 47)
    $string .= ' word';

See this section in the PHP manual (the "Ternary Operator" section).

That's the ternary operator.

Here's a reference to a tutorial

It works somehow like this:

function tern()

    if ($array[0] === 47)
    {
        return '';
    }
    else
    {
        return 'word';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM