PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO). Sergey D Skudaev. Читать онлайн. Newlib. NEWLIB.NET

Автор: Sergey D Skudaev
Издательство: Издательские решения
Серия:
Жанр произведения: Компьютеры: прочее
Год издания: 0
isbn: 9785449090157
Скачать книгу
greetme (“John’);

      echo "<br>”;

      echo “Greeting=”. $greeting.”<br>”;

      ?>

      Output:

      Hi There John

      Greeting=Good Morning

      Inside the function we assigned the value “Hi There” to the local variable $greeting. As a result, the function will return “Hi There John”. However, because we printed the $greeting variable outside the function, the global variable $greeting value remains “Good Morning”.

      Let us modify our code again.

      <?php

      $greeting=“Good Morning”;

      function greetme ($name) {

      global $greeting=“Hi There”;

      echo $greeting.””. $name;

      }

      echo greetme (“John’);

      echo "<br>”;

      echo “Greeting=”. $greeting.”<br>”;

      ?>

      Output:

      Hi There John

      Greeting=Hi There

      In the above we’ve declared a global variable inside the function, using global. Then we assigned the value “Hi There” to the global $greeting variable.

      We previously assigned “Good Morning” to the global variable $greeting outside the function. Then, inside the function, we assigned a global variable with the same name to the value “Hi There”. Now the global variable $greeting no longer has the value “Good Morning”, but rather “Hi There”. So, when we print the value of the global variable outside the function it displays, “Hi There”.

      Static variable

      A static variable is the same as a local variable, but it is not destroyed when the function execution is ended.

      <?php

      function greetme ($name) {

      static $greeting=“Hi There”;

      $greeting=$greeting.””. $name;

      echo $greeting.”! <br>”;

      }

      greetme (“John’);

      greetme (“John’);

      greetme (“John’);

      ?>

      Output:

      Hi There John!

      Hi There John John!

      Hi There John John John!

      The value of the static $greeting variable is changing after each function call because the $name variable is added to it each time.

      Remove the static keyword and the Output: of the function will be the same in the second and the third call

      <?php

      function greetme ($name) {

      $greeting=“Hi There”;

      $greeting=$greeting.””. $name;

      echo $greeting.”! <br>”;

      }

      greetme (“John’);

      greetme (“John’);

      greetme (“John’);

      ?>

      Output:

      Hi There John!

      Hi There John!

      Hi There John!

      Passing parameters by value or by reference

      At run time, the system assigns our declared variable to a memory location called a reference.

      The reference to a variable can be accessed by preceding the identifier of a variable with an ampersand sign (&). The variable address is a hexadecimal number. As seen displayed in the following C++ code, on the first line we declare a pointer (*a) to a memory address. On the second line, we declare a variable (b) and assign its address to the pointer (a). And finally, on the last line, the content of the address of the memory location is displayed as a hexadecimal number.

      int *a;

      int b=7;

      a = &b;

      cout <<“The address of the memory location of b:"<<a <<endl;

      Output: The address of the memory location of b: 0039FF15

      In PHP you can pass the variable address to a function by preceding its name with an ampersand (&). Normally, when we pass a variable to a function, by default the system makes a copy of the variable and passes the copy to the function. The value of the copy may change inside the function, but the value of the original variable remains the same.

      As reflected in the two examples below, when passing a variable by reference, the address of the variable is passed inside the function. So, if a new value is assigned to the address, the value of the original variable changes as well. In the first example, we pass variables by values; and in the second, we pass them by reference.

      <?php

      function switch_by_value ($k, $n) {

      $temp=0;

      $temp=$k;

      $k=$n;

      $n=$temp;

      }

      $a = 5;

      $b= 7;

      echo “By Value: <br>”;

      echo “Before: a=”. $a. ' b=”. $b.”<br>”;

      switch_by_value ($a, $b);

      echo “After: a=”. $a. ' b=”. $b.”<br>”;

      Output:

      By Value:

      Before: a=5 b=7

      After: a=5 b=7

      Notice that the values of the original variables (a) and (b) are not switched.

      function switch_by_reference (&$k, &$n) {

      $temp=0;

      $temp=$k;

      $k=$n;

      $n=$temp;

      }

      $a = 5;

      $b= 7;

      echo “By Reference: <br>”;

      echo “Before: a=”. $a. ' b=”. $b.”<br>”;

      switch_by_reference ($a, $b);

      echo “After: a=”. $a. ' b=”. $b;

      ?>

      Output:

      By Reference:

      Before: a=5 b=7

      After: a=7 b=5

      Notice that the values of the original variables (a) and (b) are switched.

      Useful