Home > Backend Development > PHP Problem > How to call functions in php?

How to call functions in php?

(*-*)浩
Release: 2023-02-24 12:58:01
Original
8010 people have browsed it

How to call functions in php?

PHP function calling is the same as Java/C/C, just pass the function name (parameter list).

As shown in the picture: (Recommended learning: PHP programming from entry to proficiency)

How to call functions in php?

There are two places in the picture Function calling, session_start() at the beginning is the PHP function called, validate_user($username, $password) is a user-defined function, called in the same way.

Many ways to call PHP functions

<?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;<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&#39;;
	print_r($user);
	echo &#39;
Copy after login
Copy after login
'; 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;
Copy after login
'; ?>

The above is the detailed content of How to call functions in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template