PHP Basic Syntax
1. Four different start-end tags
Only <?php?>, <script language= "PHP" ></script> two are always available, the remaining <??>, <%%> need to be configured in php.ini. That is, the Short-open-tag and asp-tags in php.ini are turned on respectively, they are disabled by default, and are not recommended for portability.
2. Instruction delimiter "semicolon"
The ";" After the end of the PHP code snippet You can also not, when using output buffering and include () or require () ellipsis would be better.
3. Notes
It is a good habit to write proper annotations. PHP comments include multi-line Comments "/* */", single-line comment "//" and "#", multiple lines of comments can not nest another multi-line comments, and other situations at random collocation can be.
4. Blank Line
A blank line can show the beauty of the code hierarchy. For example, two code fragments of a source file use two blank lines, and two class declarations also use two blank lines. For example, there is a blank line between the two function declarations, a blank line between the local variables in the function and the first statement, and a blank line between the two logical snippets in a function before the block comment or the single line comment. In the case of a space, the comma after the argument list is followed by a space, the operator and the operand, and the For statement expression after the comma.
5. Variables
PHP's most basic storage units are variables and constants that can store different types of data. Because it is a weakly typed language, the data type of the variable is determined by the context or runtime.
5.1 Variable Declaration
"$" is followed by the variable name and assigned with "=", when the first time a variable is assigned a value, the variable is created (just declared, not assigned?). )。 Most variables are used only in a separate scope to the end of the file, if not declared inside the function. The individual scope here not only contains <?php, but it can be used in all open PHP modes on a single page, and also includes files introduced with include and require. If you use a cookie or session, you can also use it on more pages.
You can use Unset () to release the specified variable, use inset () to detect if the variable is set (there is a declaration), and use empty () to check whether it is empty.
5.2 Variable naming
Variable names are case-sensitive, but built-in structures and keywords, as well as user-defined class names and function names, are case insensitive. The variable name begins with a letter underscore, followed by any letter, number, and underscore.
5.3 mutable variables
You can use the value of a defined variable as the name of another variable, such as:h i= " Hel lo " hI= "worLd"; Then echo" HiThe result of hello" and "hi {hi} "is" Hello World ".
5.4 Reference assignment of a variable
that is, add "&" to the variable name, equivalent to the fetch operation in C, assign the variable address to 21 variables, and two variables together point to the same value. Only a variable with a name can be assigned a value, such as (3*9) or the value returned by the function.
6. Types of variables
Strongly typed languages require a type to be specified before a variable or constant is defined, and PHP is a weakly typed language, depending on the context in which it is applied.
6.1 Type Introduction
PHP supports 8 primitive types and some pseudo types. The original types are as follows:
|
Four types of scalar |
Boolean (Boolean) |
|
Integer (Shaping) |
|
Float (float type, also called double) |
|
String (character type) |
Data type |
Two kinds of composite types |
Array (arrays) |
|
Object (Objects) |
|
Two special types of |
Resource (resources) |
|
Null |
The variable type is determined by the context, and you can use Var_dump () to view the type and value of a variable.
6.2 Boolean Type
A Boolean value is returned in PHP for relational or logical operations. The value in the following case is assumed to be false in the Boolean value:
Boolean false,0,0.0, blank string or "0", no array of member variables, no cell objects (for PHP4), special type null (including variables that have not yet been set).
6.3 Shaping
The length of the shaping is related to the platform. Available "+", "-" for positive or negative, integer can omit "+", you can specify octal or hexadecimal with "0" and "0x", but the result is output in decimal, unsigned integer is not supported, maximum integer is 2 147 483 647, Minimum is-2 147 483 647, the above is handled according to the float type.
6.4 Floating-point type (float or double)
The floating-point number length is also related to the platform. The range is 1.7e-308~1.7e+38, accurate to 15 digits after the decimal point. It is only an approximate value, never accurate to the last one, such as the expression 8, the internal representation of 7.9999999 .... As a result, it is not possible to indicate whether two floating-point numbers are equal, even if the two floating-point numbers are equal, the internal representation is not the same, resulting in unexpected results. Any precision math function or GMP () function can be used at this time.
6.5 String
There is no limit to the length of the string, which can be defined using single quotes, double quotes, and delimiter three literal methods, but they have functional differences.
6.5.1 Single quotation mark
The escape character "\" escapes the single quotation mark and the escape character itself in single quotation marks, and cannot escape other special characters, otherwise it is output as-is. The variable that appears in single quotes is not replaced by the value of the variable, but the variable name is output. It is most efficient to use single quotation marks to define a string, because it eliminates the overhead of character processing conversions and variable parsing at PHP parsing.
6.5.2 Double Quotes
You can escape more special characters, parse variables in a string, and use {} to isolate the variable name and resolve it ("{beer}s" or "{Beer}s").
6.5.3 delimiter
$string <<<eot/text/EOT; At the end of the EOT head the first column is written, followed only by the semicolon as the end.
6.6 Arrays
The array in PHP is actually an ordered graph, and mapping values to Keys,values can be any type value allowed by PHP.
6.7 Objects
Object types that are similar to other object-oriented languages, but differ in the declaration usage details.
6.8 Resource types
A resource type holds a reference to an external resource, which is created and used by a dedicated function, which holds a special handle to the open file, database connection, graphics canvas area, and so on, and the programmer creates, uses, releases.
6.9 null
Null indicates that a variable has no value, the only possible value for a null type is that Null,null does not represent a space, does not represent zero, and is not an empty string, but rather a variable value that is empty. Case-insensitive, the condition variable is considered null: The variable is directly assigned null, the declared variable has not been assigned a value, and the variable is destroyed with unset ().
6.10 Pseudo-type
Mixed
Number
Callback
6.11 Conversions between data types
Automatic type conversion
Forcing type conversions
Variable type test function: Is_boolean (), Is_int (), etc.
7. Constants
A constant is an identifier for a simple value that cannot be changed or undefined once defined during script execution. Scopes are global and can be declared and accessed anywhere in the script. Constants can contain only a few scalar data types, such as boolean,integer,string,float.
7.1 Definition and use
The default case sensitivity, usually uppercase, but not $, is defined using the Define () function, such as define (string name,mixed Value[,boolean case_insensitive]). The first parameter is the name, the second is the value, and the third is optional, indicating whether the case is sensitive. You can use defined () to check whether a constant is defined.
7.2 The difference between constants and variables
• No $ before constant
• Can only be defined with define () and cannot be evaluated by an assignment statement
• Ignore variable scope rules and define usage anywhere
• Once defined, you cannot redefine or cancel the definition
• The value can only be a specific scalar
7.3 Pre-defined constants
PHP defines a series of constants that are not case-sensitive and can be used directly in the program to accomplish some special functions. They have different extension library definitions, which should be loaded before they are used. Some constants change depending on where they are used, such as _line_ based on the lines in the script.
8. Operators
Arithmetic operator (+,-. *,/,%,++,--)
The character operator (.) can concatenate characters, which is equivalent to "+" in Java.
Assignment operator (=,+=,-=,*=,/=,%=,.=)
Comparison operator (>,<,>=,<=,==,===,<>/!=,!==)
Logical operators (and/&&,or/| |,not/!,xor)
Bitwise operators (&,|,^,~,<<,>>)
Other operators (?:, ', @,=>,->,instanceof)
Priority level
9. Expressions
A simple definition of an expression concept is "anything with value," and no value can be considered a declaration.
The language structure of PHP
There are three types of structures in any language: sequential, branching, and looping.
Branching structure
If, if...else ..., if...elseif...else ..., switch case ...
Loop structure
While, Do...while, for
Special control statements
Break, continue, exit (as long as execution to exit (), regardless of whether it is in that structure will directly exit the current script die () is the alias of Exit (), you can output a message with the parameter and exit).
PHP's functions
11.1 Function definitions
A function is a named, stand-alone code snippet that performs a specific task and may return a value.
11.2 Custom Functions
Defined
function fname (parameter list) {
Statement
return;
}
Call
If the function is not called, it will not execute. The function name has three functions:
• Calling functions by function name
• Used to pass parameters
• Represents the return value
Parameters
return value
11.3 Working principle and structured programming of functions
The statements in the function are executed only if the function is called. When execution is complete, control returns to the place where the function was called. Structured programming divides a task into several small modules, each of which is a function.
Range of PHP variables
Most PHP variables have a single scope, and this separate span also contains the files introduced by include and require. The valid range of variables is divided into local variables and global variables according to the location of the declaration.
Local variables
The parameters of variables and functions declared inside the function are local variables, and the function's internal variables are freed after the function execution is finished.
Global variables
A global variable is defined outside the function and is used to end the start of the definition to the program file. In PHP, a local variable in a function overrides a global variable of the same name, either by adding the Global keyword before the referenced globals, or by using the Globals array, which is automatically created at run time to hold all the variables in the global scope, which can be referenced by the index.
static variables
Local variables are stored in the form of dynamic storage and static storage. A local variable in a function is dynamically allocated storage space if it is not explicitly declared as a static type, freeing space automatically after the function call ends. At this point, if you want the function to be executed and its internal variables still exist in memory, you should use static to modify the variable to a static variable. The amount of change is initialized when the function is first called, and then left in memory.
13. Parameter passing
The way PHP parameters are passed is passed by value and by reference, and it also supports the way in which default parameters and arbitrary argument lists are passed.
Value passing
When the parent program calls the function, the function has its own memory area, and the parent program passes the variable or value directly to the function, when the parent program's variables and the variables passed into the function are not already in the same place, so any action on the parameters of the function will not affect the parent program's variables or values. For example:
1 <?php 2 3 function test ($arg) {4 5 $arg =; 6 7 } 8 9 $var = 300;10 echo $var; test ($var); echo $var;?>
Line 9th $var stored in the global PHP memory, the value of its passed to the method, the method will be in its own memory area of a block to hold parameters passed in the value of the variable. Assuming that one is in zone A, and one is in area B, then the value of the function operation in zone B does not affect the value of zone A, because they are not the same value. But the reference pass is different.
Reference delivery
Reference passing is the passing of a variable's relative address in memory to the function, at which point the value of the function operation and the parent program variable are the same value, and the function's modification of the passed parameter affects the parent program variable. To pass a reference, simply add a & to the parameter, which is equivalent to the accessor operator in C.
1 function Test (& $arg) {2}
Default parameters for functions
When you are allowed to define a function, specify the default value for the parameter, such as:
1 <?php 2 3 function test ($name, $age =10) { 4 } 5 function show ($name = "Bob Hu", $age =10) {6 } 7 8 test ("Bob Hu"), 9 test ("Bob Hu", "ten"); ?>
At this point, the test function parameter, age, specifies a default value that can be passed, but name must be passed. The two parameters of the show function have default values, can be called directly, or can be called by a parameter. Many of the previous parameters of a system function are mandatory, followed by an option, defined in such a way as the test function, such as printf (), mysql_query (), and so on.
Variable-length parameter list
That is, you can pass arguments that are more than the number of parameters, you can get an array of all the arguments inside the function through Func_get_args (), or you can get the number of arguments by Func_num_args (). This feature is similar to the arguments property inside a JavaScript function. The system function Array () (no wonder that the array can be directly added to any number of elements without prejudging the length), and the parameters such as echo () are variable length.
14. Variable functions
Like in JavaScript, you assign a function to a variable, and PHP has a function like this:
1 <?php 2 3 function test ($name, $age =10) { 4 }5 6 $func = "Test"; 7 $func ("Bob"); 8 9?>
(The name of the function is also the address?) )
15. Recursive functions
Call yourself indirectly or directly within yourself.
16. Using a custom Function library
Using your own defined functions, you can put these functions in a file, when they are included through some structural methods. Like what:
1 <?php 2 3 require ("bob.php"); 4 5 if (true) {6 include ("Hu.txt"); 7 else{8 include "hu.php"; 9 } Ten require "bob.txt";?>
The difference is that require checks and loads the file before the script executes, and the include is loaded when it is executed. In addition, require_once () and include_once () Ensure that a file is loaded only once, not repeatedly.
17. Using the System function library
To use the system's functions, you must introduce an extension package or library module, and you can tell what extensions PHP has loaded with phpinfo () and Get_loaded_extensions ().
PHP Basic Syntax