PHP Study Notes (3)-operators and control structures

Source: Internet
Author: User

Introduction: This is Php study note (3)-a detailed page of operators and control structures. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 342969 'rolling = 'no'>

I haven't updated it for a long time. It's very busy.

The following describes the operators and control structures of PHP;

I. String insertion

To provide developers with the maximum flexibility to process string values, PHP provides both literal insertion and content insertion

A method.

Double quotation marks provide maximum flexibility because variables and transfer sequences are parsed accordingly.

<? PHP

$ Username = "James ";

Echo "His name is $ username ";

Echo "<br/> ";

// Some problems may occur in Chinese

Echo "His name is $ username. He is 19 years old and has grown up! ";

Echo "<br/> ";

// String connection is supported.

Echo "His name is". $ username. "He is 19 years old and has grown up! "

// Escape characters can be parsed

Echo "although he has many girls on QQ, \ n does not belong to him ";

?>

Some common escape characters

Escape Sequence description

\ N linefeed

\ R press ENTER

\ T horizontal tabulation chart

\ Backslash

\ $ Dollar sign

\ "Double quotation marks

Single quotes are interpreted as they are declared as they are. When a string is parsed, the variables and escape sequences are not parsed.

<? PHP

Echo 'invalid variable name: $ username, Escape Character \ n is invalid in single quotes'

?>

Ii. Operators

Operators are symbols used to perform some operation operations on arrays and variables.

Arithmetic Operators

Compound value assignment operator

Increment and decrease operators:

$ A = ++ $ B;

$ A = $ B ++;

$ A = -- $ B;

$ A = $ B --;

Comparison Operators

Operator name example

+ Add $ A + $ B

-Minus $ A-$ B

* Multiply by $ A * $ B

/Except $ A/$ B

% Remainder $ A % $ B

Operator usage is equivalent

+ = $ A + = $ B $ A = $ A + $ B

-= $ A-= $ B $ A = $ A-$ B

* = $ A * = $ B $ A = $ A * $ B

/= $ A/= $ B $ A = $ A/$ B

% = $ A % = $ B $ A = $ A % $ B

. = $ A. = $ B $ A = $ A. $ B

How to Use operator names

= Equal to $ A = $ B

=== Constant equals $ A ===$ B

! = Not equal to $! = $ B

! = Non-constant $! ==$ B

<> Not equal to $ A <> $ B

<Less than $ A <$ B

> Greater than $ A> $ B

<= Less than or equal to $ A <= $ B

>=Greater than or equal to $ A >=$ B

Note: constant equals indicates that true is returned only when the operands on both sides are equal and the data type is equal;

For example, 0 = "0" returns true because the operands are equal.

0 = "0" returns false because of different data types.

Logical operators

! Not! $ B

If $ B is false

Returns true; otherwise, the opposite is true.

& $ A & $ B

If both $ A and $ B are

True, the result is true;

Otherwise, it is false.

| Or $ A | $ B

If $ A and $ B have

True or both

True, the result is

True; otherwise, false.

And and $ A and $ B

Same as &, but its advantages

Low level first

Or $ A or $ B

Same as |, but preferred

Low Level

The operator "and" and "or" has a lower priority than & |.

Ternary Operators

Condition? Value if true: value if false

Example: ($ grade> = 50? "Passed": "failed ")

Error suppression OPERATOR:

$ A = @ (57/0 );

If the divisor cannot be 0, an error occurs. Therefore, add @ to avoid error warnings.

Array Operators

+ Join! $ B

Returns

$ A and $ B

Element array

= Equivalent $ A & $ B

If $ A and $ B have

Returns the same element.

True

=== Heng et $ A | $ B

If $ A and $ B have

The same element and

Returns the same order.

True

! = Non-equivalent $ A and $ B

If $ A and $ B are not

Returns true.

<> Non-equivalent

If $ A and $ B are not

Returns true.

! = Non-constant equals $ A or $ B

If $ A and $ B are not

Returns true if it is constant.

Operator priority and associativity:

Generally, operators have a set of priorities, that is, the order in which they are executed.

Operators are also associated, that is, the execution sequence of operators with the same priority. This order is usually from

Left to right, from right to left or irrelevant.

The following table lists the operator priorities. The top operator has the lowest priority.

Priority increases.

Operator priority

Left,

Left or

Left XOR

Left and

Right print

Left

= + =-= * =/=. = % = & = | = ^ = ~ = <=

>>=

Left? :

Left |

Left &&

Left |

Left ^

Left &

Irrelevant =! =====! =

Unrelated <<=>>=

Left <>

Left + -.

Left */%

Right

! ~ ++ --

(INT) (double) (string) (array) (object )@

Right []

Unrelated new

Unrelated ()

To avoid priority confusion, use parentheses to avoid priority.

Iii. Control Structure

If we want to effectively input the corresponding user, the Code must have the ability to judge. Allow the program to make judgments

The structure of is called a condition.

If

Condition judgment statement

If (condition judgment ){

//......

}

If (condition judgment ){

//....

}

Else {

//....

}

If (condition judgment ){

//...

}

Left |

Left &&

Left |

Left ^

Left &

Irrelevant =! =====! =

Unrelated <<=>>=

Left <>

Left + -.

Left */%

Right

! ~ ++ --

(INT) (double) (string) (array) (object )@

Right []

Unrelated new

Unrelated

Elseif {

//...

}

Elseif {

//....

}

Else {

//....

}

Switch

Statement

Switch (variable ){

Case "value 1 ":

//...

Break;

Case "value 2 ":

//...

Break;

Case "value n ":

//...

Break;

Default:

//....

Break;

}

While

Loop

While (condition ){

//....

}

For

Loop

For (initial value; condition; Counter ){

//....

}

In addition to the for loop, PHP also provides a foreach loop, which is specially used for Array usage. We are counting

Group.

Do

While

Loop

Do {

//...

} While (condition );

If you want to stop the execution of a piece of code, you can implement the following three methods based on the expected results.

Type 1: break; exit loop; Type 2: exit; exit program; Type 3: continue; exit current cycle

Ring

If (condition)

{

Break; // continue

}

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/342969.html pageno: 6.

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.