Php function basic syntax custom function


PHP provides powerful functions, but this is far from enough, and programmers can create functions themselves as needed. This section begins to learn how to create functions.

In the actual development process, we need a lot of functions that need to be used repeatedly. These functions need to be used repeatedly. We can define functions (functions) and define them as functions (functions) as much as possible. When you use it, just click on its name.

Then let's learn the syntax of the custom function. The syntax is as follows:

<?php
Function name of the function ([parameter name 1[=value 1], parameter name 2[=value 2], parameter name n[=value n]])
{
       Functional body in function
    [return return value]
}

?>
 
The following characteristics were found in the above grammar rules, and the following words were not specified:

  1.  The function starts with function

  2. function followed by a space, followed by a function name.

  3. The function name is basically the same as the variable naming rule, but the difference is: the function name is not case sensitive.

  4. The so-called parameter is actually a variable

  5. The function name is followed by parentheses, the parameters are enclosed in parentheses, and the parameters are all enclosed in [] (brackets), which means the parameters can be filled or not.

  6. If there are parameters, the parameter can be followed by the (=) equal sign, and the equal sign is connected to the default value. Parameter values ​​are also enclosed in [] (brackets), representing optional

  7. After the function parameter variable, the main function is to pass the variable value outside the function to the function body. The variables of the function body and the variables outside the function are usually two different variables.

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

  9.  Functions can have a return value or no return value, enclosed in [] (brackets), representing optional.

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

  11. The execution of the function has no order relationship and can be called at the position before the definition.

  12. The function cannot be defined twice, ie the function cannot be overloaded
 Remember: you have to write more code to experiment!

We can prove these points little by little through experiments.

1. The function is a dog, where to call where, it will follow you



<?php
Function php_cn(){

   Echo 'I am a dog';
}

Php_cn();
Php_cn();
Php_cn();
?>
Php_cn this function, is not shown in three paragraphs: I am a dog?

3. The function name is the same as the variable naming rule, but the difference is: the function name is not case sensitive.



<?php
Function Demo(){

   Echo 'If it is a man who writes code, we need to take more responsibility from the family
'
; } Demo(); Demo(); DEMO(); ?>
Through the above code, you will find three lines: If you are the guy who writes the code, we need to take more responsibility from the family 

Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :