亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP function basic syntax custom function

PHP provides powerful functions, but this is far from meeting the needs. Programmers can create functions by themselves as needed. In this section, we begin to learn how to create functions.

In the actual development process, we need to have many functions that need to be used repeatedly. If we can define these functions that need to be used repeatedly, we can define them as functions (functions) as much as possible ( function). When using it, just shout its name.

Then let’s learn the grammatical provisions of custom functions. The grammatical provisions are as follows:

function 函數(shù)名([參數(shù)名1[=值1], 參數(shù)名2[=值2], 參數(shù)名n[=值n]])
{
       函數(shù)中的功能體
    [return 返回值]
}

The following characteristics are found in the above grammatical provisions, resulting in the following unspecified expression:

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: function name It is not case sensitive

4. The so-called parameters are actually variables

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

6. If there are parameters, the parameters can be followed by 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 variables and the variables outside the function are usually two different variables.

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

9. A function can have a return value or no return value. 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

Remember: You also need to write more code to conduct experiments!

We can prove these points bit by bit through experiments.

1. The function is like a dog. Wherever it is called, it will follow you.

<?php

function php_cn(){

   echo '我是一條狗';
}

php_cn();
php_cn();
php_cn();
?>

php_cnDoes this function display three paragraphs: I am A dog?

2 Function name can only be a combination of letters, numbers, and underscores, and cannot contain spaces. Numbers cannot be placed at the beginning of the variable name.

<?php
function 1demo(){
}
?>

The above code will report an error. Function naming and variable naming

3. Function names and variable naming rules are the same, but the difference is: function names are not case-sensitive

<?php

function Demo(){

   echo '如果是寫代碼的男人,我們需要更加承擔(dān)來自家庭的責(zé)任<br />';
}

demo();
Demo();
DEMO();

?>

Through the above code, you will find that three lines will be displayed: If it is a man who writes the code, we need to take more responsibility from the family
.
That is, the function name is not case-sensitive when called. A function can have its name called repeatedly and be executed repeatedly. It also reflects the characteristics of reuse.

4. If the parameters of the function body are defined and no parameters are passed, the code will report an error

<?php

//定義函數(shù)名為test,必須要傳入一個參數(shù)
function test($hello){


}

test();
?>

Write a paragraph yourself and try it. Will it report an error?

5. If the parameters after the function have default values, the parameters do not need to be filled in, and the code will not report an error.

<?php
function test( $arg = 10){

       echo $arg;

}

test();

test(88);

?>

Is there no error reported? And it showed 10 and came out.
Note that if parameters are passed, they will be brought into the function. If the function does not pass parameters, the default value after the parameters will be used.

6. You can write multiple parameters after the function

<?php
function test( $a , $b = 20 , $c = 30){

       echo $a + $b + $c;

}

test( 1 , 2 , 3 );
?>

Write the above code yourself. Have we passed in multiple parameters?

7. If there are parameters with default values ??and no default values ??after the function, the parameters without default values ??are usually written in the first 3rd and 3rd numbers before

From 4 experiments, we found that the default value means that this parameter does not need to be passed in. If there is no default value, the code will report an error, that is, parameters without default values ??must be passed. Let's take a look at the following example:

<?php
function test( $a = 20 , $b = 30 , $c ){

       echo $a + $b + $c;

}

//重點:重點看這一行執(zhí)行一下
test(  ,  , 8 );
?>

Through the above example, we found that another error was reported when executing the above code. That is to say, the above syntax will not work.
What we hope in the above syntax is that we do not pass in any values ??for parameter $a and parameter $b. The parameter $c must be passed in, we passed 8. But PHP's grammatical regulations do not allow us to write this. Therefore, we can achieve the same effect by changing the way of writing:

<?php
function test( $c , $a = 20 , $b = 30){

       echo $a + $b + $c;

}

//重點:重點看這一行執(zhí)行一下
test( 8 );
?>

Through the above example experiment, did you find that I passed the parameter $c and the code did not report an error. The parameter $a = 20 and the parameter $b = 30 are also brought into the code $a + $b + $c.

8. There is no relationship between the variables in the function body and the variables outside the function

<?php
//定義變量$hello的值為10
$hello = 10;


//函數(shù)后的參數(shù)(形式上的參數(shù),參數(shù))處寫上變量名為$hello
function demo( $hello ){

   //形參帶入了函數(shù)體內(nèi),函數(shù)體內(nèi)變量$hello 改為了 250
   $hello = 250;

   //輸入2個250相加的結(jié)果
   echo $hello + $hello;

}


//將函數(shù)體外的變量$hello,傳入變量的參數(shù)處(實際傳入的參數(shù),實參),顯示出的結(jié)果為500
demo($hello);

//此處$hello的值輸出,依然為10
echo $hello;
?>

Through the above example, we found that there is no change point relationship between the formal parameters and the actual parameters. No matter how the formal parameters passed in the function body change, it will not affect the actual result of the actual parameter $hello.

9. If there is a return in the function body, the code after the return will not be executed.

<?php

function demo(){

   echo 111;

   return;

   echo 222;

}

demo();
?>

Have you noticed? ——Only 111 is output.

10. After the function is executed, return can bring the value in the function body out of the function body

<?php

//定義一條函數(shù)狗
function php_cn(){

   $foo = 5;

   $bar = 6;

   $result =  $foo + $bar;
   //將$result的結(jié)果進(jìn)行返回
   return $result;

}

//調(diào)用php_cn()這個函數(shù),$foo和$bar相加的$result就會返回回來給到變量$piao
$piao = php_cn();

//輸出$piao的結(jié)果,果真為11
echo $piao;

?>

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

<?php

demo();
function demo(){

   $str = '爸爸媽媽年齡越來越大';
   $str .= '大多數(shù)的孩子都是獨生子女,更加應(yīng)該負(fù)起責(zé)任';

   echo $str;
}

demo();
?>

Through the above example, we found that the function can be called before or after the definition. That is, the function can be called anywhere.

11. Functions cannot be overloaded

<?php

function demo(){

}

function demo(){

}
//試試會報錯嗎?
?>

It is found through the above example that an error is reported, that is, the function with the same name cannot be defined twice, otherwise an error is reported


Assignment:

1. Define a double-layered loop with alternating rows of color-changing tables

2. Require this form, the default values ??are 10 and 10, one of the colors is a default parameter, and one has no default parameters

3. Return the string of the table back


Continuing Learning
||
<?php //定義一條函數(shù)狗 function php_cn(){ $foo = 5; $bar = 6; $result = $foo + $bar; //將$result的結(jié)果進(jìn)行返回 return $result; } //調(diào)用php_cn()這個函數(shù),$foo和$bar相加的$result就會返回回來給到變量$piao $piao = php_cn(); //輸出$piao的結(jié)果,果真為11 echo $piao; ?>
submitReset Code