Basic functions of PHP development tutorial
1. Overview of PHP functions
The real power of PHP comes from its functions.
In PHP, more than 1000 built-in functions are provided.
Function is function. Calling a function is calling a function.
2. PHP built-in functions
The date() and rand() functions we used earlier are all built-in functions of the system. Details When you use it in the future, please check the PHP manual. There are detailed examples above. In the advanced tutorials that follow, there will also be an introduction to commonly used functions. In this chapter, our focus is on how to create our own functions.
3. PHP custom function
1. Create a function
Syntax:
function functionName()
{
Code to be executed;
}
PHP function guidelines:
The name of the function should hint at its function
The function name starts with a letter or underscore (not a number)
Note : There is no semicolon after the curly braces
2. Call the function
Syntax:
functionName;
Write Function names can be separated by semicolons
A simple example: the code is as follows
<?php //定義一個(gè)輸出名字的函數(shù) function myName(){ echo "小明"; } echo "大家好,我叫"; //調(diào)用函數(shù) myName(); ?>
##4. Add parameters to the custom function
<?php //定義一個(gè)個(gè)人信息的函數(shù) function information($name,$age){ echo "大家好,我叫".$name.",今年已經(jīng)".$age."歲了。"; } //調(diào)用函數(shù),帶兩個(gè)參數(shù) information("小明",18); ?>This method of adding parameters will be very common in our future study and work
5. The return value of the function
<?php //定義函數(shù),傳入?yún)?shù),計(jì)算兩個(gè)參數(shù)之和,并且返回出該值 function add($x,$y) { $total=$x+$y; return $total; } //調(diào)用該函數(shù) echo "1 + 18 = " . add(1,18); ?>