PHP 3

From Ilianko
Revision as of 13:27, 20 October 2013 by Anko (talk | contribs) (→‎scope)


Един код извикваме от много места

над 3500 вградени функции наименование букви и цифри


Функции

function sumNumbers() {
  echo 2+2
}
 
sumNumbers();

параметри

function sumNumbers($a,$b) {
  echo $a+$b;
}
 
sumNumbers(2,2);


параметри по подразбиране

function sumNumbers($a) {
  echo $a+3;
}
 
sumNumbers(2);
function sumNumbers($a, $b = 3) {
  echo $a+$b;
}
 
sumNumbers(2);
sumNumbers(2,4);

връщане на резултати

function sumNumbers($a, $b=3) {

  $a = $a + $b; 
  return $a;

}
$result = sumNumbers(2);
echo $result;


function sumNumbers($a, $b=3) {
  return $a;
  echo $a + $b;  
}

рекурсия

$myArray = array( 1, 
                   array( 2, 
                          3, 
                          4 ), 
                   'five', 
                   array( 'six', 
                          '7', 
                          array('third array', 
                                'third array second element'))); 
 

 function listArray($array, $step=0) {
   if( is_array($array)){
   foreach($array as $key => $value){
     echo 'Array index: '.$key;
	 listArray($value);
	}
  }
  else
  	echo " value $array <br />"; 
 }
  
 listArray($myArray)

scope

$a = 3;
$b = 4;

sumNumbers($a, $b);

function sumNumbers($a, $b) {
  echo $a + $b.'<br />';
  $a = 333;
  echo $a.'<br />';
}

echo $a.'<br />';


$a = 3;
$b = 4;

sumNumbers($a, $b);

function sumNumbers(&$a, $b) {
  echo $a + $b.'<br />';
  $a = 333;
  echo $a.'<br />';
}
 
echo $a.'<br />';