Open Notepad, write the following program, and save it as hello.php:
$lang_name= "PHP5
";
echo "Welcome".$lang_name;
//print "Use $lang_name to print text here
";
printf("This is a display style similar to C language
");
/*
Multi-line comments for PHP
*/
?>
Based on the above small examples, let’s get to know the basic syntax of PHP:
1. How to embed PHP into HTML
It starts with "", with PHP code in the middle.
It starts with
It starts with "" and ends with "?>", with PHP operation code in the middle. Variable embedding uses the <% echo variable%> format.
Starts with "<%" and ends with "%>", with PHP operation code in the middle. Variable embedding uses <% echo variable %>
Note: It is recommended to use the first method, and the third and fourth methods are not supported.
2. There are two ways to include require and include
The require function is usually placed in front of the PHP program. Before the program is executed, the files specified by require are read in. For the public function part of the system, you can use this method to introduce the required files.
The include function is generally placed in the processing part of process control. When the PHP program reads the include file, it will read the imported file. For example:
include(“mysql.db.class.php”);
require(“mysql.db.class.php”);
If there is a syntax error or does not exist in the file included using require, a Fatal error will be prompted and the program will be terminated immediately; while include will only display a Warning warning error, and then continue to execute the statements following the script.
3. PHP variables
PHP variables start with $, and the names are used as the distinction between variables. The naming rules are similar to those of C language, so I won’t go into details.
Initialization of regular variables: $var_name=value. For example: $num=123;$str=“phpbook”;$bool=false;$a=1.2e3;$var=null;
Initialization of array variables
$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[]='March'; $mon[]='May'; $Array name=array(value1,value2,…);For example: $arr =array('a','b','c');