one. String Insertion
To provide developers with maximum flexibility in handling string values, PHP provides
a method for literal insertion and content insertion.
Double quotes provide maximum flexibility because variables and transfer sequences are parsed accordingly.
Copy code The code is as follows:
$userName = "张三";
echo "His name is $userName ";
echo "
";
/ /Chinese will have some problems
echo "His name is $userName, he is 19 years old and has grown up!";
echo "
";
//You can use string concatenation Solution
echo "His name is ".$userName.", he is 19 years old and has grown up!"
//Escape characters can be parsed
echo "Although his QQ account has many girls, n but None of them belong to him";
?>
Some commonly used escape characters
Escape sequence description
n Line feed character
r Carriage return
t Horizontal chart
\ Backslash
$ dollar sign
" Double quotes
Single quotes will be interpreted and parsed as declared When entering a string, variables and escape sequences will not be parsed.
echo 'Wu Qi's variable name is: $userName, the escape character n is invalid in single quotes'
?>
2. Operators
Operators are symbols used to perform certain operations on arrays and variables.
Arithmetic operators
Compound assignment operators
Prefix increment and decrement and post increment and decrement operators:
$a=++$b. ;
$a=$b++;
$a=--$b;
$a=$b--;
Comparison operator
Example of operator name
+ Add $a+$b
- Subtract $a-$b
* Multiply $a*$b
/ Divide $a/$b
% Take the remainder $a%$b
Using the operator 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
How to use the operator name
= = equal to $a= =$b
= = = Identity $a= = =$b
!= Not equal $a!=$b
!= = Not equal $a!= =$b
<> Not equal $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: Identity means only True will be returned only when the operands on both sides are equal and the data types are the same;
For example: 0= ="0" This returns true because the operands are equal
0= = ="0" This returns false because the data types are different
Logic Operator
! Non!$b
If $b is false,
returns true; otherwise the opposite
&& and $a&&$b
If $a and $b are both
true, the result is true;
otherwise it is false
|| or $a||$b
If one
of $a and $b is true or both are
true, the result is
true; otherwise it is false
and and $a and $b
and&& Same as ||, but its priority is lower. Operators "and" and "or" have higher priority than && and || Low.
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, otherwise an error will occur, so add @ to avoid error warnings.
Array operator
+ union !$b
returns an array containing all the elements
elements in
$a and $b
= = equivalent to $a&&$b
If $a and $b have
the same elements, Return
true
= = = Identity $a||$b
If $a and $b have
the same elements and
the same order, return
true
!= Not equivalent $a and $b
if $ a and $b are not
equivalent, return true
<> non-equivalent
if $a and $b are not
equivalent, return true
!= = non-identical $a or $b
if $ a and $b are not
identical, return true
Priority and associativity of operators:
Generally speaking, operators have a set of priorities, which is the order in which they are executed.
Operators are also associative, which is the execution order of operators with the same priority. This order usually goes from
left to right, right to left or irrelevant.
The table of operator precedence is given below. The top operator has the lowest priority, and the priority increases in order from top to bottom in the table.
Operator precedence
Left,
Left Or
Left >>=
Left? :
left||
left&&
left|
left^
left&
irrelevant = = != = = = = != =
irrelevant<<= >>=
left<< >>
left+ - .
left* / %
right
! ~ ++ --
(int)(double)(string)(array)(object) @
right[]
Not relevant New
No Related()
To avoid priority confusion, you can use parentheses to avoid priorities.
Three. Control Structure
If we want to effectively respond to user input, the code needs to be judgmental. The structure that allows the program to judge
is called a condition.
if
Conditional judgment statement
if (conditional judgment){
//......
}
if (conditional judgment){
//....
}
else {
//....
}
if (conditional judgment) {
//...
}
left||
left&&
left|
left^
left&
irrelevant= = != = = = = != =
irrelevant <<= >>=
left<< >>
left+ - .
left* / %
right
! ~ ++ --
(int)(double)(string)( array)(object) @
right[]
Not relevant New
Not relevant
elseif {
//...
}
elseif {
//....
}
else {
//....
}
switch
Statement
switch (variable) {
case "value1":
//...
break;
case "value2":
//...
break;
case "valueN" :
//...
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 specifically used for the use of arrays. We cover it in detail in the
group.
do
while
Loop
do {
//...
} while(condition);
If you want to stop the execution of a piece of code, there are 3 methods to achieve it depending on the desired effect.
The first one: break; exit the loop; the second one is exit; exit the program; the third one is continue; exit the current loop
loop
if (condition)
{
break; //continue
}
The above introduces the introductory learning of photoshop PHP learning notes (3) operators and control structures, including the content of introductory learning of photoshop. I hope it will be helpful to friends who are interested in PHP tutorials.