PHP function

PHP Functions

#The real power of PHP comes from its functions.

What is a function

We have used a large number of functions in previous studies: var_dump, sprintf and There are so many array functions, what are their characteristics?

•                                                                                                       out out out out out out of 2000             out out out ‐ ‐   out out ‐   out ‐   Through use out through through through ‐ , and with -                             out out out of Functions will have a return value, and even if they do not, they will perform specific operations.

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

Functions exist in most programming languages, and they are used to separate Code that performs independent and 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

What are function parameters

We can think of the function as a juicer, the parameters are like the fruits we put in, and the code executed in the function is like the stirring of the blenderWith different parameters, the results returned by the function are also different, just like apple juice will not come out after adding oranges. Functions can also accept multiple parameters, just like mixed juiceThe function only leaves us with the function name to call, and we don’t need to know how the code in the function is defined. Similarly, we don’t need to know how to squeeze How the juicer works

PHP built-in functions

In PHP, more than 1,000 built-in functions are provided. built function. For a complete reference manual and examples of all array functions, please visit our PHP Reference Manual.

PHP Function

In this chapter, we will explain how to create your own The function. 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. Syntaxfunction function name (parameter 1, parameter 2, parameter 3...) {

Code/statement;

return return value;

}

PHP functions do not necessarily have to explicitly retain the return statement. If there is no return, the function will automatically return null

PHP function guidelines:

· The name of the function should indicate its function

· The function name starts with a letter or underscore (cannot start with a number)

Example

A simple function. My name can be output when calling:

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

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>


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>


##PHP Function - Return Value

If you need 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>

Custom function

What we called before PHP built-in functions. However, the real function of a programming language is achieved by creating your own functions

Most of the functions provided by PHP are basic functions, such as operating arrays, accessing databases, reading and writing files, etc.

But , In real projects, PHP functions cannot meet our needs. We can't find functions that can be used to process contracts, no functions to calculate attendance rates, and no functions to print data tables

Fortunately, We are not limited to PHP built-in functions, we can write our own functions to complete any task, this is a custom function. You can use PHP's built-in functions in custom functions. Let's learn about the basic structure of the function.

One-time function

In computers, functions were first used to simulate various mathematical functions. Take a look at the following code

function linear($x) {

$k = 5 ;
$b = 3;
$y = $k * $x + $b;
return $y;
}

$value = 3;

echo linear($value);

This function can solve linear equations

• The function is a black box, and any variables inside linear cannot be accessed from the outside

• Similarly, the external variables cannot be accessed inside the function.

#The default value of the 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 has a default value, and only one parameter needs to be passed when calling the function

function function name (parameter 1, parameter 2 = 1) {             Code/statement;          return return value;

}

Function name (1); at this time, the value of parameter 2 in the function body is 1 (default value)

Function name (1, 2); At this time, the value of parameter 2 in the function body is 2 (the value passed)

The role of default value

In actual development, we often use it, why?

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 parameter to it

If A new parameter has been added. At this time, the definition of the function has changed. We need to find the place where each call is made to modify and add this parameter.

What if a default value is given? 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. The new code can open the new logic by specifying parameters.


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