Basic PHP syntax
Open notepad, write the following program, and save it as hello. php:
<Html>
<Head>
<Title> PHP test </title>
</Head>
<Body>
<? Php
$ Lang_name = "PHP5 <br/> ";
Echo "welcome". $ lang_name;
// Print "use $ lang_name to print the text <br> ";
Printf ("this is a display style similar to the C language <br> ");
/*
PHP multi-line comment
*/
?>
<H1> This is a super <? Php # echo "simple";?> . </H1>
</Body>
</Html>
Based on the above example, let's take a look at the basic PHP syntax:
1. PHP embedded HTML Method
With "<? Php starts with "?>" End: PHP code in the middle.
Start with <SC riplanguage = "php">, end with </SC ript>, and use the PHP operation code in the middle.
With "<?" Starting with "?>" The PHP operation code is in the middle. Variable embedding uses the <% echo variable %> format.
Start with "<%" and end with "%>". The PHP operation code is in the middle. Variable embedding using <% echo variable %>
Note: We recommend that you use the first method. Methods 3rd and 4 are not supported.
2. The file package contains two methods: require and include.
The require function is usually placed before the PHP program. Before executing the program, read the file specified by require. This method can be used to introduce the required files.
The include function is generally placed in the processing part of the process control. When the PHP program reads the include file, it will read the introduced file. For example:
Include ("mysql. db. class. php ");
Require ("mysql. db. class. php ");
If a syntax error exists in the file contained by require or does not exist, the system will prompt a Warning of Fatal error and immediately terminate the program running. The include command only displays a Warning error, then execute the statement after the script.
3. PHP Variables
PHP variables start with $ and use names as the distinction between variables. Naming rules are similar to those in C, so we will not repeat them here.
Initialization of common variables: $ var_name = value. For example, $ num = 123; $ str = "phpbook"; $ bool = false; $ a = 1.2e3; $ var = null;
Array variable Initialization
$ Array name [key name] = value; for example: $ animals ['a'] = 'tiger ',...; $ Array name = array (key name 1 => value1, key name 2 => value2 ,...); $ Birds = array ('b1 '=> 'owl', 'b2' => 'magpie ',...);
$ Array name [] = value; for example: $ mon [] = 'july 6'; $ mon [] = 'july 6'; $ array name = array (value1, value2, ...); For example: $ arr = array ('A', 'B', 'C ');