Php learning notes (3) operators and control structures

Source: Internet
Author: User
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.

The code is as follows:


$ UserName = "James ";
Echo "His name is $ userName ";
Echo"
";
// Some problems may occur in Chinese
Echo "his name is $ userName. he is 19 years old and has grown up! ";
Echo"
";
// 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, none of them 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.
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 $ 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
}

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.