PHP Self-study Note 3

Source: Internet
Author: User

-------
Add
--------
When defining constants, it is recommended to determine whether a constant name is defined, to be judged by defined (), and to return a Boolean type
if (!defined ("POEM")) {//Not defined
Define ("POEM", "Du Fu");
}
---------------------------------
1, operator
2. Select structure
3. Cyclic structure
-----------------------------------
1, operator
Arithmetic operations (+-*/non-divisible% for remainder + +)--)
$x = 10;
$y = $x + +; After the first assignment, the self-increment
$z =++ $x; Self-increment and assign value first
Echo $y, $x, $z;
Logical operations
With && and OR | | Or not! Not XOR different or
Note: (1) Priority: and less than &&,or less than | |
$e =false| | True
$f =false or true;//first assignment, then judge
echo $e, "----", $f; 1---
Suggested use of && or | |, add parentheses as needed when using or or
(2)
Expression 1&& Expression 2 if 1 is false, no longer judge 2
Expression 1| | Expression 2 If 1 is true, no longer judge 2
$x = 10;
$y = 9;
$z = $x | | + + $y;//Left is 1, right + + $y not executed
Echo $z, $y;
Comparison operators
> < >= <=
= = (value equal) = = = Congruent (both values and types are equal)
! = (unequal values)!== not congruent (value or type not equal)
$x = 10;
$y = 10.0;
$z = 9;
if ($x = = $y) {//value, etc.
echo "Equal 1";
}
if ($x = = = $y) {//value and type are exactly equal and
echo "equivalent 2";
}
if ($x! = $z) {
echo "Unequal 3";
}
! (A and b) =!a | | ! B

if ($x!== $z) {//value or type varies
echo "Unequal 4";
}
"Note" string comparison: compare each character from left to right

Assignment operators
= += -= *= /= %= .=
$a = 10;
$a +=12;
echo $a;

$y = "Hello";
$y. = "World";
Echo $y;

Trinocular operation (ternary expression)
Conditions? Expression 1: Expression 2
If the condition is true, the result is an expression of 1, otherwise the result is an expression of 2
Requirement: If you enter Tom 123 to log on successfully, the login fails
$name =$_get["username"];
$PSW =$_get["PSW"];

$result = $name = = "Tom" && $psw = = "123"? " Login successful, $name, $PSW ":" Login Failed ";
$result 2= $name = = "Tom" && $PSW! = "123"? " The password is incorrect ":" ";
$result 3= $name! = "Tom"? User does not exist ":" ";

Echo $result, $result 2, $result 3;
The second Kind
$result =$_get["User"]== "Tom"? ($_get["Pass"]== "123"? "Login Successful": "Password is incorrect"): "User name does not exist";
echo $result;

Select structure
Single Branch If
Double if Else
Multi-Branch If else if ... else

echo Date ("Y year M D Day D week");
$time =date ("H");
if ($time <12) {
echo "Good Morning";
}else if ($time >=12&& $time <18) {
echo "Good Afternoon";
}else{
echo "Good evening";
}
**SWITCH selection Structure
Multiple choices: values, etc. or unequal, cannot handle the selection of range or interval
$time =$_get["Time"];
Switch ($time) {
Case "1": echo "Monday"; Break
Case "2": echo "Tuesday"; Break
Case "3": echo "Wednesday"; Break
Case "4": Echo "Week 4"; Break
Case "5": Echo "Week 5"; Break
Case "6": Echo "Week 6"; Break
Case "7": echo "Sunday"; Break
Default:echo "Week error";
}


$time = 4;
Switch ($time) {
Case "1": echo "Monday";
Case "2": echo "Tuesday";
Case "3": echo "Wednesday"; Break
Case "4": Echo "Week 4";
Case "5": Echo "Week 5"; Break
Case "6": Echo "Week 6";
Case "7": echo "Sunday";
Default:echo "Week error";
}
Week 4 Week 5
"Note" When the switch matches case succeeds, the selection structure is not exited until a break is encountered, so it is recommended to add a break after each case;
Default position arbitrary, execution is also encountered break exit selection structure
Switch execution order: matches the case first, and matches the default if it does not match successfully
Example 1:
$b =true;
Switch ($b) {
Case False:echo ' false ';
Case 0:echo ' ******* ';
Case Null:echo ' empty ';
Case-1:echo ' negative ';
Case 2:echo ' Tuesday ';
Default:echo "no This Day";
}
Match a case entry that is not false when the expression is true in switch
Not 0 is true, false and 0, and Null is False
Example two:
$b = 2;
Switch ($b) {
Case True:echo ' happy ';
Case 2:echo ' Tuesday ';
Default:echo "no This Day";
}
Description: If a case is true, the entry will match until the match succeeds.
Example three:
$b = "2";
Switch ($b) {
Case 2:echo ' Tuesday ';
Case True:echo ' happy ';
Default:echo "no This Day";
}
Case matching is numeric, switch structure can automatically convert type
Example four:
$b =null;
Switch ($b) {
Case False:echo ' false ';
Case Null:echo ' null object ';
Default:echo "no This Day";
}
Description: The expression value in switch is null as a false match
Here null and false are match entries, and the results are related to the order of the two case portals
Loop structure
while (condition) {
Loop body
Note: Control variables
}
Requirements: For 1-100 and
$i =1;//Control variables
$sum =0;//Save Results
while ($i <=100) {
$sum + = $i;
$i ++;//, don't forget.
}
Echo $sum;
Do-while: Xianzhanhouzou
do{
Loop body
modifying control variables
}while (conditions);

Requirements: For 1-100 and
$i =1;//Control variables
$sum =0;//Save Results
do{
$sum + = $i;
$i + +;
}while ($i <=100);
Echo $sum;
For loop
for (statement 1; condition; statement) {
Loop body
}
Requirements: For 1-100 and
$sum =0;//Save Results
for ($i =1; $i <=100; $i + +) {
$sum + = $i;
}
Echo $sum;
Note: When the number of cycles is known, a for loop is usually used, and if you do not know the number of cycles generally use while or do-while
Cyclic structure Complex
Outer control line number
for ($i =1; $i <=9; $i + +) {
The inner control loses several # of each line
for ($j =1; $j <= $i; $j + +) {
echo "#";
}
echo "<br>";
}

Outer control line number
for ($i =1; $i <=9; $i + +) {
The inner layer controls what is entered on each line
Lose space
for ($j =1; $j <=10-$i; $j + +) {
echo "&nbsp;&nbsp;";
}
Output
for ($j =1; $j <= $i; $j + +) {
echo "#";
}
echo "<br>";
}
Output pyramid
for ($i =1; $i <=9; $i + +) {
The inner layer controls what is entered on each line
Lose space
for ($j =1; $j <=10-$i; $j + +) {
echo "$";
}
Output
for ($j =1; $j <=2* $i-1; $j + +) {
echo "#";
}
echo "<br>";
}
Output Hollow Pyramid
for ($i =1; $i <=9; $i + +) {
The inner layer controls what is entered on each line
Lose space
for ($j =1; $j <=9-$i; $j + +) {
echo "&nbsp;";
}
Output
for ($j =1; $j <=2* $i-1; $j + +) {
Except for the last line,//Output # on the first end, otherwise the output space
if ($i!=9) {
if ($j ==1| | $j ==2* $i-1) {
echo "#";
}else{
echo "&nbsp;";
}
}else{
echo "#";
}
}
echo "<br>";
}

PHP Self-study Note 3

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.