Chapter III PHP operators and control structure Code _php tutorial

Source: Internet
Author: User
A String insertion
The difference between a double quote and a single quote:
1. Use of double quotes:
Copy CodeThe code is as follows:

Double quotation marks can parse variables and escape characters
$username = "Jack";
echo "His name is $username!";
echo "
";
$username = "small East";
If the exclamation point in English will parse the variable normally
echo "His name is $username!"; /His name is little East!
echo "
";
If it is Chinese exclamation point will not parse out
echo "His name is $username!. ";//His name is
echo "
";
The escape character is parsed here, but \ nthe line is wrapped in the source code.
The browser displays the location of just one character
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, it is recommended to use the string connection method
echo "His name is". $username. "He's 20 years old this year";//His name is Xiao Dong, he's 20 years old this year.
?>

2. Use of single quotation marks:
Copy CodeThe code is as follows:
Single quotes are just output string literals,
Variables and escape characters are not parsed.
Also does not prompt for syntax highlighting
$username = ' Anllin ';
Echo ' His name is $username, \ n is 20. ';
Output
His name is $username, \ n his is 20.
?>

Some of the commonly used escape characters

Escape sequences

Describe

\ n

Line break

\ r

Enter

\ t

Horizontal chart

\\

Back slash

\$

Dollar symbol

\”

Double quotes


two. Operator
Copy CodeThe code is as follows:
Arithmetic operators
$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
Copy CodeThe code is as follows:
Compound 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
Copy CodeThe code is as follows:
Increment decrement operator
$a = 5;
echo + + $a;
Echo '
';
echo $a + +;
Echo '
';
echo--$a;
Echo '
';
echo $a--;
?>

6
6
6
6
Copy CodeThe 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 $a! = $b;
Echo '
';
echo $a!== $d;
Echo '
';
echo $a! = $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
Copy CodeThe code is as follows:
$a = false;
Echo! $a;
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
Operator "and" and "or" than && and | | The priority level is lower
Ternary operator
Copy CodeThe code is as follows:
$a = 100;
echo $a > 60? ' Success ': ' Fail ';
?>

Success
Error suppression operator
Copy CodeThe code is as follows:
echo @ (100/0);
?>

Three Control structure
If condition judgment statement
Copy CodeThe 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 equals zero ';
}
?>

Switch statement
Copy CodeThe code is as follows:

$role = ' admin ';
Switch ($role)
{
Case ' admin ':
echo ' admin ';
Break
Case ' user ':
Echo ' Ordinary user ';
Break
Case ' guest ':
Echo ' Visitor ';
Break
Default:
Echo ' Visitor ';
Break
}
?>

While Loop statement
Copy CodeThe code is as follows:
$a = 10;
while ($a > 0)
{
echo $a--;
Echo '
';
}
?>

Do and loop statements
Copy CodeThe code is as follows:
$a = 10;
Do
{
echo $a--;
Echo '
';
}
while ($a > 0)
?>

For Loop statement
Copy CodeThe code is as follows:
for ($a = 0; $a < $a + +)
{
echo $a;
Echo '
';
}
?>

Break statement
Copy CodeThe code is as follows:

for ($a = 0; $a < $a + +)
{
echo $a;
Echo '
';
if ($a ==5)
{
break;//terminates the loop, but executes the statement following the loop
}
}
echo ' Loop over ';
?>

Exit statement
Copy CodeThe code is as follows:
for ($a = 0; $a < $a + +)
{
echo $a;
Echo '
';
if ($a ==5)
{
exit;//exit directly, the statement behind the loop does not execute
}
}
echo ' Loop over ';
?>

Continue statements
Copy CodeThe code is as follows:
for ($a = 0; $a < $a + +)
{
echo $a;
Echo '
';
if ($a ==5)
{
continue;//end this cycle, continue the next loop, the statement after the loop still executes
}
}
echo ' Loop over ';
?>

http://www.bkjia.com/PHPjc/324812.html www.bkjia.com true http://www.bkjia.com/PHPjc/324812.html techarticle A. The difference between a string and a single quotation mark: 1. Use of double quotes: copy code as follows: Meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "? PHP//double ...

  • 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.