Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single return of ternary operator

I wonder if possible to write ternary operator for single return. I tried google online, but couldn't find an answer. or it doesnot called ternary operator??

Thank you very much for your advice.

If(A == 1) execute_function(); into A == 1 ? execute_function() //???Possible???

like image 655
Micah Avatar asked Jan 11 '13 09:01

Micah


1 Answers

yes:

(exists == 1) ? execute_function() : false;

runs function if exists is true else wont

Added: Doing just like following would be better:

if( A == 1 ) {
  execute_function();
}

As using ternary operator in above case is not so fruitful, as you are checking only for the true side of the condition and do not care about what's in the false side.

like image 53
Sudhir Bastakoti Avatar answered Sep 25 '22 21:09

Sudhir Bastakoti