Self taught PHP notes (I) PHP syntax

Time:2021-9-27

This article is forwarded from:https://blog.csdn.net/KH_FC/a…

PHP basic syntax

PHP uses a special pair of tags to contain PHP code, mixed with HTML code. When the server parses the page, it can automatically filter out the PHP script and interpret it, and finally pass the generated static web page to the client.

1. PHP Tags

Generally, PHP code is embedded in HTML documents. PHP code can be embedded in HTML in the following ways:

  1. Default tag

The default tag is to separate HTML and PHP code by “<? PHP” and “? >” tags. For example:

<?php
    #Write PHP code here
?>

example】Using this default tag, you can freely mix PHP and HTML code in HTML documents, such as:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<?php if ($test) { ?>
    < div > $test variable is true. < / div >
<?php } else { ?>
    < div > $test variable is false. < / div >
<?php } ?>
</body>
</html>

The output result is:PHP $test variable is false

be careful】When using the above example method to output large HTML strings, it is more convenient to use than to useecho()orprint()More convenient and faster.

  1. Script tag

Script tags are usedscriptMediumlanguageProperty to set the scripting language for PHP. For example:

<script language="php">
    #Write PHP code here!
</script>
  1. Abbreviation mark

The shorthand tag is removed compared to the default tagphpKeywords, convenient for everyone to write. For example:

<?
    #Write PHP code here!
?>
  1. ASP tag

ASP tags are written in a way similar to Java<%and%>A pair of tags splits PHP code. For example:

<%
    #Write code here!
%>

be careful】If you use this ASP tag to write PHP code, you need to modify the PHP configuration filephp.iniFile, need toshort_open_tagandasp_tagsThese two parameters are set toOn, considering the poor portability of the two methods, this method is not recommended. Amend as follows:

short_open_tag = On
asp_tags = On

2. PHP comments

A programming language must write comments. Let’s see what comments PHP has

  1. Single line annotation in C + + language style

For example:

<?php
    //Here are my notes!
?>

example】This single line annotation method is often used and is also a common annotation method

<?php
echo 123;  // Output 123
?>

The output result is:123My comments are not displayed.

  1. Shell scripting language style comments

For example:

<?php
    /*
        here
        Is much
        Line note
        Yo!
    */
?>

example】Multiline comments are mainly used at the beginning of a brief description of the code

<?php
/*
    The following code is used for multiplication calculation
 */
    $a = 10;
    $b = 20;
    $c = $a * $b;
    echo $c;
?>

The output result is:200
be careful】Do not write code in multi line comments, otherwise it will not take effect and cannot be explained!!!
example】Here, write PHP code into multi line comments to see what happens

<?php
/*
    The following code is used for multiplication calculation
    $d = 200;
 */
    $a = 10;
    $b = 20;
    $c = $a * $b * $d;
    echo $c;
?>

The output result is:0Here, because the variabledWhen it is written in the comments, PHP will not interpret the code in the comments

  1. Multi line annotation in C language style

For example:

<?php
    #Here are my notes!
?>

example】Single line note

<?php
    $a = 10;    # This is the a variable
    $b = 20;    # This is the B variable
    $c = $a * $b; # This is the C variable
    echo $c;    # This is the output C variable
?>

The output result is:200
be careful】Don’t write single line notes?>Yo, if you don’t let the following code take effect, an error will appear!!!
example】When a single line comment is added?>What will happen

<?php
    $a = 10;    # This is the a variable
    $b = 20;    # This is B? > variable
    $c = $a * $b; # This is the C variable
    echo $c;    # This is the output C variable
?>

The output result is:Self taught PHP notes (I) PHP syntax
When a single line comment is added?>When the terminator, PHP will stop interpreting the following code, and the following code will be presented in the form of HTML

3. PHP instruction separator

It has the same separator as C, Perl and other languages;, after the statement that needs to be separated, when a PHP code contains an ending character?>You can omit the delimiter;, so you can omit the delimiter in the last section of PHP code because?>The closing character contains a separator.
example】Use separator normally

<?php
    Echo "first line of code";
    Echo "the second line of code";
?>

The output result is:
Self taught PHP notes (I) PHP syntax
example】The last piece of code omits the delimiter

<?php
    Echo "first line of code";
    Echo "the second line of code";
    Echo "last line of code"
?>

The output result is:Self taught PHP notes (I) PHP syntax