PHP is a computer programming language, mainly used in the development of Web applications (website construction, etc.), this series of blogs from the basic SYNTAX of PHP, introduced the basic knowledge of PHP. Make readers learn the fun of programming in a simple way.

This blog series will cover variables, constants, data types, operators, arrays, flow control (order, selection, loop), functions, file handling, and object orientation

Each article will leave a homework at the end of the article, please talk to me for the answer, we can actively discuss in the comments section, common progress

concept

No matter what programming language you learn, functions are an essential point. Why is the function so important? This is because using functions can reduce code redundancy and improve code reuse. Greatly increase the efficiency of programming. So we must study it seriously.

First of all, what is a function? A function encapsulates a related block of code and gives it a special name. Elsewhere, we just call it by its name.

For example, when we go to a restaurant to eat, we need to order first, such as ordering a shredded pork with green pepper. The boss only needs to shout shredded pork with green pepper to the kitchen, and he doesn’t care about the details. After hearing shredded pork with green pepper, the kitchen will complete the operation of cutting vegetables, preparing food materials, frying vegetables, loading dishes and serving dishes.

In this case, the actions of the kitchen are the body of the function (i.e. a related block of code), the name of the function is shredded green pepper, and the boss is calling the function when he calls shredded green pepper.


      
/** * Created by Lengyue xiaobai */
/** ** Green pepper meat (function) */
function qingjiaorousi() 
{
    printf("Cutting");
    echo "<br />";
    printf("Prepare the ingredients.");
    echo "<br />";
    printf("Cooking");
    echo "<br />";
    printf("Plate");
    echo "<br />";
    printf("Serving");
    echo "<br />";
}
qingjiaorousi(); // Call the function
Copy the code

Function format

Function name (parameter) {function body return xx; // Return value}

Note: 1, declare the function must start with funtion, the function name is generally not repeated; The return value must be unique if it has a parameter.

The code field

Write a function of a+b that takes the return value in a variable and prints it. Please do it in code.


      
/** * Created by Lengyue xiaobai */

//add函数功能为a+b
function add($a.$b)
{
    $sum = $a + $b;
    return $sum;
}

$c = add(10 , 20);
echo $c;
Copy the code

Execution Result:

Practice after class

1. What is the function? How do I declare functions?

2, write a function a-b, and use a variable to receive the return value and output. Please do it in code.

After class practice answers, through wechat search a search “senior lengyue” reply PHP to get the article continues to update, this article GitHub github.com/lengyueit/p… Welcome Star.

If this blog helps you, you can help lengyue click a like or add a concern oh!