开发者

Php return boolean

Here is an example of what I am t开发者_高级运维rying 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 boolean TRUE from the function in which it resides. If the SQL query fails, it returns boolean FALSE.

Whether or not you can use this construct depends on if you expect your execute() call to return a result set (in a SELECT statement) or if it is used for INSERT/UPDATE/DELETE. If you attempt this with a SELECT statement, 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 do

return $this->sql->execute() ? true : false;

However, 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.

$returned_or_assigned_value = condition ? true_return_val : false_return_val;

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜