PHP Beginner's Learning basic grammar _php Tutorial

Source: Internet
Author: User
Tags closing tag processing instruction vars
PHP, is the abbreviation of English Super Text preprocessing language hypertext preprocessor. PHP is an HTML embedded language, is a server-side implementation of embedded HTML document scripting language, language style has similar to C language, is widely used.

Detach from HTML

When PHP parses a file, it looks for the start and end tags, and the tag tells PHP to start and stop explaining the code. Parsing this way allows PHP to be embedded in a variety of documents that are ignored by the PHP parser in the case of anything other than a pair of start and end tags. In most cases, PHP is embedded in an HTML document, as shown in the following example.

 
  
  
  1. This was going to be ignored.

  2. echo' While this was going to beparsed. ' ;?>
  3. This would also be ignored.

You can also use a more advanced structure:

Example#1 Advanced Separation Technique

 
  
  
  1. if ($expression) {
  2. ?>
  3. This is true.
  4. Else {
  5. ?>
  6. This is false.
  7. }

The previous example works, because when PHP touches the end tag?>, it simply outputs the contents as it is until it touches the next start tag. Of course, the above example is contrived, but for output chunks of text, it is usually more efficient to break out of PHP parsing mode than to use Echo () or print () output for all content.

You can use four pairs of different start and end tags in PHP. Two of them,

 
  
  

And

 
  
  

is always available. The other two types are short and ASP style tags that can be turned on or off in the php.ini configuration file. Although some people find it convenient to have short marks and ASP style tags, they are poorly ported and are usually not recommended.

Note: Also be aware that if you embed PHP in XML or XHTML you need to use it to keep up with the standards.

Example#2 PHP start and end tags

 
 
  1. Echo ' If you want to serve XHTML or XML documents,does like this ';?>
  2. Echo ' This was the simplest, an SGML processing instruction ';?>
  3. This was a shortcut for " "
  4. <% Echo ' Optionally use Asp-style tags ';%>
  5. <%= $variable ; # This is a shortcut for "<% echo ..." %>

The above example of 1 and 2 is always available, where 1 is most commonly used and recommended.

The short mark (above example 3) is only available after the instruction Short_open_tag in the php.ini configuration file is opened, or the--enable-short-tags option is added when PHP is compiled.

Note: If you use PHP 3, you can also activate using the short mark via the Short_tags () function. This method is only available for PHP 3!

The ASP style tag (above example 4) is only available if the instruction Asp_tags in the php.ini configuration file is opened.

Note: Support for ASP style tagging is added in version 3.0.4.

Note: You should avoid using short tags when developing a program or library that needs to be released, or on a server that the user cannot control. Because the target server may not support short tokens. For code porting and distribution, be sure not to use short marks.

Instruction delimiter

Like C or Perl, PHP needs to end the instruction with a semicolon after each statement. An end tag in a PHP code implies a semicolon; the last line in a PHP snippet can end without a semicolon. If there are new rows later, the end tag of the snippet contains the end of the line.

 
  
  
  1. echo"This is a test";
  2. ?>
  3. echo"This is a test" ?>
  4. echo' We omitted the last closing tag '

Note: The end tag of the PHP snippet at the end of the file can be omitted, in some cases it would be better to omit it when using include () or require (), so that the unwanted white space does not appear at the end of the file, and the response header can still be output. It is also handy when using output buffering, and you will not see the unwanted white space generated by the include file.

Comments

PHP supports c,c++ and Unix Shell-style (Perl-style) annotations. For example:

 
  
  
  1. !--? php
  2. echo "This is a test" ; //this is a one-line C + + style comment
  3. /* this was a multi line comment
  4. yet another line of comment */
  5. echo "This is yet another test" ;
  6. echo ' one Final Test ' ; This is a one-line shell-style comment
  7. ?>

A single-line comment only comments to the end of the line or the current PHP code block, depending on which first appears. This means that in//...? > or # ...? The HTML code after > will be displayed:?> jump out of PHP mode and return to HTML mode,//or # does not affect this. If the asp_tags configuration option is enabled, the behavior is the same as//%> or #%>. However, the tag does not jump out of PHP mode in a single-line comment.

 
  
  
  1. This is a echo"simple";? > Example.

  2. ' This was an example '

Copy code C-style comments end when you hit the first */. Make sure that you do not nest C-style annotations. This error is easily encountered when trying to comment out a chunk of code.

 
  
  
  1. echo "This is a test"; /* This comment'll cause a problem */
  2. */

Hope that through the above content of the introduction, can help you.


http://www.bkjia.com/PHPjc/445768.html www.bkjia.com true http://www.bkjia.com/PHPjc/445768.html techarticle PHP, is the abbreviation of English Super Text preprocessing language hypertext preprocessor. PHP is an HTML embedded language, is a server-side execution of embedded HTML document script language, ...

  • 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.