Home > Software engineering >  Why do functions execute in PHP if statements?
Why do functions execute in PHP if statements?

Time:03-20

So the syntax for if statements is:

if(condition){
    //code to execute
}

Then why do functions when placed in place of conditions work in PHP.

CodePudding user response:

In PHP (and many languages) there is no specific concept of a "condition"; the actual definition of an if statement is:

if(expression){
    //code to execute
}

An "expression" is simply anything that can be evaluated and results in a value. A single value, like 42 is an expression on its own, as is a simple sum like 1 1. A function call like strlen('hello') is also an expression, evaluating to the result of the function; the function has to be run, which may have side effects, to determine that result. Expressions can be arbitrarily complex by linking then with operators, like strlen('hello') * 2 1.

Commonly in an if statement, you'd have something like $foo === $bar - this is just an expression that uses the === operator to give a boolean result, either true or false. PHP will evaluate that expression, and then decide whether to run the conditional code based on the result. The expression can be as simple or complex as you want - if(true) is valid, though not often useful, and so is if((strlen('hello') * 2 1) > 10).

If the result of the expression is not a boolean, PHP "coerces" it into one, as described on the manual page about the boolean type. For instance strlen($foo) evaluates to an integer, and all integers other than zero coerce to true, so if(strlen($foo)) acts like if(strlen($foo) !== 0).

Interestingly, an assignment can also be used as an expression, evaluating to the value assigned. This lets you do things like $foo = $bar = 0; where $foo is assigned the result of running $bar = 0; which is of course 0. It also lets you put assignments inside if statements, like if ( $result = getData() ) { ... }, which is shorthand for $result = getData(); if ( $result ) { ... } This technique should be used with care, though, because at a glance it can be hard to spot the difference between = (assignment) and == (weak comparison).

CodePudding user response:

The values returned in a PHP if condition are not restricted to be "strictly" Boolean, however the condition is expected to be Boolean. Why? Because all PHP variables types (inbuilt or user-defined) can be implicitly type-casted (converted automatically) to Boolean. According to the PHP manual:

To explicitly convert a value to bool, use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a bool argument.

The PHP manual also explicitly specifies the falsy values for the different variable types including user defined types with all other values not specified being truthy:

the following values are considered false:

  • the boolean false itself
  • the integer 0 (zero)
  • the floats 0.0 and -0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • the special type NULL (including unset variables)
  • SimpleXML objects created from attributeless empty elements, i.e. elements which have neither children nor attributes.

Every other value is considered true (including any resource and NAN).

Therefore, to explicitly answer your question:

why do functions when placed in place of conditions work in PHP?

One reason I know of, is so you can conveniently perform assignments in the conditions:

function inverse_power($base, $exp)
{
    if($power = pow($base, $exp)) {
        return 1/$power;
    }
    else {
        return "logical error: you can't divide by zero";
    }
}

echo inverse_power(2,1); // 0.5
echo inverse_power(0,1); // logical error: you can't divide by zero

From the above example, you see the feature saves me multiple lines of code. Note that $power is not explicitly Boolean, but will be automatically converted to Boolean only to test the condition. The actual value of $power still persists throughout the function.

CodePudding user response:

Because a condition is a boolean and functions can return booleans. And for conditions to work, the condition itself has to be evaluated, so it can be a function that is executed also.

CodePudding user response:

Its depend what if the function return something. You can also declare in PHP void functions. Then the condition would not work.

But if the function return a value = boolean, string, number, object etc. Then you can check this in your if condition.

  •  Tags:  
  • php
  • Related