Understanding PHP from a simple program
The PHP site's online tutorials are already great. This part of this article will familiarize you with PHP. I can't do anything without omission. My goal is to allow you to quickly start your PHP programming.
3.1 prerequisites
First, you must have a working web server that supports PHP. I suppose the extension of all PHP files on your server is. php3.
3.2 install PHP
For the installation and configuration of PHP, refer to the "PHP installation guide" topic article on taoba.
3.3 syntax
In terms of syntax, the PHP language is similar to the C language. It can be said that PHP draws on the syntax features of the C language and is improved by the C language. We can write PHP code and HTML code in a mix. We can not only embed PHP scripts into HTML files, but also embed HTML tags into PHP scripts. The following are several methods you can use. You can choose one of the most suitable and stick to this method!
Separating from HTML
You can use the following methods:
<? ...?>
<? Php...?>
<Script language = "php">... </script>
<%... %>
Note: When you use "<? ...?>" When you embed PHP code into an HTML file, it may conflict with XML. Whether or not you can use this reduction form depends on the PHP settings. To adapt to XML and other editors, you can add "php" after the question mark to adapt the PHP code to the XML analyzer. For example, "<? Php...?> ". You can also use script markup like writing other scripting languages, such as <script language = "php">... </script> ".
Statement
Like Perl and C, ";" is used in PHP to separate statements. The signs separated from HTML also indicate the end of the statement.
Note
PHP supports C, C ++, and Unix-style Annotations:
/* C, C ++ multi-line comment */
// C ++ style single line comment
# Unix style single line comment
Echo and print
The simplest interaction between PHP and HTML is achieved through print and echo statements. In actual use, print and echo functions are almost identical. In this case, you can use either of them. However, there is also a very important difference between the two: In the echo function, multiple strings can be output at the same time, while in the print function, only one string can be output at the same time. At the same time, the echo function does not need parentheses, so the echo function is more like a statement than a function. Let's take a look at the following example:
<?
$ A = "hello ";
$ B = "world ";
Echo "a", "B ";
Print "a", "B ";
?>
After you view the running status of the Code in a browser, you will see the following running result:
Aba
Parse error: parse error in d: adminmyphphometest. php3 on line 5
This indicates that this Code cannot be fully explained. The error occurs on the fifth line of the Code: "print" a "," B ";".
3.4 A simple instance
With the knowledge we have learned, you can write the simplest program to output one of the most famous words in the program world.
<HTML>
<HEAD>
<TITLE>
<?
Echo "Hello World! ";
?>
</TITLE>
</HEAD>
<BODY>
<H1>
First PHP page
</H1>
<HR>
<?
// Single line C ++ style comment
/*
Printing the message
*/
Echo "Hello World! ";
# Unix style single line comment
?>
</BODY>
</HTML>