跳至内容

欢迎!

This community is for professionals and enthusiasts of our products and services.
Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

此问题已终结
作为版主,您可以批准或拒绝该答案。
接受 拒绝
26 查看

When you want to build condition like below:

if($result == ''){

 $result = 'Empty Value';

}else{

 $result = $result;

}

You assign string 'Empty Value' to result when it is empty


But this condition can be done with one line condition, which called ' ternary operator'

============================================

variable = condition ? {true_action} : {false_action};

============================================

$result = empty($result) ? 'Empty Value' : $result;


this is mean:

if $result empty, it will take the 'Empty Value' as the value, else if will take $result


This method make your code shorter, clean. But if the condition is too complicated, not recommended using this method since the normal if else condition more readable.

形象
丢弃

您的回复

请大家尽量给个实质性的回答。如果您想回答这个问题或发表评论,只需 使用评论工具。请记住,您可以随时修改您的答案。 - 不用重复回答同样的问题。另外,请不要忘记投票 - 它确实有助于选择最好的问题和答案!