PHP functionsLOGIN

PHP functions

We have used a large number of functions in previous studies: var_dump and many array functions. What are their characteristics?

1. All can perform some specific functions

2. All have special names

3. All have more or less parameters

4. Most functions will have a return value, and even if they do not, they will perform specific operations

5. There is no need to know the internal implementation logic of the function

Functions exist in most programming languages. They are used to separate code that performs independent, well-defined tasks.

The function name must start with a letter or underscore, followed by letters, numbers, or underscores. In addition, the function name is case-insensitive

The real power of PHP comes from its functions.

In PHP, more than 1000 built-in functions are provided.

PHP Built-in Functions

For a complete reference manual and examples of all array functions, please visit our PHP Reference Manual.

PHP functions

What we called before were all PHP built-in functions, most of which are basic functions; however, in real projects, Can't meet our needs. Therefore, we are not limited to PHP built-in functions, we can write our own functions to accomplish any task.

In this chapter, we will explain how to create your own functions.

To execute a script when the page loads, you can put it in a function.

Functions are executed by calling functions.

You can call functions anywhere on the page.

Creating PHP functions

Functions are executed by calling functions.

function function name ([parameter name1[=value1], parameter name2[=value2], parameter namen[=valuen]])
{
Function Function body (code/statement)
[return return value]
}

Description:

1. The function starts with function

2. Function is followed by a space, and the space is followed by the function name

3. The naming rules for function names and variables are basically the same, but the difference is: the function names are not distinguished Uppercase and lowercase

4. The so-called parameters are actually variables

5. The function name is followed by parentheses, and the parameters are enclosed in the parentheses. All parameters are enclosed by [] (square brackets), which means that the parameters can It is optional to fill in

6. For parameters, you can follow the parameter with an equal sign (=), and the equal sign is followed by the default value. Parameter values ​​are also enclosed in [] (square brackets), indicating optional

7. The main function of the parameter variables after the function is to pass the variable values ​​outside the function into the function body for use. The function body The variable outside the function and the variable outside the function are usually two different variables.

8. The specific function (function body) in the function is enclosed in curly brackets, which means that this is the function range of a function.

9. A function can have a return value or not. Those enclosed by [] (square brackets) represent optional fields.

10. Return is followed by a space, and a space is followed by the return value. If there is a return, the code after the return will not be executed.

11. There is no order relationship in the execution of functions. They can be called before the definition.

12. A function cannot be defined twice, that is, the function cannot be overloaded.

Simple example :

A simple function that, when called, prints my name:

<html>
<body>
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
echo "My name is ";
writeName();
?>
</body>
</html>

Output:

My name is Kai Jim Refsnes

PHP Function - Add Parameters

In order to add more functions to the function, we can add parameters. Parameters are like variables.

The parameters are specified in parentheses after the function name.

Example 1

The following example will output different first names, but the same last name:

<html>
<body>
<?php
function writeName($fname)
{
echo $fname . "Refsnes.<br>";
}
echo "My name is";
writeName("Kai Jim");
echo "My sister's name is";
writeName("Hege");
echo "My brother's name is";
writeName("Stale");
?>
</body>
</html>

Output:

My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.

Example 2

The following function has two parameters:

<html>
<body>
<?php
function writeName($fname,$punctuation)
{
echo $fname . "Refsnes" . $punctuation . "<br>";
}
echo "My name is";
writeName("Kai Jim",".");
echo "My sister's name is";
writeName("Hege","!");
echo "My brother's name is";
writeName("Ståle","?");
?>
</body>
</html>

Output:

My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes!
My brother's name is Ståle Refsnes?

PHP Function - Return value

If you want the function to return a value, please use the return statement.

Example

<html>
<body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 =" . add(1,16);
?>
</body>
</html>

Output:

1 + 16 = 17

Default value of function

Looking back at the syntax of the function, just make a slight modification, add an equal sign after the second parameter, and assign a value, then parameter 2 will have a default value, and only one parameter 1 needs to be passed when calling the function

function function name ([parameter 1, parameter 2 = 2)
{
Function body (code/statement) in the function
[return return value]
}

Function:

Suppose there is a system function that has been used for a long time and is called everywhere. If you need to add new logic, you will most likely need to add a new function to it. parameters. If a new parameter is added and the function definition changes, we need to find each call place to modify and add this parameter. What if we give a default value? The default value is used to control the closing of new logic, so that the original code does not need to be modified and the new logic will not be executed, while the new code can open the new logic by specifying parameters explicitly.

Example

<html>
<body>
<?php
function add($x,$y = 10)
{
$total=$x+$y;
return $total;
}
echo "1 + 10 =" . add(1);
?>
</body>
</html>

As can be seen from the above example, if the parameter after the function has a default value, the parameter represented by the default value does not need to be passed in.
For example: echo "1 + 10 = ". add(1) only passes a value 1 to $x.


Next Section
<html> <body> <?php function writeName() { echo "Kai Jim Refsnes"; } echo "My name is "; writeName(); ?> </body> </html>
submitReset Code
ChapterCourseware