PHP Learning Note--5. Functions

Source: Internet
Author: User

Directory:

PHP Learning note--1. variables

PHP Learning note--2. Constants

PHP Learning note--3. operator

PHP Learning Note--4. Control structure

PHP Learning Note--5. Functions

1. Concept

From a programmatic point of view,

Wrap several statements together and take a name

Then called by name, which is the function

2. Declaration and invocation

Function ABC (formal parameter) {

...

return value;

}

ABC (actual parameter);

In fact, the parameter is the variable name, and the argument is the variable value

3. Naming conventions

Consistent with variables, but not case-sensitive

4. Default parameters

Function ABC (Formal parameter = ' default value ') {

...

}

ABC (); = = Default Value

ABC (actual parameter); = = Incoming Value

Note: If you have more than one parameter and one of the parameters has a default value, place it in the last

5. Scope of functions

Local variables: Variables inside a function

Global variables: Variables outside the function

By default, variables in the body of a function are independent of external variables

Note: The global variable in JS will be found inside the function call, but PHP will not

Execution function, will open up a separate call space, after the call is released

6. Execution of function and return of execution right

When the function is called, the execution of the program goes inside the function

When the statement finishes executing the/return, the execution right is returned

7. function return value

Return value returns if there are return values

return NULL if no return value or return is NULL

Note: Return returns only one value/compound value

8. Dynamic functions

function Wel () {

echo ' welcome ';

}

function Hi () {

echo ' Hello ';

}

$func =$_get[' func ']; <=wel/hi

$func (); = Welcome/Hello

Plus (), PHP will parse the value before the parentheses as a function

9. Referring to the parameters

$age = 19;

function foo (& $num) {

$num +=5;

}

Foo ($age);

Echo $age; =>24

Note: Instead of $age's value of 19, the $age address is passed to $num,

When $num+=5, $age naturally changed.

Therefore, local variables and global variables are not incompatible.

Have the opportunity to interact with each other, such as referencing a parameter, a super global variable

10. Recursion

① definition

function calls itself inside the

② First Knowledge

Example: for 1 to 100 and

Possible analysis points:

SUM (+) = = SUM (99) + 100

SUM (in) = = SUM (98) + 99

...

SUM (2) = = SUM (1) +2

SUM (1) = = 1

That

function sum ($n) {
if ($n > 1) {
return sum ($n-1) + $n;
}else{
return 1;
}
}

Example: recursive operation linked-level directory

a{1,2 (A,b<①②③>,c), 3}

The order of operation is 1 2 A b①②③c 3

③ Note

Because recursion calls itself for itself, so the level is too deep, not suitable for use

Must have a termination condition

11. Super Global variables

① definition

In general, the ' normal global variables ' declared inside the page are not accessible within the function

But: ' constant ' within the function to access

And there's a class of variables--super-global variables, anywhere in the page (whether it's a deep-seated function),

have direct access to

② Nine Super Global variables

$_get[]=> Receive Address bar parameters

$_post[]=> receive POST parameters (e.g., user registration parameters)

$_peqvest[]=> is the $_get, $_post, and $_cookie

$_files[]=> File Upload

$_cookie[]=> User Login

$_session[]=> User Login

$_env[]

$_server[]=> Server and visitor information (such as the system version, IP address, etc.) that are used by the user

$_server[' REMOTE_ADRR ']=> User IP

$_server[' http_user_agent ']=> user browser information

$_globals[]=> ' list ' of all global variables within the page, where all declared variables can be found

Global keyword, which is used to refer to variables inside the function body

Example: $age = 5;

function T () {

Global $age;=> refers to $age in the world

$age + +;

}

T ();

echo $age; =>6

@zhnoah
Source: http://www.cnblogs.com/zhnoah/
This article is owned by me and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original
The right to pursue legal liability.

PHP Learning Note--5. Functions

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: info-contact@alibabacloud.com 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.