Summary of PHP basic syntax and PHP basic syntax

Source: Internet
Author: User
Tags define function

Summary of PHP basic syntax and PHP basic syntax

I. What can PHP do?

What can PHP do? I think it is very powerful, as long as I can think of it, it can be done, but my technical capabilities are not enough. Okay, let's take a look at the figure. (ps: PHP functions are not limited to this (pai_^ ))

The image is a bit blurred )/

 

Ii. PHP language mark

1. End and start mark

1.1 <? Php // code?> : It belongs to the xml style and is a standard PHP style. It is recommended.

1.2 <script language = "php"> // code </script>: long style mark, not commonly used. If your wonderful editor does not support other php tags, use it.

1.3 <? // Code?> : The short style follows SGML processing. In php. in ini, open the command short_open_tag, or add-enable-short-tags during php compilation. if you want your program to be portable, You can discard this style, and it is less than 1.1 php.

2. Location

What should I do? You can put the PHP language anywhere in the HTML file suffixed with. php. Note: it is an HTML file ending with. php.

   1: 
   2:     
   3:         <meta http-equiv="content-type" content="text/html;charset=utf-8">
4: <! -- Embed scripts in HTML tags -->
5: <title> <? Php echo "PHP language mark"?> </Title>
   6:     
7: <! -- Embed data in the attribute position -->
   8:     <body <?php echo 'bgcolor="#ccc"'?>>
9: <! -- Come to an advanced level -->
  10:         <?php
  11:             if($exp){
  12:         ?>
13: <! -- Embed php in the property value -->
14: <p align = "<? Php echo 'center'?> "> The condition is true. </p>
  15:         <?php
  16:             }else{
  17:         ?>
18: <p> the condition is FALSE. </p>
  19:         <?php
  20:             }
  21:         ?>
  22:     </body>
  23: 

3. Notes

3.1 single line comment: // or # multi-line comment:/* Description */

More than 3.2 line comments cannot be nested, but can contain single line comments; single line comments can also contain multiple lines of comments. Just like this

   1: <?php
2: // echo "test";/* a single line contains multiple line annotators */
3:/* echo 'test'; // contains a single-line annotator */
   4: ?>

 

Iii. Variables

1. Use of Variables

   1: <?php
2: $ a = 1; // declare a variable
3: $ B = "php"; // declare a variable B
4: $ 8d = 2; // invalid variable name. It can only start with a letter or underline and does not contain spaces
   5:
6: $ I site is = "php"; // valid variable name, which can be in Chinese
   7:     /*
8: * The following three function call methods are equivalent.
9: * keywords and built-in functions and user-defined class names. The function names are case-insensitive.
  10:      */
  11:     phpinfo();
  12:     PhpInfo();
  13:     PHPINFO();
  14:
  15:
  16:     /*
17: * The following three variables are different.
18: * variable names are case sensitive
  19:      */
  20:     $name = "php1";
  21:     $Name = "php2";
  22:     $NAME = "php3";
  23:
24: // variable: variable names can be dynamically set
  25:     $hi = "hello";
  26:     $$hi = "world";
27: // hello world is output below
  28:     echo "$hi $hello";
  29:     echo "$hi ${$hi}";
  30:
31: // variable assignment
32: $ foo = "B" // value assignment
33: $ bar = & $ foo // reference value assignment
  34:     $bar = "LZ";
35: echo "$ foo"; // output LZ
36: $ cde = $ foo; // value assignment
  37:     $cde = "E";
38: echo "$ foo"; // output LZ
  39: ?>

2. Variable type

 

Iv. Constants

1. Define and use

   1: <?php
   2:     /*
   3:      *boolean define(string name,mixed value[,bool case_insensitive)
4: * name: constant name; value: constant value; the third value is an optional Boolean value. The default value is FALSE (Case Insensitive)
   5:      */
   6:     define("FLO",1000);
7: echo FLO; // output 1000
   8:
9: // use the define function to check whether a FLO constant exists. If yes, the constant value is output.
  10:     if(define("FLO"))
  11:     {
  12:         echo FLO;
  13:     }
  14: ?>

2. constants and variables

2.1 The scope of a constant is global. You can declare and access a constant anywhere in the script.

2.2 There is no $ before a constant, and a constant cannot be defined using a value assignment statement.

2.3 Once a constant is defined, it cannot be redefined or canceled until the script stops running.

2.4 The constant value can only be a scalar (a type in boolean, integer, float, string)

3. predefined constants of the system

4. Common magic Constants


Source: http://www.ido321.com/510.html


Basic php syntax

Double quotation marks are used to output strings. Example: echo "data insertion failed, error message: <br> ";
And "insert into testtable VALUES ('". $ xm. "',". $ nl. ")"; in insert into testtable VALUES is a string, which means to INSERT to the database, two "" are a group, put. $ xm. separated ,(. $ xm .) $ xm is a variable. echo is used when a variable is displayed in php.

Basic php syntax

An equal sign is a value from right to left.
The two equal signs are equal.
The three equal signs are absolutely equal (the values and data types are completely equal)
To avoid the problem in the third line, we recommend that you write the conditional statement ("" = $ I), so that an error is returned even if the equal sign is missing, rather than executing the assignment.

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.