search
HomeBackend DevelopmentPHP TutorialPHP Advanced Programming-Function-Zheng Aqi_PHP Tutorial
PHP Advanced Programming-Function-Zheng Aqi_PHP TutorialJul 21, 2016 pm 03:26 PM
functionphpcodefunctionnamecopyuserprogrammingcustomizeadvanced

1.php function
1. User-defined function

Copy code The code is as follows:

function function name ([ $parameter,[,…]])
{
//Function code
}

Note: The function name cannot have the same name as a system function or a function that has been defined by the user.
$parameter is a function parameter. A function generally can have 0 or more parameters.
2. Passing of parameters
Parameters are passed by value. For example, the func() function defined earlier is passed by the variable $ The values ​​of a and $b are passed. Passing parameters by value does not change the value outside the function because the parameter value inside the function changes.
Copy code The code is as follows:

function color(&$col) //Define function color()
{
$col="yellow";
}
$blue="blue";
color($blue); //Call function color(), parameters use Variable $blue
echo $blue; //Output "yellow"
?>

3. Scope of function variables
Variables defined in the main program and in the function The variables defined in are all local variables. Variables defined in a function can only be used inside the function. Variables
defined in the main program can only be used in the main program, not in functions.
Copy code The code is as follows:

function sum()
{
$count=2;
}
sum();
echo $count;
?>

Since the variables in the function cannot act outside the function, so An error occurred when running the above, prompting that the $count variable is undefined.
4. Return value of function
When a function is declared, using the return statement in the function code can immediately end the running of the function. When the program returns, the next statement of the function is called.
Copy code The code is as follows:

function my_function($a=1)
{
echo $a;
return; //End the running of the function, the following statements will not be executed
$a++;
echo $a;
}
my_function() ; //Output 1
?>

Interrupting functions is not a common function of the return statement. Many functions use the return statement to return a value to interact with the code that calls them. The return value of a function can be of any type, including list objects
5. Function call
can be called after the function is declared. In addition, if the function does not return value, just use the function name when calling. If a function has a return value, you can assign the function's return value to a variable.
Copy code The code is as follows:

//The function my_sort() that sorts an array in ascending order
function my_sort ($array)
{
for($i=0;$i{
for($j=$i+1;$j{
if($array[$i]>$array[$j])
{
$tmp=$array[$j];
$array[$j]=$array[$i];
$array[$i]=$tmp;
}
}
}
return $array;
}
$arr=array(6,4,7,5,9,2); //Unsorted array
$sort_arr=my_sort($arr); //Assign the sorted array Give $sort_arr
foreach($sort_arr as $num)
echo $num; //Output 245679
?>

6. Recursive function
php supports recursion Functions, recursive functions call themselves, which can achieve the effect of looping.
Ask for 10!
For example:
Copy code The code is as follows:

function factorial($n)
{
if($n==0)
return 1; //If $n is 0, return 1
else
return $n*factorial($n1); //Recursive call until $n equals 0}
echo factorial(10); //Output 3628800
?>

Use recursion - in fact, recursion termination is given condition, otherwise the function will continue to execute until the memory is exhausted or the maximum number of calls is reached.
When using recursion, you must actually provide a recursion termination condition, otherwise the function will continue to execute until the memory is exhausted, or the maximum number of calls is reached.
7. Variable function
PHP has the concept of function variable. Adding a pair of parentheses after the variable forms a variable function.
$count();
8. System function
9. Example - Design a calculator program
Copy code The code is as follows:



Calculator Program












function cac($a, $b, $caculate) //Define the cac function to calculate two numbers The result
{
if($caculate=="+") //If it is addition, the addition
return $a+$b;
if($caculate=="-") / /If it is subtraction
return $a-$b;
if($caculate=="*") //If it is multiplication, return product
return $a*$b;
if($caculate=="/")
{
if($b=="0") //Determine whether the divisor is 0
echo "The divisor cannot be equal to 0";
else
return $a/$b; //Divide if the divisor is not 0
}
}
if(isset($_POST['ok']))
{
$number1=$_POST['number1']; //Get number 1
$number2=$_POST['number2']; //Get number 2
$caculate=$_POST['caculate']; / /Get the operation action
//Call the is_numeric() function to determine whether the received string is a number
if(is_numeric($number1)&&is_numeric($number2))
{
//Call cac function calculation result
$answer=cac($number1,$number2,$caculate);
echo "<script>alert('".$number1.$caculate.$number2."=".$ answer."')</script>";
}
else
echo "<script>alert('The input is not a number! ')</script>";
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323878.htmlTechArticle1.php function 1. User-defined function copy code The code is as follows: function function name ([$parameter,[ ,…]]) { //Function code} Note: The function name cannot be the same as the system function or user-defined...
Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool