1

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

0

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

@WesleyMurch Actually but he needs some reference, I thought he might take a look how functions actually work
So, I'll assume the server parses it on the first occurrence? This is exactly what I was looking for, thanks.
@Adelphia It actually executes the code inside the function when you declare it...
1
$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.

2 Comments

i guess i should look up the definition of the word "parse". Thanks
The parsing will construct a tree of the source code that the computer can understand. The engine will then execute it. Between the two of them, a compiler can convert the tree to bytecode to speed up the execution. That's not very important in the original question, just nice to know :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.