PHP development basic tutorial: learning syntax

1. PHP syntax

The PHP script is executed on the server, and then the pure HTML result is sent back to the browser.


2. Basic PHP syntax

PHP scripts can be placed anywhere in the document.

PHP script starts with <?php and ends with ?>:

<?php

//This is the PHP code

? >

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

PHP files usually contain HTML tags and some PHP script code.

Below, we provide an example of a simple PHP file that outputs the text "Hello World!" to the browser:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<h1>我的第一个PHP页面</h1>
<?php
echo "Hello World";
?>
</body>
</html>

Every line of code in PHP must be preceded by a semicolon Finish. A semicolon is a delimiter used to separate sets of instructions.

Through PHP, there are two basic instructions for outputting text in the browser: echo and print. These two will be explained in the following courses


3. Comments in PHP

The code in PHP will not be executed, it is a reference to the current Explanation of program code segments, comments have many functions:

1. Comment the key code segments in the program to improve code readability

2. Make it easier for new code takeovers Programmers understand

3. It is convenient to recall the code quickly after not touching the code for a long time

4. Generate documents

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

PHP supports three types of comments:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
// 这是单行注释
# 这也是单行注释
/*
这是多行注释块
它横跨了
多行
*/
echo "Hello World";
?>
</body>
</html>

4. PHP pair size Write Sensitivity

  • #In PHP, all user-defined functions, classes and keywords (such as if, else, echo, etc.) are not case-sensitive.

In the following example, all three echo statements are legal (equivalent):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
Echo "Hello World";
echo "Hello World";
ECHO "Hello World";
?>
</body>
</html>
  • But in PHP , all variables are case-sensitive.

In the following example, only the first statement will display the value of the $name variable (this is because $name, $NAME, and $naME are considered three different variables ):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
$name="小明";
echo "我的名字叫做 ".$name;
echo "我的名字叫做 ".$NAME;
echo "我的名字叫做 ".$naME;
?>
</body>
</html>

Learning experience:

  • Pay attention to the semicolon after each line of code. In many cases, such an error will be reported

4.png

It should be that you forgot to add semicolons in the upper and lower lines of this line.

  • Comments are an important part of a program, please don’t forget it


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <h1>我的第一个PHP页面</h1> <?php echo "Hello World"; ?> </body> </html>
submitReset Code