mercredi 25 février 2015

One PHP statement to use result of IF statement for assignment

I have a situation where I have a function that does some database retrieval and either returns a value or an error. I'd like to have a single statement that calls the function and depending on result either assigns the returned value or a default value.


Using the tenary operator I could do something like this:



$val=(getVal($param)!='error' ? getVal($param) : "default");


but I don't want to have to call getVal twice because it excutes database queries and is expensive on performance.


I could also do it in two statements, but if possible I just want one. Appreciate any help.


Aucun commentaire:

Enregistrer un commentaire