payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Conditional Functions & Variable Functions   in Category: PHPlevel2   by amit

🕙 Posted on 2023-06-13 at 17:31:20     Read in Hindi ...


Short-Circuiting & Functions

    Short circuiting means the technique which is used by many program­ming languages, when evaluating condi­tional operations (or boolean logic) to save computing power. Thus, short-circuiting skips unnecessary parts of boolean logic or comparison operands. Condit­ional expressions are evaluated from left to right generally: when value of first operand, decides the true/false character of result, PHP will not evaluate the second operand.

<?php var_dump( 4<3 && 5<6 ); ?><!-- Except ! (not), comparison operators have higher precedence over all other logical operators. When the first operand is evaluated false in and comparison, then second operand will not be evaluated. --><br />

<?php var_dump( true || false ); ?><!-- When the first operand is evaluated true in or comparison, then second operand will not be evaluated. --><br />

<?php $my_var = true and false; var_dump( $my_var ); ?><!-- The assignment operator has higher precedence over and xor or operators. Hence, variable $my_var is assigned with true and remaining operand is discarded. --><br />

<?php var_dump( false && false || !false ); ?><!-- ! (not) operator has higher precedence, it is evaluated first. Then, (false && false) will be evaluated as false. Finally, (false || true) will be evaluated. The output is bool(true) --><br />

<?php var_dump( false && (false || !false) ); ?><!-- When paren­theses is used to wrap two operands, those will be evaluated first. Therefore, (false || true) will be evaluated first, and then (false && true) will be evaluated. The output is bool(false) --><br />

    If you want to see the above operations as proof, you can use following examples to see the result. A simple custom function will be defined, and it is placed at those operands which will be evaluted later. Each of following two examples shows you that the function will not be called, because it will not be evaluted after short-circuiting technique is used.

<?php function new_function() { echo 'Hello<br />'; return false; } ?><!-- This function will not be called, when the second operand is not evaluated. -->

<?php var_dump( false && new_function() ); // outputs bool(false) ?><br />

<?php var_dump( true || new_function() ); // outputs bool(true) ?><br />

    However, when the evaluation of first operand does not decides the result in comparison operations (or boolean logic), then second operand will be evaluated and you will see the output from the function new_function() along with returned value is evaluated.

<?php var_dump( true && new_function() ); // outputs Hello<br />bool(false) ?><br />

<?php var_dump( false || new_function() ); // outputs Hello<br />bool(false) ?><br />

Conditional Functions

    Functions need not be defined before they are referenced, except when a function is conditi­onally defined. When a function is defined in a condi­tional manner, its definition must be processed prior to being called. Though all functions are global in scope, but any condi­tional function is to be defined first. Let't see an example of a conditional function:

<?php
  if( true ) {
    /* if the above condition is false, you get
    Fatal error: Uncaught ERROR: Call to Undefined function mno() */


    function mno() {
      return 'Hello';
    }
  }
  var_dump( mno() ); // outputs string(5) "Hello"
?>

    The PHP interpreter is also called "lazy", meaning it will do the minimum number of comparisons possible to evaluate conditions, etc. It's a proof that PHP behaviour is consistent. When the PHP interpreter see that any function name (either built-in or custom) is invoked, then it first processes all codes from top to bottom. But, when it sees a conditional expression or conditional block, it becomes "lazy".

Variable functions

    PHP supports the concept of variable functions. This means that if a variable name has paren­theses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

    Variable functions won't work with language constructs such as echo, print, unset(), isset(), empty(), include, require and the like. Utilize wrapper functions to make use of any of these constructs as variable functions. Let't see an example of variable functions:

<?php
  function xyz() {
    return 'Hello<br />';
  }
  $abc = 'xyz';
  echo $abc();     // outputs Hello<br />
?>

    The above example is an simple variable function. However, within () parentheses, parameters and arguments can be passed, and then desired results can be obtained. There are other types of functions: anonymous, arrow, variadic, etc. You will learn more about functions, parameters, arguments, etc. in next page.


Leave a Comment: