Writing PHP rules

Source: Internet
Author: User
Tags echo command html form

PHP is a language that runs on the server side and can dynamically generate HTML pages. This blog describes some of its coding rules.


1, PHP code always use <?php and? > Surround, for example

<?phpecho "Hello world!";? >

2, each PHP statement to end with a semicolon (;);

3, if there is PHP code in the Web page, it is best to name the file name extension on the Web server is. php instead of. html;

Second, the variable

A variable is a "container" for storing information in which data can be stored and each variable has a unique name

2.1 The naming rules are as follows:

1. Must start with a dollar symbol ($);

2, the variable name length is at least 1;

3, the first character after the dollar symbol can be a letter or underscore (_), after which can be letters, underscores, numbers;

4, spaces, and other special characters other than _ and $ are not allowed in the variable name

The above four are rules that must be followed, otherwise the code will not run; In addition, there are two conventions that can help the code to be more prescriptive:

1. The variable names are lowercase;

2. Use the underscore to divide the words in multiple-word variable names.

The types of data that PHP variables can store can be: Character, Boolean, numeric, Array, object, (objects can associate a set of data with the code used to process that data.) ) NULL, indicating that there is no value;

2.2 $_post is a special variable that contains form data, and is a super global variable that is built into PHP and does not have to be created separately;

$_post is a special PHP storage container that becomes an array and stores a set of variables in the same name. The data in the submitted form is stored in the $_post array, and the task of this array is to transfer the data to the script. The name of the form field, name, determines how it is accessed in the $_post array, such as the HTML form

<form method= "POST" action= "report.php" >    <label for= "Howlong" >how Long is you gone?</label>    <input type= "text" id= "Howlong" name= "Howlong"/><br/></form>

Accessed through the $_post array in PHP, "Howlong" comes from the name attribute of the form field <input> tag;

$how _long = $_post[' Howlong ');

Iii. How PHP code works

The browser will never see PHP files, PHP running on the server side, and converted into HTML code sent to the browser;

By default, the code in the PHP script is considered HTML code, placed between the <?php and?> tags, the server sees these tags as PHP code to run, the main use of the echo command, through the connection of text strings and PHP variables, can be constructed in real-time HTML code, Then use echo to output it as part of the resulting web page to the browser for example:

Echo ' and were gone for '. $how _long. ' <br/> ';

Where the echo command is used to output the extra information as an HTML content browser, so that the strings and variables are connected together.

Writing PHP rules

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.