Php learning function courseware

Source: Internet
Author: User
Php function courseware materials. you can download the courseware and check the code for reuse.
Include ()
Require ()
These two functions are used to reference files. The difference is that when processing fails, include () generates a warning and require () is a dense error.
Include_once ()
Require_once ()
These two functions are the same as include () and require (). The difference is that include_once and require_once can only be referenced once.

Custom functions
UDFs are declared using function ().
Advantages of functions:
Complexity of control program design
Improve Software reliability
Improve software development efficiency
Improve software maintainability
Improve program reusability
Syntax format of the custom function:
Function name (parameter 1, parameter 2 ){
Program description;
Function name (parameter 1, parameter 2 );
Return value; // The return value can also be an expression.
User-defined function names are case-insensitive. You cannot use declared functions or PHP built-in function names when naming a function.
Determine whether a function exists: function_exists (function name );

Variable range
The visibility of variables refers to the scope of the variables in the program.
Generally, variables are declared as local variables and global variables.
Local variables:
The variable declared in the function is a local variable and can only be used within the function range. If other programs need to call the variable value locally, they must use the "return" command to send it back to the main program block for subsequent processing.
Global variables:
Variables declared outside the function scope are global variables. Because a function can be considered as a separate program segment, local variables overwrite the visibility of global variables. Therefore, global variables cannot be directly called in functions.
To use a global variable in a function, you must use the global keyword to define the target variable to tell the function subject that the variable is global.
You can also use the predefined global variable array $ GLOBALS. this is a special variable automatically created when the program is running.
Echo $ GLOBALS ["A"];
You can use unset ($ var) to manually delete a variable. the variable is released in the memory and is not in the global scope.
Using require and include does not affect the scope.

Static variables
Declare the function variable as static ).
A static variable is shared among all calls to this function, and is initialized only when the function is called for the first time during script execution. To declare a function variable as static, use the keyword static. Generally, an initial value is assigned to a static variable for the first time.

Parameter transfer
Pass parameters by value:
The parent program directly transmits the specified value or variable to the function. Because the passed values or variables are stored in different memory blocks from the values in the function, when the function changes the imported values, does not directly affect the parent program.
Pass parameters by address (implemented with the "&" symbol)
Compared to the value-based transmission mode, the specified value or target variable in the parent program is not passed to the function. Instead, the memory storage block relative address of the value or variable is imported into the function. Therefore, if the value changes in the function, it may affect the parent program.

Default parameters
Default parameters must be listed after all parameters without default values.
Function fun_sum ($ a, $ B = 0, $ c = 0 ){
Return $ a + $ B + $ c;
Echo fun_sum (10, 20 );
Echo fun_sum (10, 20, 30 );
0 is the default parameter

List of any number of parameters
Func_get_args () // returns an array containing all parameters
Func_num_args () // total number of returned parameters
Func_get_arg () // receives a numeric parameter and returns the value of the specified parameter in subscript search.
Function foo ()
$ Numargs = func_num_args ();
Echo "Number of arguments: $ numargs
\ N ";
If ($ numargs> = 2 ){
Echo "Second argument is:". func_get_arg (1 )."
\ N ";
$ Arg_list = func_get_args ();
For ($ I = 0; $ I <$ numargs; $ I ++ ){
Echo "Argument $ I is:". $ arg_list [$ I]."
\ N ";

Foo (1, 2, 3 );
Output result: Number of arguments: 3
Second argument is: 2
Argument 0 is: 1
Argument 1 is: 2
Argument 2 is: 3

Variable functions
This means that if a variable name has parentheses, PHP will look for a function with the same name as the value of the variable and will try to execute it. This can be used to implement callback functions, function tables, and so on.

Recursive call
The so-called recursive call of a function means that a function can call itself in the execution description of its declaration.
A condition judgment statement is usually attached to this type of function to determine whether recursive call is required and terminate the recursive call action of the function under specific conditions, return the main control right of the current process to the previous layer for function execution. Therefore, an infinite loop error may occur when a function that executes recursive calls has no additional conditions to judge the description.
The biggest benefit of function recursive calling is that it can streamline complicated and repetitive calling procedures in programs and execute complicated operations with this feature.
Phpchina courseware

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.