/    Sign up×
Community /Pin to ProfileBookmark

Whats with the return true and return false ??

Given the following code :

[code=php]

function send_msg($sender , $message){

if(!empty($sender) && !empty($message)){

$sender = mysql_real_escape_string($sender);
$message= mysql_real_escape_string($message);

$query = “INSERT INTO `chat`.`chat` VALUES (null , ‘{$sender}’ , ‘$message’)”;

if($run = mysql_query($query)){
return true;
}else{
return false;
}
}
else{
return false;
}
}
[/code]

What if i do just this in the end : (i.e take off the return true and return false)

[code=php]
$run = mysql_query($query)

[/code]

what is the implecation of not typing in the return true and return false ??

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 24.2014 — Public service announcement: you should not be using mysql_*() functions any more, use MySQLi or PDO instead.

Anyway, per the manual:
Return Values ¶

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. [/quote]

Since you are doing an insert statement, there is nothing gained by that if/else block, you could just directly return the result of the mysql_query() call. As to why, it appears the function is designed to return a Boolean result, with true indicating successful insert, otherwise a false indicates the insert did not occur (for one reason or another). Some people do not like to have multiple exit points in their functions, so you could get around that with something like the following:
[code=php]
function send_msg($sender , $message){
$result = false;
if(!empty($sender) && !empty($message)){
$sender = mysql_real_escape_string($sender);
$message= mysql_real_escape_string($message);
$query = "INSERT INTO chat.chat VALUES (null , '{$sender}' , '$message')";
$result = mysql_query($query));
}
return $result; // only exit point
}
[/code]
Copy linkTweet thisAlerts:
@deathshadowOct 24.2014 — Actually, the return true filters out the result handle. That may be a desired behavior for security purposes.

What doesn't serve a purpose is all those extra ELSE... likewise there's no reason to store the result in a value since it's an INSERT. The return short-circuits, so I'd reduce it to:

function send_msg($sender , $message){
if (!empty($sender) && !empty($message)) {
$sender = mysql_real_escape_string($sender);
$message= mysql_real_escape_string($message);
$query = "INSERT INTO <span><code>chat</code></span>.<span><code>chat</code></span> VALUES (null , '{$sender}' , '$message')";
if (mysql_query($query)) return true;
}
return false;
}


NOT that it's very security minded with pasting together a query and as already mentioned, using the mysql_ functions we've been told for eight years (since PHP 5 dropped) to stop using and that a few years ago they FINALLY added [url=http://php.net/manual/en/function.mysql-connect.php]giant red warning boxes[/url] in the manual to wave you off from using!

Not that said query makes any sense... are you dumping them all into the same field? Blindly filling it? Not sure I follow the logic of that whatsoever. I suspect if I wrote that, it would look more like this:

// Assumes $db is an initialized and connected PDO object
function send_msg($sender , $message) {
global $db;
if (!empty($sender) &amp;&amp; !empty($message)) {
$stmt = $db-&gt;prepare('
INSERT INTO chat (sender, message) values (?, ?)
');
if (
$stmt-&gt;execute([$sender, $message]) &amp;&amp;
$stmt-&gt;rowCount
) return true;
}
return false;
}


NOT that I'd actually allow the database connection into the global scope. (one of the biggest security flaws with mysql_)
×

Success!

Help @gautamz07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...