Chapter 3 php operator and control structure code _ php entry _ script house

Source: Internet
Author: User
Tags php operator
Chapter 3 php operators and Control Structure Code. For more information about php learning, see.

Chapter 3 php operators and Control Structure Code. For more information about php learning, see.

I. String insertion
Difference between double quotation marks and single quotation marks:
1. Use of double quotation marks:
The Code is as follows:

// Double quotation marks can parse variables and escape characters
$ Username = "jack ";
Echo "his name is $ username! ";
Echo"
";
$ Username = "Xiaodong ";
// If the exclamation point is in English, the variable will be parsed normally.
Echo "His name is $ username! "; // His name is Xiaodong!
Echo"
";
// If it is a Chinese exclamation point, it cannot be parsed.
Echo "His name is $ username! "; // His name is
Echo"
";
// Although the escape characters are parsed here, \ n is a line break in the source code.
// Only one character is displayed in the browser
Echo "His name is $ username, \ n he is 20 years old this year"; // his name is Xiao Dong, he is 20 years old this year
Echo"
";
// To avoid errors, string connection is recommended.
Echo "His name is". $ username. "He is 20 years old."; // his name is Xiaodong. He is 20 years old.
?>

2. Use of single quotes:
The Code is as follows:
// Single quotes only output the string literal value,
// The variables and escape characters are not parsed.
// The syntax is not highlighted.
$ Username = 'anllin ';
Echo 'his name is $ username, \ n his age is 20 .';
// Output
// His name is $ username, \ n his age is 20.
?>

Some common escape characters

Ii. Operators
The Code is as follows:
// Arithmetic Operator
$ A = 5;
$ B = 3;
Echo $ a + $ B;
Echo'
';
Echo $ a-$ B;
Echo'
';
Echo $ a * $ B;
Echo'
';
Echo $ a/$ B;
Echo'
';
Echo $ a % $ B;
?>

8
2
15
1.66666666667
2
The Code is as follows:
// Compound value assignment operator
$ A = 5;
$ B = 3;
Echo $ a + = $ B;
Echo'
';
Echo $ a-= $ B;
Echo'
';
Echo $ a * = $ B;
Echo'
';
Echo $ a/= $ B;
Echo'
';
Echo $ a % = $ B;
Echo'
';
Echo $ a. = $ B;
?>

8
5
15
5
2
23
The Code is as follows:
// Increment/decrease Operator
$ A = 5;
Echo ++ $;
Echo'
';
Echo $ a ++;
Echo'
';
Echo -- $;
Echo'
';
Echo $ --;
?>

6
6
6
6
The Code is as follows:
$ A = 5;
$ B = 3;
$ C = 5;
$ D = '5 ';
Echo $ a = $ c;
Echo'
';
Echo $ a ===$ c;
Echo'
';
Echo $ a = $ d;
Echo'
';
Echo $! = $ B;
Echo'
';
Echo $! ==$ D;
Echo'
';
Echo $! = $ B;
Echo'
';
Echo $ a> $ B;
Echo'
';
Echo $ B <$ c;
Echo'
';
Echo $ a >=$ c;
Echo'
';
Echo $ a <= $ c;
?>

1
1
1
1
1
1
1
1
1
1
The Code is as follows:
$ A = false;
Echo! $;
Echo'
';
$ B = 5;
$ C = 3;
Echo $ B> 0 & $ c> 0;
Echo'
';
Echo $ B> 0 and $ c> 0;
Echo'
';
Echo $ B! = 0 | $ c! = 0;
Echo'
';
Echo $ B! = 0 or $ c! = 0;
Echo'
';
?>

1
1
1
1
1
The "and" or "operator has a lower priority than & |
Ternary Operators
The Code is as follows:
$ A = 100;
Echo $ a> 60? 'Success': 'fail ';
?>

Success
Error suppression Operator
The Code is as follows:
Echo @ (1, 100/0 );
?>

Iii. Control Structure
If condition judgment statement
The Code is as follows:

$ A = 10;
If ($ a> 0)
{
Echo 'integer greater than 0 ';
}
Echo'
';
If ($ a> 0)
{
Echo 'integer greater than 0 ';
}
Else if ($ a <0)
{
Echo 'integer less than 0 ';
}
Else
{
Echo 'integer equal to zero ';
}
?>

Switch statement
The Code is as follows:

$ Role = 'admin ';
Switch ($ role)
{
Case 'admin ':
Echo 'admin ';
Break;
Case 'user ':
Echo 'normal user ';
Break;
Case 'guest ':
Echo 'tourists ';
Break;
Default:
Echo 'tourists ';
Break;
}
?>

While loop statement
The Code is as follows:
$ A = 10;
While ($ a> 0)
{
Echo $ --;
Echo'
';
}
?>

Do while LOOP statement
The Code is as follows:
$ A = 10;
Do
{
Echo $ --;
Echo'
';
}
While ($ a> 0)
?>

For Loop statement
The Code is as follows:
For ($ a = 0; $ a <10; $ a ++)
{
Echo $;
Echo'
';
}
?>

Break statement
The Code is as follows:

For ($ a = 0; $ a <10; $ a ++)
{
Echo $;
Echo'
';
If ($ a = 5)
{
Break; // terminate the loop, but the statement after the loop is executed
}
}
Echo 'loop termination ';
?>

Exit statement
The Code is as follows:
For ($ a = 0; $ a <10; $ a ++)
{
Echo $;
Echo'
';
If ($ a = 5)
{
Exit; // exit directly. The statement following the loop is not executed.
}
}
Echo 'loop termination ';
?>

Continue statement
The Code is as follows:
For ($ a = 0; $ a <10; $ a ++)
{
Echo $;
Echo'
';
If ($ a = 5)
{
Continue; // end the loop and continue the next loop. The statement after the loop is still executed.
}
}
Echo 'loop termination ';
?>

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.