Will side-effects be executed in this if clause

By : Masi
Source: Stackoverflow.com
Question!

Does the function add_new_user run the SQL INSERT query if it is inside the if clause?

I have the functions

function add_new_user ( $username, $email, $passhash_md5 )
{
    -- cut: database INSERT queries
    return 1;                                                                                                
}

I use the code above as

if ( validate( $username, $password, $email ) == 1 ) {
    if ( add_new_user ( $username, $email, $passhash_md5 )  == 1 ) {   
                // problem here
              -- cut
    }                                                                                                        
}

I asked this question because I am not sure why this handler always gives unsuccessful registraton notice. The two codes belong to the handler. The bug seems to be in the syntax of my code and perhaps in the use of if/else.

By : Masi


Answers
Does the function add_new_user run the SQL INSERT query if it is inside the if -clause?

Yes

function foo() {
  echo 'A';
  return true;
}

function bar() {
  echo 'B';
  return false;
}

if ( foo() ) {
  if ( bar() ) {
    echo 'C';
  }
}
// Output: AB
By : Mike B


Yes

The function first get's evaluated for it's return value.

After the return value is known, the return value is compared to the integer value 1.



Yes. Anything between the if ( and the ) { is run to see if it is true. You can do anything in here really.



This video can help you solving your question :)
By: admin