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
//定义一个输出名字的函数
function myName(){
    echo "小明";    
}
echo "大家好,我叫";
//调用函数
myName();
?>


##4. Add parameters to the custom function

When calling a custom function, pass in point information to the function.

Example: The code is as follows

<?php
//定义一个个人信息的函数
function information($name,$age){
    echo "大家好,我叫".$name.",今年已经".$age."岁了。";
}
//调用函数,带两个参数
information("小明",18);
?>

This method of adding parameters will be very common in our future study and work


5. The return value of the function

If you need to let the function return A value, use the return statement.

Example, the code is as follows

<?php
//定义函数,传入参数,计算两个参数之和,并且返回出该值
function add($x,$y)
{
    $total=$x+$y;
    return $total;
}
//调用该函数
echo "1 + 18 = " . add(1,18);
?>
Continuing Learning
||
<?php //定义一个输出名字的函数 function myName(){ echo "小明"; } echo "大家好,我叫"; //调用函数 myName(); ?>
submitReset Code