Skip to Content

Welcome!

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.

This question has been flagged
As a moderator, you can either validate or reject this answer.
Accept Reject
25 Views

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.

Avatar
Discard

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!