20141223--Global Use local variable + static variable-01

Source: Internet
Author: User

Review

Operators: Arithmetic, comparison, logic, assignment, Trinocular, error suppression, bitwise arithmetic (three yards), self-operation, string

Execution structure: Order, branch (if and switch), loop (For,while,do-while,foreach)

Functions: Custom functions (function name, parameter (formal parameter, argument), function body, return value) Custom function return value

The return value refers to when the function call ends, the function returns some column data to the external call (data type any: eight data types)

If the function does not have any return values, it can be understood that the function returns NULL.

Functions in PHP can have no return value.

Parameter Passing value

Pass-through: value passing (default) and reference passing

Value passing: Copies a copy of the argument, passes it to the formal parameter (the shape participates in the external real parametric has no relation), the value passing can use the data constant directly when the function is called.

Reference passing: The formal parameter is the memory address of the data that the argument points to, and the form participates in the argument to point to the same memory address. Reference delivery

Reference passing syntax: When defining a function, use the address symbol & for the parameter, and when invoking a function that refers to passing, you must use the variable (the argument must be a variable: cannot be a data constant) to pass.

Reference passing cannot use data constants

Memory analysis

Special Note: Reference values can only be used to transmit data using variables. Parametric action

Passing data outside the function to the inside of the function for use scope scope concepts

1. What is a scope?

Scope refers to the range of variables that can be used.

Scopes fall into two categories: global scope and local scope.

2. Global scope and local scope definition?

Global scope

As long as the variables defined outside the function are global scopes

Local scope

Variables defined inside a function are called local scopes

3. Global scope and local scope capabilities?

Local scope variables can only be used inside a function and cannot be used outside the function.

Global scope:

JS: Variables of global scope can be used both inside and outside the function

In PHP: Global scopes can only use scope proofs outside the function

Using variables across domains

To use a global variable inside a function, use a local variable outside the function. Local use global scope

Programme one:

Let the function use the reference value at the time of definition.

Scenario Two:

Use the system pre-defined Hyper-global variable $globals, which holds all global variables.

Global use local scope

Programme I

Let the function use the reference value at the time of definition.

Programme II

Return value: The internal data (local variable) as the return value.

Programme III

Use the Global keyword to globally (create a global variable while creating a local variable: the current global variable does not exist and a reference is generated if it exists)

Referencing global variables

Creating global Variables

Memory Analysis 1: Referencing global variables

Memory Analysis 2: Creating global variables

Parameter Default value

When defining a function, specify a default value for the parameter and, when making a function call, you can not pass arguments to parameters that have default values.

Syntax: formal parameter = default value

Note: If a parameter with a default value exists in a parameter list, the parameter must be on the far Right (later)

static variables

1. What are static variables?

Using the static keyword-decorated variable inside the function, the function can use the variable in conjunction with multiple invocations (shared variables across functions)

Syntax: static $ variable

Memory analysis

anonymous functions

Functions that do not have a function name are called anonymous functions.

Grammar

variable = function (argument list) {

function body

return value

}; The whole is an assignment statement, so a statement terminator is required

Use of anonymous functions

Because the variable holds the address of the function, the function must be found through a variable. To recognize the function, only the parentheses, you need to add a parenthesis to the variable to tell the system to treat as a function.

Use of anonymous functions

Anonymous functions are often used to do other function parameters: callback function pseudo type

Refers to a defined function that requires the specified data type as a parameter or return value, which is a help to the user.

Mixed: There are many kinds of data types, not specific restrictions

Number: Numeric type: integer and floating-point

Callback: A callback function that refers to a string of names that require a function that can be executed.

Void:null, no parameter required or no return value variable function

Functions that can be changed, the function name is the value of a variable, you can directly access the function through the variable.

20141223--Global Use local variable + static variable-01

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.