Examples of common PHP syntax applications

Source: Internet
Author: User
Tags echo d

Common PHP syntax 1: PHP script code mark

The PHP script is the content included in a pair of special tags in the file. For example, ASP is "<% .... %> ", PHP can be viewed as" <?...?> ".

However, to adapt to XML standards to embed PHP into XML or XHTML, PHP does not recommend the use of "<?...?>" in short format ", We recommend that you mark "<? Php...?>"

In addition, the PHP code block supports the markup form of <script language = "php">... </script>.

Common PHP syntax 2: PHP Command Separator

Each PHP statement needs to be separated by the Semicolon ";", but the PHP end mark "?>" Because it automatically implies a semicolon, you do not need to append the semicolon.

Therefore, the format of a PHP script is as follows:

<? Php

// Note that the last row can have no semicolon
?>
 

PHP common syntax 3: PHP comments

PHP multi-line comment using ""

Use "#" or "//" for a single line comment

Common PHP syntax 4: PHP output

Use "<% =... %>" in ASP to quickly output a single row, or use "<% Response. Write ("... ") %>"

Directly use "echo ()" or "print ()" in PHP, for example:

 
 
  1. < ?php  
  2. echo "a";  
  3. echo (b);  
  4. echo ("c");  
  5. echo d;  
  6. ?>  

The output is "abcd". All the above four types can be output normally.

However, in ASP, especially echo "a"; and echo d;, both are output as strings themselves, which is impossible. Therefore, you need to understand the definition of PHP variables.

Common PHP syntax 5: PHP Variables

Like ASP, PHP variables can be directly used without being defined first. Variable types are automatically generated when values are assigned.

Various variables in PHP Add "$" before the variable name to show the difference.

 
 
  1. < ?php  
  2. $a="123";  
  3. echo a;  
  4. echo $a;  
  5. ?>  

Enter "a123"

Common PHP syntax 6: The difference between single quotes and double quotes in PHP

 
 
  1. < ?php  
  2. $a="123";  
  3. echo "$a";  
  4. echo '$a';  
  5. ?>  

The output is "123 $ a", where echo "$ a" outputs the value of variable a, while echo '$ a' outputs the string itself in single quotes.

 
 
  1. < ?php  
  2. $a="123";  
  3. echo "$a'$a'";  
  4. ?>   

The output value "123 '123' is not" 123 $ ". Although it is '$ A', the variable placed under double quotation marks is still replaced.

Therefore, we can conclude that variables in double quotation marks will be replaced, while those in single quotation marks will not be replaced.

The content in double quotation marks must be escaped and prefixed with "", such as "", "$", and "". Therefore, if you enter "123 $ ",

 
 
  1. < ?php  
  2. $a="123";  
  3. echo "$a$a";  
  4. ?>  

For example:

 
 
  1. < ?php  
  2. $a="123";  
  3. echo "$a$a"";  
  4. ?>  

The output is "123 $ "".

Notes on common PHP syntaxes for ASP transfer to PHP:

1. The separator comma ";" is easy to forget to write.

2. Definition and use of variables.

3. single quotation marks and double quotation marks.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.