|
|||
|
PHP. Functionshttps://www.w3schools.com/php/php_functions.asp
1. Function is called many times: <?php
function a (){ echo "Hello"; } a(); a(); # the value of the function is not returned $a = a(); # nothing to print echo $a ; ?> 2. The function is used recursivelly two times: <?php
function f($a){ $b=$a*10; return $b; } $c = f(10); $d = f($c); echo "$c $d"; ?> 2a. Function with two parameters. One of then does not have default values, but another has. <?php
function addNumbers( $a , $b = 2) { return $a + $b; } echo addNumbers(1); # fatal error # echo addNumbers(); ?> 3. Function, which has result an array: <?php
function eee (){ return array ("jonas", "petras", "simas", "ona"); } list ($a, $b, $c) = eee(); echo "$c <br />";// simas ?> THE SCOPE OF VARIABLES 1. $a is not seen inside function: <?php
$a = 2; function test(){ echo "rezultatas $a <br />"; } test(); ?> 2. Inner variable $a is not seen outside: <?php
$a =2 ; function test(){ $a = 3; echo "inner $a <br />"; } test(); echo "$a <br />"; //2 ?> 3. return does not affect scope of view: <?php
$a = 2; function test(){ $a = 3; return $a; } test(); echo "$a <br />"; // 2 ?> 4. Reference &: <?php
$a = 2; function reference(&$b){ $b = 10; } reference($a); echo "$a <br />"; // 10 ?> 5. Global variables: <?php
$a = "pirmas "; $b = "antras"; function test(){ global $a, $b; echo $a . "<br />"; echo $b . "<br />"; $b = $a . $b; } test(); echo "$b"; // It will print "pirmas antras" ?> 7. Global variables between functions: <?php
--$a = ""; $b = ""; function model(){ global $a, $b; $a= 10; $b= 20; } function view(){ global $a, $b; echo $a, $b; } model(); view() ?> |