Introduction to PHP syntax

The PHP script is executed on the server and the plain HTML results are sent back to the browser.


Your first PHP code

This code is very magical, one sentence can turn into a web page. This is our first piece of code to get started with PHP.

Now you can create a new file in the root directory of the web server (for example, D:\xampp\htdocs). The name of the file is: abc.php.

Write the following code in this abc.php file:

<?php
  phpinfo();
?>

You can enter your server address in the browser address bar. We currently use our own computer as the web server. You can enter in the address:

http://127.0.0.1/abc.php or http: //localhost/abc.php

You will see a web page with the content as shown below:

101.png

Note: phpinfo is a function (function), this function (function) will display detailed PHP information of a current computer (server). This function must be remembered by everyone!

Let’s learn about the basic syntax of PHP through our first piece of code:

PHP scripts can be placed anywhere in the document.

PHP scripts start with "<?php" and end with "?>"

##The default file extension for PHP files is ".php".

A PHP file usually contains HTML tags and some PHP script code.

All code parts of PHP must use half-width English. Many people tend to write full-width English and symbols, causing PHP code errors.

Every line of code written in PHP must end with a semicolon (;). A semicolon is a delimiter used to separate sets of instructions.

When we write the file abc.php and name the file, we only use English half-foot characters (a-zA-Z0-9_-) to name the file. Do not use Chinese characters in file names, do not use special appendixes, do not add spaces in the middle, and strictly distinguish between sizes.

Below, we provide a simple PHP file example, which can output the text "Hello World!" to the browser:

Example

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

Run Example»


Through PHP, there are two basic instructions for outputting text in the browser: echo and print.


Comments in PHP

Because the code is in English and the code is very long, people will forget it after a long time. So we'll add annotations.

There are many functions of annotation:

Mark the key points

It is easy to forget to recall quickly after a long time, which is convenient for searching

When letting others see it Understand quickly

You can also generate documents. After writing the code, the relevant documents will be written, which improves work efficiency.

The code after comments, blank lines, and carriage returns will look more beautiful.

Comments can be used Troubleshooting. If you are not sure which part of the code is wrong, you can comment a large section to determine the error interval

The content in the middle of the comment will not be executed by the computer

We understand the comment Benefits, let’s talk about PHP comments

Single-line comments:

//  表示单行注释

Multi-line comments:

/*
 多行注释 这里是注释区域代码 
*/

Examples

<!DOCTYPE html>
<html>
<body>

<?php
// 这是 PHP 单行注释

/*
  这是 
  PHP 多行
  注释
*/
?>

</body>
</html>




Continuing Learning
||
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
submitReset Code