PHP Learning Notes (iii) _php base of operator and control structure

Source: Internet
Author: User
Tags logical operators
one. String Insertion
To provide the most flexibility for developers to handle string values, PHP provides for literal inserts and content inserts
A method.
Double quotes provide maximum flexibility because both the variable and the transition sequence are parsed accordingly.
Copy Code code as follows:

<?php
$userName = "John";
echo "His name is $userName";
echo "<br/>";
There are some problems in Chinese
echo "His name is $username, he is 19 years old, has grown up!" ";
echo "<br/>";
can be resolved by string concatenation
echo "His name is". $userName. "He's 19 years old and he's grown up! "
Escape characters can be parsed
echo "Although his QQ number has many girls, \ n But one does not belong to him";
?>

Some of the commonly used escape characters
Escape sequence Description
\ n Line Feed
\ r Carriage Return
\ t Horizontal tab chart
\ reverse Slash
\$ Dollar character
\ "Double Quotes

Single quotes are interpreted as they are, and the variables and escape sequences are not parsed when the string is parsed.
<?php
The variable named echo ' Wu Qi is: $userName, the escape character is not valid in single quotes '
?>
two. Operator
An operator is a symbol used to perform some operation on an array and a variable.
Arithmetic operator
Compound assignment operator
Forward increment and post decreasing operators:
$a =++ $b;
$a = $b + +;
$a =--$b;
$a = $b--;
Comparison operators
Operator name Example
+ Add $a+ $b
-Reduction of $a-$b
* by $a* $b
/except $a/$b
% $b to take more than $a%
Operator use method is equivalent to
+ + $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
Operator name use method

= = = Equals $a= = $b
= = = Identity $a= = = $b
!= Unequal $a!= $b
!= = not identical $a!= = $b
<> Unequal $a<> $b
< less than $a< $b
> Greater than $a> $b
<= is less than or equal to $a<= $b
>= is greater than or equal to $a>= $b

Note: The identity means that only the two operands are equal and the data type returns true;
For example: 0 = = "0" This returns true because the operands are equal
0= = = "0" This returns false because the data type is different


logical operators
! It's not $b.
If $b is false, the
Returns true;
&& and $a&& $b
If $a and $b are both
True, the result is true;
otherwise false
|| or $a| | $b
If $a and $b have a
To true or to all
True, the result is
true; False otherwise
and $b with $a and
The same as &&, but its excellent
Lower First level
Or or $a or $b
with | | Same, but its priority
Lower level

Operator "and" and "or" ratio && and | | Have a lower priority.
Ternary operator
Condition? Value if True:value if False
Example: ($grade >=50? "Passed": "Failed")


Error suppression Operator:
$a =@ (57/0);
The divisor cannot be 0, and there will be an error, so add @ To avoid the error warning.
Array operators
+ Union! $b
Returns a containing
All elements in $a and $b
An array of vegetarian
= = = Equivalent $a&& $b
If $a and $b have
The same element, returning
True
= = = Identity $a| | $b
If $a and $b have
The same elements and
In the same order, return
True
!= non-equivalence $a and $b
If $a and $b are not
Equivalent, return True
<> Non-equivalence
If $a and $b are not
Equivalent, return True
!= = Non-identity $a or $b
If $a and $b are not
Identity, return True

Precedence and associativity of operators:
Generally speaking, operators have a set of priorities, which is the order in which they are executed.
Operators also have associativity, which is the order in which the operators of the same priority are executed. This order usually has the
Left to right, from right to left or irrelevant.
The table below gives the operator precedence. The top operator has the lowest precedence, and follows the top of the table.
Order, priority increment.

Operator Precedence
Left
Left or
Left XOR
Left and
Right print
Left
= + = = *=/=. =%= &= |= ^= ~=
>>=
Left? :
Left | |
Left &&
Left |
Left ^
Left &
Not related = =!= = = = =!= =
Non-related <<= >>=
Left << >>
Left +-.
Left */%
Right
! ~ ++ --
(int) (double) (string) (array) (object) @
Right []
Not relevant new
Not related ()
To avoid priority confusion, you can use parentheses to avoid precedence.
Three Control structure
If we want to effectively input the corresponding user, the code needs to have the ability to judge. That allows the program to be judged.
The structure is called a condition.
If
Conditional Judgment Statement
if (conditional judgment) {
//......
}
if (conditional judgment) {
//....
}
else {
//....
}
if (conditional judgment) {
//...
}
Left | |
Left &&
Left |
Left ^
Left &
Not related = =!= = = = =!= =
Non-related <<= >>=
Left << >>
Left +-.
Left */%
Right
! ~ ++ --
(int) (double) (string) (array) (object) @
Right []
Not relevant new
Not relevant

ElseIf {
//...
}
ElseIf {
//....
}
else {
//....
}
Switch
Statement
Switch (variable) {
Case "Value 1":
//...
Break
Case "Value 2":
//...
Break
Case "Value N":
//...
Break
Default
//....
Break
}
While
Cycle
while (condition) {
//....
}
For
Cycle
for (initial value; condition; counter) {
//....
}
In addition to the For Loop, PHP also provides a foreach loop, which is specifically used for array use. We are counting
Group to describe it in detail.
Todo
While
Cycle
do {
//...
while (condition);
If you want to stop the execution of a piece of code, depending on what you need to achieve, you can have 3 of the methods implemented.

The first type: break; Exit the loop; the second is exit; the third is continue; exiting the current
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.