3.php Basics (Control statements, functions, array traversal)

Source: Internet
Author: User
Tags php basics variable scope

if condition judgment statement

structure One: only true, regardless of false

structure Two: Both Judge true, also Judge false(two choice one)

Structure three: multi-condition judgment

Switch Multi-branch structure

S Witch Syntax structure Description:

L Switch principle: According to the different values of a variable, compared with the value in each case, if the same, then the condition satisfies, executes the code.

The value of the L case , which can only be a constant, is a string, or an integer.

L Break terminates switch run and jumps to the end of curly braces.

L If the break statement is omitted, it will continue to run to the next case .

The default statement, which is a statement that executes on its own. If none of the above conditions are met, the last executed code.

If multiple conditions and switch differences

L IF is suitable for many conditions, or more complex conditions. Instead , the switch variable has only a few fixed values.

L IF The operation is less efficient. and switch efficiency is a bit higher.

while loop structure

Three elements of a cycle:

L variable initialization. Before the loop starts, the variable is initialized, executed only once, and once the loop starts, the initialization of the variable is no longer performed.

L condition judgment. if the condition is true, the loopstarts ( executing the Loop body Code )and thecondition is falseto exit the loop.

L variable Update. If there are no variable updates, a "dead loop" appears. Variables should be updated more and more closely to the while judgment condition.

for Loop

break Statement

meaning: Interrupted. In other words:break can be used to interrupt various loops, as well as switch statements.

in general, you need to add a condition before the break statement.

L Syntax: Break [n]

L parameter:n is an optional parameter. n indicates a loop that jumps out of the first layer. The default value is 1.

L Example:

U break; Jump out of the current loop

U break 1; Jump out of the current loop

U break 2; Jump out of a two-layer loop

U break 3; Jump out of a three-layer loop

continue Continue

L Meaning: Interrupt this loop ( the code that follows is no longer executed ), and start the next new loop.

L Syntax:continue [n]

L Example:

U continue; Abort this cycle

U continue 2; Jump out of a two-layer loop

copy value and reference pass value

1. Copy Transfer value

Pass the value of a variable,"copy" one copy, to another variable.

These two variables, which are not related to each other, are two variables independent of one another.

Modifies the value of one of the variables, and the other variable does not change.

in the in PHP, which data types default to "copy pass value"?

character, Integer, floating-point, NULL, array.

2. Referring to address

Pass the data address of one variable,copy it, to another variable.

These two variables point to the same thing.

If this "object" has changed, then, for the two variables, it has changed.

Modifies the value of one of the variables, and the other variable changes as well.

in the in PHP, the default "reference address" type is: Object and resource.

However, you can also manually declare "reference address", just before the referenced variable, add a "&" symbol.

In this way, all data types can implement a reference address.

PHP Arrays

1, the concept of arrays

An array is a collection of sets of numbers.

$arr = Array (10,20,30,40);

arrays are indexed by ( subscript ) finds an element.

2, the classification of the array

(1) enumerating arrays

The subscript of an array is a positive integer starting at 0, so that the array is called an "enumerated array."

$arr = Array (10,20,30,40);

$arr [0] = 10;

$arr [1] = 20;

(2) associative arrays

The subscript of an array is a string , which is called an associative array.

(3) mixed array

The so-called "mixed-array" is the array's subscript is both integer and string.

3. Creation of Arrays

(1) Create an array using the array () function

(2) using [] to create an array

Multidimensional Arrays

An array of elements, or an array. Array arrays.

array-related functions

1,print_r ()

L Description: Print easy-to-understand information about variables

L Syntax:bool Print_r (mixed $expression)

L Description: If a string,integer , or floatis given, the variable value itself will be printed. If an array is given , the keys and elements are displayed in a certain format. Object is similar to an array.

2,unset ()

L Description: Releases the given variable.

L Syntax:void unset (mixed $var [, mixed $ ...]

L Description:JS using The delete operator, delete variables and array elements.

3.count ()

L Description: Calculates the number of cells in an array or the number of attributes in an object

L Syntax:int count (mixed $var)

L Description: Only the number of valid data is counted.

foreach () can only be used to iterate over an array

function Parameters

1. Value passing Parameters

2. Reference passing Parameters

2.time()

Description: Return 1970 year 1 month 1 day 0 hours 0 minutes 0 Seconds , to the current number of seconds.

Variable Scope

in the JS , global variables, can be used directly inside the function.

in the in PHP, global variables cannot be used directly inside a function.

Global variable: A variable defined outside the function. The Web page execution is complete and disappears.

Local variable: A variable defined inside a function. The function finishes executing disappears.

Global Declaration Globals variable

using the global keyword, you can declare a variable as a " global variable." The "global variable" here is not a global variable that is considered in JS.

Global keyword usage Note

L Global keyword, can only be used inside the function.

L Global keyword, you cannot assign a value while declaring a global variable.

The real meaning of global is " reference to Address", and the global variable in JS.

3.php Basics (Control statements, functions, array traversal)

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.