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


Parameters, Arguments, & Scope   in Category: PHPlevel2   by amit

🕙 Posted on 2023-06-15 at 14:22:21     Read in Hindi ...


Parameters and Arguments in Functions

    Let's analyze in detailed the syntax of a function. You have seen in previous page that when expressions and statements written without return keyword inside a custom (rigid) function, they cannot be used dynamically. Similarly, parameters and arguments are features, which make custom functions more flexible.

    At the time of defining a custom function, the variable names placed inside () paren­theses just after the function­Name, are called para­meters. These parameters can have default value or can take data (values) outside from the function.

    The function­Name() (after or before defining it) must be invoked along with data (or values), which must be same as number of parameters, (when default values for those parameters are not provided). The respective data (or values) are called arguments. You have already used various pre-defined functions in which arguments are passed within () parentheses just after that function.

    In the following example, the arguments passed in function­Name (at the time of invoking/calling it) are in form of variables. These variable names (for respective arguments) must already be defined and have assigned some values, otherwise ERROR will be shown. You can directly place literal values as arguments for respective parameters of a function­Name.

<?php
  function functionName( $param1, $param2 ) {
    // statements inside the custom function
    return !$param1 + !$param2;     // The return value can be an expression or literal (e.g. NULL), etc.
  }
  $arg1 = false; $arg2 = NULL;
  var_dump( functionName( $arg1, $arg2 ) );
?>

    A variable name declared outside the function will not be automat­ically accessed inside the functionName. Similarly, a variable name declared inside the function can not be accessed from the outside. Parameters and arguments are way to insert data (or values) from outside to inside of the function­Name. In this way, your custom function will become more flexible.

    When you comment the line 6 of above example, that is, // $arg1 = false; $arg2 = NULL; you will get the following ERROR. However, undefined variables are evaluated as NULL, and thus output is int(2).


Warning: Undefined variable $arg1 in C:\xampp\htdocs\php_project\phptest.php on line 7

Warning: Undefined variable $arg2 in C:\xampp\htdocs\php_project\phptest.php on line 7
int(2)

    When you don't provide any value (even NULL or undefined variable name) as an argument, for any of parameters required in your custom function, then other type of ERROR is shown. Following ERROR will also describes you that how many arguments are expected, and how many you have passed. Reading ERRORs carefully will help you to understand and to analyze the mistake happened in your script, and then you can resolve the problem.


Fatal error: Uncaught ArgumentCountError: Too few arguments to function functionName(), 1 passed in C:\xampp\htdocs\php_project\phptest.php on line 7 and exactly 2 expected in C:\xampp\htdocs\php_project\phptest.php:2 Stack trace: #0 C:\xampp\htdocs\php_project\phptest.php(7): functionName(NULL) #1 {main} thrown in C:\xampp\htdocs\php_project\phptest.php on line 2

    In the following two examples, you can see the first code with simple switch statments, and the second example is exactly same, but in a custom function. Variables (and constants) defined inside a function­Name are local to that function, and not accessible to outside global world! In the first example, you can also see that $zodiac can be accessible from everywhere in one file, or one request-response cycle (when other files are inter-connected with each other).

<?php
  $year = 2023;
  $zodiac = [ 'Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake', 'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig' ];
  switch( ($year - 4) % 12 ) {
    case 0: $sign = $zodiac[0];   break;
    case 1: $sign = $zodiac[1];   break;
    case 2: $sign = $zodiac[2];   break;
    case 3: $sign = $zodiac[3];   break;
    case 4: $sign = $zodiac[4];   break;
    case 5: $sign = $zodiac[5];   break;
    case 6: $sign = $zodiac[6];   break;
    case 7: $sign = $zodiac[7];   break;
    case 8: $sign = $zodiac[8];   break;
    case 9: $sign = $zodiac[9];   break;
    case 10: $sign = $zodiac[10];   break;
    case 11: $sign = $zodiac[11];   break;
  }
  echo "{$year} is the year of the {$sign}."; // outputs as shown below:
?>

2023 is the year of the Rabbit.

    In the following example, return keyword is used which exits the PHP interpreter from the custom function immedi­ately. Therefore, break keyword is not needed. You can also see that the above variable name, $zodiac cannot be accessed directly inside the function. Therefore, you have to declare the same values again in a variable name. It does not matter what is variable name. A local variable name can have the same variable name as of outside the custom function. But, memory locations for both variable names are different. Both variable names are unrelated (not related) to each other.

<?php
  function chinese_zodiac( $year ) {
    $zodiac = [ 'Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake', 'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig' ]; // local variable
    switch( ($year - 4) % 12 ) {
      case 0: return $zodiac[0];
      case 1: return $zodiac[1];
      case 2: return $zodiac[2];
      case 3: return $zodiac[3];
      case 4: return $zodiac[4];
      case 5: return $zodiac[5];
      case 6: return $zodiac[6];
      case 7: return $zodiac[7];
      case 8: return $zodiac[8];
      case 9: return $zodiac[9];
      case 10: return $zodiac[10];
      case 11: return $zodiac[11];
    } // end of switch statement
  } // end of function
  $year = 2022; $sign = chinese_zodiac( $year );
  echo "{$year} is the year of the {$sign}.<br />";
  echo "2027 is the year of the " . chinese_zodiac( 2027 ) . "."; // outputs as shown below:
?>

2022 is the year of the Tiger.
2027 is the year of the Goat.

Scope: Local and Global variables

    You have seen in previous page that functions are global in nature. Here, we will discuss about scope of variables. You have read herein above that variables declared outside any custom function can be accessible from anywhere in one file or in one request-response cycle, but not directly inside the custom function. Also, all variables declared inside the custom function are not accessible from outside of that function. Let's examine these in following examples.

<?php
  $girl = "outside"; // global scope
  function faa() {
    $girl = "inside"; // local scope
  }
  echo "1: " . $girl; // outputs 1: outside
  faa(); // function faa() is called here
  echo "<br />2: " . $girl; // outputs 2: outside - don't access inside of faa()
?>

    In the above example and first of the below examples, both variables (outside and inside the function­Name) have same names, but they are unrelated. The function call doesn't touch the variable declared inside the function. Following the same analogy of above example, parameter is not provided in the custom function fee() herein below. Hence, the if(){} statement doesn't output anything, because $boy is not set inside fee() before 'inside' declaration.

<?php
  $boy = "outside"; // global scope
  function fee() {
    if( isset( $boy ) ) {
      echo 'variable $boy is accessed in fee() function: ' . $boy;
    }
      $boy = "inside"; // local scope
  }
  echo "1: " . $boy; // outputs 1: outside
  echo '<br />';   fee(); // function fee() is called here
  echo "<br />2: " . $boy; // outputs 2: outside - don't access inside of fee()
?>

    In the following example, an argument is passed while calling the function foo(), and this argument makes the outside variable accessible inside the custom function. However, the variable name declared even inside this function, is not touched when calling the function. Obviously, some of you may try to place the inside variable declaration above the if(){} statement, and it will definitely change the value!

<?php
  $man = "outside"; // global scope
  function foo( $man ) {
    if( isset( $man ) ) {
      echo 'variable $man is accessed in foo() function: ' . $man;
    }
      $man = "inside"; // local scope
  }
  echo "1: " . $man; // Outputs 1: outside
  echo '<br />';   foo( $man ); // Outputs variable $man is accessed in foo() function: outside
  echo "<br />2: " . $man; // Outputs 2: outside - don't access inside of 'foo'
?>

    The above examples are described to explain you that variable names defined outside a custom function can only be accessible through parameters. And these parameters (and local variables inside a custom function) are also not related with variables declared outside the function. However, using global (reserved) keyword inside the custom function block {} curly braces, the variables declared outside will be connected to variable declared inside the function block.

<?php
  $bird = "outside"; // global scope
  function fuu() {
    global $bird; /* importing global variable, parameter and arguments are not needed */
    if( isset( $bird ) ) {
      echo '<br />variable $bird is accessed in fuu() function: ' . $bird;
    }
    $bird = "inside"; // local scope
    echo '<br />variable $bird is changed in fuu() function: ' . $bird;
  }
  echo "1: " . $bird; // Outputs 1: outside
  fuu();       /* as soon the custom function is invoked, the variable declared outside is connected and can be changed. OUTPUT is shown as below: */
  echo "<br />2: " . $bird; // Outputs 2: inside
?>

1: outside
variable $bird is accessed in fuu() function: outside
variable $bird is changed in fuu() function: inside
2: inside

    There are some SUPER-GLOBAL (pre-defined) variables, for example, $_GET, $_POST, $_SERVER, $_SESSION, $GLOBALS, $_REQUEST etc. These SUPER-GLOBAL variables can store data in an associative array. These SUPER-GLOBAL variables are accessible even inside a custom function, and you don't need global (reserved) keyword for them.


Leave a Comment: