the code “ : ” in php

前端 未结 7 713
难免孤独
难免孤独 2020-12-22 13:40

I\'m curious to know what the syntax \" : \" mean in php I\'ve seen it a couple of times but I can\'t seem to explain it to myself. Can you also use it in a sentence....or i

7条回答
  •  有刺的猬
    2020-12-22 13:46

    Perhaps you are referring to the ternary operator, which uses a ? and : as follows:

    $variable = boolean_expression ? "true_value" : "false_value";
    

    This code is shorthand for an if-else:

    if (boolean_expression) {
       $variable = "true_value";
    }
    else {
       $variable = "false_value";
    }
    

提交回复
热议问题