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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
