php如何调用函数?

(*-*)浩
(*-*)浩asal
2019-09-19 13:20:358036semak imbas

php如何调用函数?

PHP函数调用与Java/C/C++是一样的额,通过函数名(参数列表)就可以了。

如图:(推荐学习:PHP编程从入门到精通

php-154.png

图中,有两处函数调用,一开始的session_start()是调用的PHP函数,validate_user($username, $password)是用户自定义函数,同样的方式调用。

PHP函数调用的多种方式

<?php
	// 最常见的函数调用 --- 1
	function userFunction1($param1, $param2){
		echo &#39;UserFunction1: Param1 : &#39;,$param1,&#39; , Param2 : &#39;,$param2,&#39;<br/>&#39;;
	}
	userFunction1(&#39;Hello&#39;,&#39;world&#39;);
 
	// 最常见的函数调用 --- 2
	$userFunction2 = function($param1, $param2){
		echo &#39;UserFunction2: Param1 : &#39;,$param1,&#39; , Param2 : &#39;,$param2,&#39;<br/>&#39;;
	};
	$userFunction2(&#39;Hello&#39;, &#39;PHP&#39;);
 
	// 作为回调函数的函数调用 --- 1
	function funcWithCallback1($callback, $param1, $param2){
		echo &#39;funcWithCallback1 : &#39;;
		if(is_callable($callback)) $callback($param1, $param2);
	}
	funcWithCallback1($userFunction2,&#39;Hello&#39;,&#39;world&#39;);
 
	// 作为回调函数的函数调用 --- 2 call_user_func
	function funcWithCallback2($callback, $param1, $param2){
		echo &#39;funcWithCallback2 : &#39;;
		if(is_callable($callback)) call_user_func($callback, $param1, $param2);
	}
	funcWithCallback2($userFunction2,&#39;Hello&#39;,&#39;world&#39;);
 
	// 一个函数调用示例: 以 用户注册,修改密码为例
	$function_pool = array(
		&#39;regist&#39; => array(
			&#39;validateUsername&#39; => function($username){
				return (strtolower($username) !== &#39;admin&#39;) ? true : false;
			},
			&#39;addUser&#39; => function($username, $password){
				$user[$username] = $password;
				echo &#39;Add user Ok&#39;;
				return $user;
			}
		),
		&#39;updatePass&#39; => array(
			&#39;validatePassword&#39; => function($username, $password, $user){
				return (is_array($user) && array_key_exists($username, $user) && $user[$username] == $password);
			},
			&#39;setPassword&#39; => function($username, $password, $user){
				$user[$username] = $password;
				return $user;
			}
		)
	);
 
	function regist($function_pool, $username, $password){
		if(isset($function_pool) && isset($function_pool[&#39;regist&#39;])){
			if(isset($function_pool[&#39;regist&#39;][&#39;validateUsername&#39;]) && $function_pool[&#39;regist&#39;][&#39;validateUsername&#39;]($username)){
				return $function_pool[&#39;regist&#39;][&#39;addUser&#39;]($username, $password);
			}else{
				return &#39;User exists or Uniligel &#39;;
			}
		}
	}
	$user = regist($function_pool, &#39;Guojunzhou&#39;, &#39;sanyue&#39;);
	echo &#39;<pre class="brush:php;toolbar:false">&#39;;
	print_r($user);
	echo &#39;
'; function updatePass($function_pool, $username, $password, $newPass, $user){ if(isset($function_pool) && isset($function_pool['updatePass'])){ if(isset($function_pool['updatePass']['validatePassword']) && $function_pool['updatePass']['validatePassword']($username, $password, $user)){ return $function_pool['updatePass']['setPassword']($username, $newPass, $user); } } } $user = updatePass($function_pool, 'Guojunzhou', 'sanyue', '123456', $user); echo '
&#39;;
	print_r($user);
	echo &#39;
'; ?>

Atas ialah kandungan terperinci php如何调用函数?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php如何跳转页面Artikel seterusnya:php如何调用ffmpeg