Friday, February 12, 2010

Shorthand way of verifying an IF statement is true or false?

If i send some string to a function to check whether it is true or false and therefore return true or false like





function doStuff($content){





if ($content ===false){





return false;





}else{


return true;


}


}





this is ok, but at the receiving end i think there is an even shorterhand way of say,





if(doStuff(';stuffinhere';)===false){


//etc


}





something like





dostuff(';suffinhere';) ? {





}





basially miss out the IF word or something i could be mistaken though.Shorthand way of verifying an IF statement is true or false?
Most languages will let you use syntax like this -





function doStuff($content)


{


return $content==true;


}





if (doStuff(';stuffinhere';))


{


// whatever


}





However you can do away with the function entirely -





if ($content)


{


// whatever


}





The 'if' simply check that the argument is zero or not, the argument dosen't have to be a logical operation, or even numeric, a pointer to a string can be zero just as much as an integer can.Shorthand way of verifying an IF statement is true or false?
Depends what language you are using. The general syntax is





Test ? Do this if T; Do this if F

No comments:

Post a Comment