Home  >  Article  >  Backend Development  >  Talk about PHP syntax (2)_PHP tutorial

Talk about PHP syntax (2)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:06:05791browse

Author: Hua Honglang


The previous article "Talk about PHP syntax" has already talked about PHP data types and expressions. Now, let’s take a look at PHP variables and constants.
Let’s look at an example first.
File: test.php
//This is a single-line comment method
#This is another single-line comment method
/*This is a multi-line comment method Comment method
Let’s take a look at the example below*/
funtion display($file,$line)
{
global $message;
echo "FILE:$file echo "LINE:$line
";
echo "Message:$message
";
}

$message="This is a routine . ";
display(_FILE_,_LINE_);
?>
The displayed result is:
text.php
15
This is a routine

In the above example, function defines a custom function. The next two variables $file and $line are two local variables. They only work in the function body and do not interfere with the variables outside the function. If there is $file or $line outside the function, , the values ​​of two $files and two $lines are not necessarily the same. _FILE_ and _LINE_ are two constants, and their values ​​have already been determined. _FILE_ is the file name of this file, _LINE_ is the line number of the execution line. There is another sentence in the function body: global $message; Its function is to enable the global variable $message to be applied to the function body. This sentence can also be written as $GLOBAL["message"];
For GET, POST and PHP will automatically treat the information generated by mechanisms such as cookies as PHP variables. In this way, the information processing of submitting the form is particularly easy. As follows:
File: form.html



Username:





File: deal.php
echo "Your username For: $uname";
?>

The above program will ask the user to enter a username. After submitting the form, the username confirmation message will be returned. It can be seen that uname in the form has become the $uname variable in the deal.php program. Keep it simple. :-)
Let’s take a look at the basic flow control of PHP:
if…else…Elseif
Grammar 1:
if (condition) {
Statement body
}
Grammar Two:
if (condition) {
Statement body one
}else{
Statement body two
}
Grammar three:
if (condition 1) {
Statement body one
}elseif (condition 2) {
Statement body two
}else{
Statement body three
}
We change the above deal.php program to:
if ($uname=="Xiao Ming") {
echo "I'm so happy to see you, Xiao Ming.";
}elseif ($uname=="Xiaohua") {
echo "Oh, it's Xiaohua.";
}else{
echo "You are $uname, right";
}
?>
except if In addition to the statement, there is a while loop, whose syntax is as follows:
while(condition){
statement body
}
When the condition is true, the statement body is executed. The syntax of
do...while is as follows:
do {
Statement body
}while (condition)
Execute the statement body once first. If the condition is true, the statement body will be executed again in a loop.
The syntax of the for loop is the same as C, as follows:
for (initial condition; judgment condition; condition change) {statement}
And break jumps out of the executing loop, and continue is to interrupt this loop.
Okay, that’s it for this article. I believe you will be able to get started with the above basics very quickly.
--(to be continued)--

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315518.htmlTechArticleAuthor: Hua Honglang The previous article "Talk about PHP Syntax" has talked about PHP data types and expressions. Now, let’s take a look at PHP variables and constants. Let's look at an example first. File: t...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn