PHP syntax (2)

Source: Internet
Author: User

Author: Hua honglang

The previous article "talk about PHP syntax" has talked about PHP Data Types and expressions. Now let's take a look at the variables and constants of PHP.
Let's take a look at this example first.
File: test. php
<? PHP
// This is a single line Comment Method
# This is another single line Comment Method
/* This is a multiline comment method.
Let's take a look at the example below */
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 _);
?>
The result is as follows:
Text. php
15
This is a routine

In the preceding example, function is a custom function. The following two variables $ file and $ line are two local variables. They only play a role in the function body and do not interfere with the variation amount of the function in vitro, if $ file or $ line exists in the function body, the values of both $ file and $ line may not be the same. _ File _ and _ line _ are two constants. Their limit values have been fixed. _ File _ is the object name, And _ line _ is the row number of the execution line. In the function body, there is also a global $ message, which is used to enable the fully qualified variable $ message to function body, this sentence can also be written as $ global ["message"];
For information generated by get, post, and cookie mechanisms, PHP automatically treats it as a PHP variable. In this way, the information processing of the submitted form is extremely easy. As follows:
File: form.html
<HTML> <Body>
<Form action = "deal. php">
Username: <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 preceding Program requires the user to enter a user name. after submitting the form, return to the user name for confirmation. It can be seen that the uname in the form has become the $ uname variable in the deal. php program. Simple. :-)
let's take a look at the basic process control of PHP:
if... Else... Elseif
Syntax 1:
If (condition) {
statement body
}< br> syntax 2:
If (condition) {
statement body 1
}else {
statement body 2
}< br> syntax 3:
If (condition 1) {
statement body 1
} elseif (condition 2) {
statement body 2
}else {
statement body 3
}< br> we set the preceding deal. change the PHP program to:
if ($ uname = "James") {
echo "nice to meet you, James. ";
}elseif ($ uname =" Xiaohua ") {
echo" oh, it's Xiaohua. ";
}else {
echo" you are $ uname, right? ";
}< BR >?>
in addition to the IF statement, there is a while loop. Its syntax is as follows:
while (condition) {
statement body
}< br> when the condition is true, the statement body is executed.
do... The syntax of while is as follows:
do {
statement body
}while (condition)
execute the statement body once. If the condition is true, the statement body is executed cyclically.
the syntax of the For Loop is the same as that of C:
for (initial condition; judgment condition; condition change) {statement}
the break jumps out of the execution loop, and the continue interrupts the loop.
okay. Here is the article. I believe you will soon be able to get started with the above basics.
-- (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.