I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

PHP functions as variables

When I do something like this:

$query = mysql_query("INSERT INTO something (something) VALUES('".$something."')");  
if($query){
//do stuff
}

or

$mailstuff = mail($to,$subject,$message,$headers);
if($mailstuff){
//do stuff
}

Which part of the code actually executes the function? The if or the declaration? References appreciated.

Thanks

Upvotes: 1

Views: 61

Answers (2)

Maël Nison
Maël Nison

Reputation: 7353

$mailstuff = mail($to,$subject,$message,$headers);

if($mailstuff) {
    //do stuff
}

Here is what will be done (in the correct order) :

  • mail($to,$subject,$message,$headers)
  • The mail function has returned something. This something is assigned to $mailstuff.
  • The if statement will check if the something in $mailstuff is somewhat equal to true

Please not that contrary to what you suggest in your comment, the server will parse all the code before actually executing it.

Upvotes: 1

Mr. Alien
Mr. Alien

Reputation: 157324

Explanation in the code...

$mailstuff = mail($to,$subject,$message,$headers);
             ^-----------------------------------^
            /*  This part executes the function */


if($mailstuff) {
  ^----------^
/* Condition Here Checks whether the function is successfully executed */
//do stuff
}

PHP function() Reference

Upvotes: 4

Related Questions