开发者

Php what is the name of this and what does it do?

开发者 https://www.devze.com 2023-03-24 09:00 出处:网络
I\'d love to know what this means so I can google it as I see it all the time and it seems to be very useful

I'd love to know what this means so I can google it as I see it all the time and it seems to be very useful

(($winstate==1)?'X':'O')

edit: The var开发者_开发百科s are irrelevant.

Thanks guys


That's called a ternary operator, it's PHP's only ternary operator, and it's shorthand for a conditional:

if($winstate == 1){
    return 'X';
}else{
    return 'O';
}

It's frequently used when the conditional test results in an assignment or returns something, in this case suppose you wanted to assign 'X' or 'O' to a variable $move, it's far more concise to write:

$move = ($winstate == 1) ? 'X' : 'O';


Look at Comparsion Operators

There's everything explained

<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

// The above is identical to this if/else statement
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消