PHP functions and applications

PHP functions and applications

1. Custom function: custom function
with parameters

<?php
function   cooking($name)
{
    
     
	echo "买{
      
      $name}所需的菜<br/>";
	echo "洗{
      
      $name}菜<br/>";
	echo "炒{
      
      $name}菜<br/>";
	echo "{
      
      $name}上桌<br/>";
}
cooking("回锅肉");	 
?>

Running result:
Insert picture description here
custom function with parameters and return value

<?php
   function  add($a,$b)
   {
    
    
      return $a + $b;
   }

   $c = add(5,10);
   echo $c;	 
?>

Running result:
Insert picture description here
PHP recursive function

<?php
 function test(){
    
     
    static $dig=0; 
    if($dig++<10){
    
     
    echo $dig;  test(); 
   }
}
test();
?>

Running result:
Insert picture description here
factorial code:

<?php
//实现阶乘
function  leiCheng($n)
{
    
    
     If($n <=1){
    
    
        return $n; 
} else {
    
    
   return  $n * leiCheng($n-1);
}
}
echo leiCheng(10);
?>

Operating results:
Insert picture description here
Finally: 180 commonly used functions of PHP are attached: https://www.jb51.net/article/101179.htm

Guess you like

Origin blog.csdn.net/qq_44023710/article/details/113173544