Home > Backend Development > PHP Tutorial > Benniao learns PHP (5) Declaration and use of functions_PHP tutorial

Benniao learns PHP (5) Declaration and use of functions_PHP tutorial

WBOY
Release: 2016-07-13 10:21:37
Original
1007 people have browsed it

Beniao learns php (5) declaration and use of functions

1. Function declaration

<?php

	/**
	 * function 函数名(参数1, 参数2.... ){
	 *  	函数体;
	 *  	返回值;
	 * }
	 */
	
	$sum = sum(3, 4);
	echo $sum;
	
	function sum($x, $y){
		$sum = 0;
		$sum = $x * $x + $y + $y;
		return $sum;
	}
?>
Copy after login

2. Scope of variables

Local variables: Variables declared in a function are local variables and can only be used within your own function .
Global variables: Variables declared outside a function are global variables. They can be used after the variable is declared until the end of the entire script, including in functions and { }.

2.1 Knowledge point: PHP variables cannot be distinguished whether they are declared or used

<?php
	$a = 10;
	function demo($a){
		$a += 10;
		echo $a . &#39;<br>&#39;;
	}
	demo($a);
	echo $a;
	// 结果: 20, 10
	// PHP的变量分不出 $a 是声明还是使用
?>
Copy after login
2.2 Knowledge point: When using global variables in a function, you must use the global keyword. The variable after declaring global is the global variable

<?php
	$a = 10;
	
	function demo() {
		global $a;
		$a += 10;
		echo $a . " demo <br>";
	}
	
	function test() {
		global $a;
		$a += 5;
		echo $a . "test <br>";
	}
	
	echo $a . " ----<br>";  // 10 ----
	demo();                 // 20 demo 
	echo $a . " !!!!<br>";  // 20 !!!!
	demo();                 // 30 demo 
	echo $a . " @@@@<br>";  // 30 @@@@
	test();                 // 35 test 
	echo $a . " ####<br>";  // 35 ####
?>
Copy after login

Static variables: can only be declared in functions (classes) and cannot be declared globally. Use the static keyword to modify before the variable.

2.3 Knowledge point: Static variables are stored in static code blocks. Its value can be shared between multiple calls of a function, but it is only declared to memory when the function is called for the first time

When called in the future, it will not be declared but used directly (similar to Java).

<?php
   function test(){
	   static $a = 0;
	   $a++;
	   echo $a."<br>";
   }

  test(); // 1
  test(); // 2
  test(); // 3
?>
Copy after login

3. Variable function

In fact, this is very similar to JS, for example: define $var = hello, the next time you use $var(), you will look for the function hello() with the same name as the variable value

<?php
	  function one($a, $b){
	  	return $a + $b;
	  }
	
	  function two($a, $b){
	 	return $a * $a + $b * $b; 
	  }
	
	  function three($a, $b){
	  	return $a * $a * $a + $b * $b * $b;
	  }
	
	  //$var = one;
	  //$var = "two";
	  $var = three;
	
	  echo "结果:".$var(3, 4)."<br>"; // 91
?>
Copy after login

4. System function

4.1 Regular functions
bool copy (string source, string dest)

4.2 With mixed, mixed means that any type of data can be transmitted
bool chown (string filename, mixed user)

4.3 Function with & parameter, indicating reference assignment. This parameter cannot pass a value, only a variable can be passed, and then the function changes the value of the variable,

When we use this variable, the value also changes (the same as passing references in Java)

bool arsort (array &array [, int sort_flags])
<?php
	$arr=array(1, 9, 5, 8, 3, 4);
	sort($arr);
	print_r($arr);
?>
Copy after login
4.4 Default function, a function with [], indicates that this parameter is optional. If you pass a value, the value you passed will be used. If no value is passed, the default value will be used
bool arsort (array &array [, int sort_flags])
<?php
	function demo($a=1, $b=20, $c){
		echo "### $a ### $b ### $c ###<br>";
	}	
	demo(8,9);
?>
Copy after login
4.5 Parameter function with..., ... means that any number of parameters can be passed
int array_unshift (array &array, mixed var [, mixed ...])

<?php
	function demo(){
		  $sum = 0;
		  for($i = 0; $i < func_num_args(); $i++){
		  	  $sum += func_get_arg($i);
		  }
		  return $sum;
	 }
	 
	echo demo(1, 2, 3, 4, 5, 6, 7, 8, 9);
?>
Copy after login

4.6 Callback function with callback, that is, when calling this function, we need to pass in a function (function name, function name string)
array array_filter (array input [, callback callback])

<?php
   function demo($n){
	   	if($n % 2== 0){
			return true;
	   	}else{
			return false;
	   	}
   }
   
   $a = array(1, 2, -3, 4, -5, 6, -7, 8, 9);
   print_r(array_filter($a, demo));  // Array ( [1] => 2 [3] => 4 [5] => 6 [7] => 8 ) 
?>
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/854421.htmlTechArticleStupid Bird Learning PHP (5) Declaration and Use of Functions 1. Declaration of functions 2. Scope of variables local variables : Variables declared in a function are local variables and can only be used within your own function...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template