Here is an example of what I am trying to understand:
return (@mysql_query($sql)) ? true:false;
I want to return either true or false if this line is executed successfully:
$this->sql->execute();
Can someone explain me how this if like thing works and how can I use it with my line?
This statement executes the
mysql_query()call and if it was successful, returns the booleanTRUEfrom the function in which it resides. If the SQL query fails, it returns booleanFALSE.Whether or not you can use this construct depends on if you expect your
execute()call to return a result set (in aSELECTstatement) or if it is used forINSERT/UPDATE/DELETE. If you attempt this with aSELECTstatement, you would not be able to fetch your result rows with this statement.If it is only for
INSERT/UPDATE/DELETE, you may be able to doHowever, we still can’t be sure this will work because you have not posted the contents of the class from which
execute()is called.The ternary operator is used to assign or return one value or another based on the result of a condition.
Ternary operations are very commonly used to assign one of two values to a variable. Using them in a return statement is a similar operation, if you think of the variable assignee as the function’s return.