Talk about PHP syntax (2)

Source: Internet
Author: User
Author: Chinese Red Wolf


The previous article "Talk about PHP syntax" has talked about the data type and expression of PHP. Now, let's look at the variables and constants in PHP.
Let's look at one example first.
File: test.php
<?php
This is a single-line annotation method
#这是另一种单行注释方法
/* This is a multi-line annotation method
Let's take a look at the following example.
funtion display ($file, $line)
{
Global $message;
echo "FILE: $file <br>";
echo "line: $line <br>";
echo "message: $message <br>";
}

$message = "This is a routine. ";
Display (_file_,_line_);
?>
Display results as:
text.php
15
This is a routine

In the example above, a function is defined as a custom function. The following two variables $file and $line are two localized variables, they work only in function bodies, with variable Queensland in vitro, and if there are $file or $line outside the function body, the values of two $file and two $line are not necessarily the same. _file_ and _line_ are two constants, and their Queensland values are already well established. _file_ is the file name of this file, _line_ is the line number of the execution line. There is also a global $message in the function body; Its function is to make the whole Queensland Bureau variable $message can function to function body, this sentence also can write $global["message"];
For information generated by mechanisms such as GET, post, and cookies, PHP automatically treats it as a PHP variable. In this way, the information processing of submitting forms is remarkably relaxed. As follows:
File: form.html
<body>
<form action= "deal.php" >
User name: <input type= "text" name= "uname" size=20><br>
<input type= "Submit" value= "OK" >
</form>
</body>

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

The above program will require the user to enter a user name, submit the form, and then return to the user name confirmation information. It can be seen that the form of the uname has become the deal.php program in the $UNAME variable Queensland amount. It's easy. :-)
Here's a look at the basic Process Control in PHP:
If...else ... Elseif
Grammar one:
if (condition) {
Statement body
}
Syntax Two:
if (condition) {
Statement Body A
}else{
Statement Body Two
}
Syntax Three:
if (condition 1) {
Statement Body A
}elseif (condition 2) {
Statement Body Two
}else{
Statement Body Three
}
We change the deal.php procedure above to read:
<?php
if ($uname = = "Xiaoming") {
echo "It's good to see you, Xiaoming." ";
}elseif ($uname = "Xiao Hua") {
echo "Oh, it's Xiao Hua." ";
}else{
echo "You are $uname, right";
}
?>
In addition to the IF statement, there is a while loop whose syntax is as follows:
while (condition) {
Statement body
}
Executes the statement body when the condition is true.
The syntax for Do...while is as follows:
do {
Statement body
}while (condition)
Executes the statement body first, and if the condition is true, the loop executes the statement body again.
The syntax for the For loop is the same as C, as follows:
for (condition initial; Judge condition; conditional change) {statement}
The break jumps out of the loop being executed, continue to interrupt the cycle.
All right, here's the article. Some of the above basics believe you will soon be able to get started.
--(to be continued)--


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.