The main topic is
Variable Extension System in. php Strings
More data types available in. php
. Type conversion
. Input and use variables and constants
. How to Build expressions and operators required to build expressions in php
. Use the available control structures in the language
. 1. More about input strings
Copy codeThe Code is as follows:
<? Php
$ Hour = 16;
$ Kilometres = 4;
$ Content = "cookie ";
Echo "4 in 24 hour time is {$ hour} 00 hours. <br/> \ n ";
Echo <DONE
There are {$ kilometres} 000 m in {$ kilometres} km. <br/>
The jar is now, indeed, full of $ {content} s. <br/>
DONE;
?>
Output: 4 in 24 hour time is 1600 hours.
There are 4000 m in 4 km.
The jar is now, indeed, full of cookies.
If you want to generate the exact character sequence {$ in the output, use {\ $ to escape it.
. 2. More introduction to Data Types
1. array: Use the array method to declare an array. It obtains a group of initial values and returns an array object that stores all these values. By default, it assigns the integer names or keys starting from 0 to the values in the array.
You can also specify the index of the new item to be added. $ Frunit [120] = "nespola"; however, you can also use a string value to specify a key without the default number assigned to it.
$ Myfavourite = array ("car" => "ferrari", "number" => 21, "city" => "ouagadougou ");
Array operator example name result
$ A + $ B join $ a and $ B.
$ A = $ B is equal. If $ a and $ B have the same key/value pair, the value is TRUE.
$ A ===$ B. TRUE if $ a and $ B have the same key/value pairs and the order and type are the same.
$! = $ B: TRUE if $ a is not equal to $ B.
$ A <> $ B: TRUE if $ a is not equal to $ B.
$! ==$ B
Copy codeThe Code is as follows:
<? Php
$ A = array ("a" => "apple", "B" => "banana ");
$ B = array ("a" => "pear", "B" => "strawberry", "c" => "cherry ");
$ C = $ a + $ B; // Union of $ a and $ B
Echo "Union of \ $ a and \ $ B: \ n ";
Var_dump ($ c );
$ C = $ B + $ a; // Union of $ B and $
Echo "Union of \ $ B and \ $ a: \ n ";
Var_dump ($ c );
?>
After the script is executed, the script displays:
Union of $ a and $ B: array (3) {["a"] => string (5) "apple" ["B"] => string (6) "banana" ["c"] => string (6) "cherry"} Union of $ B and $ a: array (3) {["a"] => string (4) "pear" ["B"] => string (10) "strawberry" ["c"] => string (6) "cherry"} 2.2.2 objects will be used in Object-Oriented Programming in Unit 4. 2.2.3 NULL is a special type and value in php. It indicates "no value ". it is null as follows :. they are set to case-sensitive keywords null ;. they have never been assigned a value. use the unset method to clear them explicitly. Resource: Sometimes, php needs to process objects not necessarily from php, such as the handles of database or operating system objects. They are called special variables of resources.
. 3 forced type conversion
2.3.1 Basics
Implicit forced type conversion: The most common scenarios for implicit forced type conversion are:
. Binary operator
. Boolean expression and expression Operator
. Specific methods of the string-specific methods and operators, such as echo \ print or string connector (.)
Display forced type conversion: add the type prefix enclosed in parentheses to the variable (or expression), and php will try to convert it for you.
(Int) \ (interger)
(String)-convert to a text string
(Object)-convert to object
2.3.2 special forced type conversion
Convert to integer
(Int) 4.999
Note: null is always converted to an integer 0.
Convert to floating point
(Float) true = 1.0
The result of converting an array, object, or resource to a floating point value is undefined. Do not try to convert it or believe the conversion result.
Convert to string
You can use the type conversion character (string) or call strval to convert the variable to a string.
Convert Boolean true to string 1, and false to be converted to an empty string ("")
Convert null to an empty string ('").
Convert to array
You can use array or arraryr to convert a variable or expression to an array.
Null and other unspecified variables are converted to an empty array with 0 elements.
Convert to object
You can use type conversion (object) to convert a variable or expression to an object.
Converting an object to an object only returns the handle of the same object. Create an object of the stdClass type for all other types.
2.3.3 useful forced type conversion functions
Is_type ()
. Is_integer,. is_float,. is_bool, is_null,. is_object. Return a Boolean value indicating whether a specific variable belongs to an appropriate type.
Gettype () is a very useful routine that tells you what type php currently considers a variable or expression. This conversion function is not recommended.
Settype () uses two parameters: the variable to be converted and the type to be converted, which indicates the string.
. 4 variables and constants
2.4.1 defining Constants
In a php program, the language structure define is used to define constants. Constant names do not start with a character $, and their values can only be of a specific type: integer, floating point, string, or Boolean.
2.4.2 values and referenced Variables
By default, most variables and all constants are assigned values by value. When the value of a variable is assigned to another variable, its value is copied. This method applies to all types except objects
For object variables and resources, all the copied content is the underlying object or resource handle, but the underlying objects of operations are the same.
Another way to assign values to another variable is to assign values by reference. Prefix.
$ A = 123;
$ B = & $;
2.4.3 variable range
For function-level variables, internal declared variables are valid only within the function.
Variables declared outside the Function
Super global variable
2.4.4 variable survival
No matter whether the same script or different scripts are executed, php will not remember any content between calls.
2.4.5 predefined Variables
Php provides many predefined variables that provide information about the operating environment. Most of them are super global arrays, for example:
$ GLOBALS-It contains references to all variables globally available within the script being executed
$ _ SERVER-information about the surrounding environment of the script
$ _ SESSION, $ _ COOKIE-it contains information about managing visitors and storage methods called "cookies"
$ _ REQUEST-it contains $ _ post, $ _ GET, and $ _ session Arrays
$ _ ENV-it contains the environment variable of the process where the php language engine is located. The key of the array is the name of the environment variable.
$ Php_errormsg-It stores the latest error message generated by the php language engine when executing the current script.
. 5 expressions and operators
2.5.1 OPERATOR: Combination expression
Assignment:
Arithmetic Operators
Example name result
-$ A: returns the negative value of $.
$ A + $ B addition: sum of $ a and $ B.
The difference between $ a-$ B subtraction $ a and $ B.
The product of $ a * $ B multiplication $ a and $ B.
$ A/$ B Division $ a divided by the operator of $ B.
$ A % $ B modulo $ a divided by the remainder of $ B.
Comparison operator
Example name result
$ A = $ B is equal to TRUE if $ a is equal to $ B.
$ A ===$ B all equal to TRUE, if $ a is equal to $ B, and their types are the same. (Introduced in PHP 4)
$! = $ B is not equal to TRUE, if $ a is not equal to $ B.
$ A <> $ B is not equal to TRUE, if $ a is not equal to $ B.
$! = $ B is not equal to TRUE. If $ a is not equal to $ B, or they have different types. (Introduced in PHP 4)
$ A <$ B Small and TRUE, if $ a is strictly less than $ B.
$ A> $ B is greater than TRUE if $ a is strict with $ B.
$ A <= $ B is less than or equal to TRUE, if $ a is less than or equal to $ B.
$ A >=$ B is greater than or equal to TRUE if $ a is greater than or equal to $ B.
Logical operators
Example name result
$ A and $ B And (logical and) TRUE, if both $ a And $ B are TRUE.
$ A or $ B Or (logical or) TRUE, if $ a Or $ B is TRUE.
$ A xor $ B Xor (logical exclusive or) TRUE. If either $ a or $ B is TRUE, but not both.
$ A Not (logical Not) TRUE, if $ a is Not TRUE.
$ A & $ B And (logical And) TRUE, if both $ a And $ B are TRUE.
$ A | $ B Or (logical Or) TRUE. If $ a Or $ B is set to TRUE.
Bitwise Operator
Operator name result
$ A & $ B And (bitwise And) sets the bits of $ a And $ B to 1.
$ A | $ B Or (bitwise Or) sets the bit where $ a Or $ B is 1 to 1.
Xor ^ $ B Xor sets the bits of $ a and $ B to 1.
Not $ a Not (Not by bit) sets the bit 0 in $ a to 1, and vice versa.
$ A <$ B Shift left (left Shift) moves the bits in $ a to the left $ B (each movement represents "multiplied by 2 ").
$ A> $ B Shift right (right Shift): Move the bits in $ a to the right $ B (each movement means "divided by 2 ").
String Operators
Concatenation operator. It operates on two strings and returns a single string that connects them.
Array Operators
Example name result
$ A + $ B join $ a and $ B.
$ A = $ B is equal. If $ a and $ B have the same key/value pair, the value is TRUE.
$ A ===$ B. TRUE if $ a and $ B have the same key/value pairs and the order and type are the same.
$! = $ B: TRUE if $ a is not equal to $ B.
$ A <> $ B: TRUE if $ a is not equal to $ B.
$! = $ B incomplete. If $ a is not all equal to $ B, TRUE is returned.
Other operators
Auto increment and auto increment Operators
$ A = 10;
$ B = $ a ++; B = 10, a = 11;
$ C = ++ $ a; c = 12, a = 12;
$ D = $ a --; d = 12, a = 11;
$ E = -- $ a; e = 10, a = 10;
There is also an operator called @ a, which allows php to ignore the failure of a specific function call.
The last operator-shell command execution program. For this purpose, you need to include the command between the anti-apostrophes ('), so that the command will be passed to the shell for execution, but this will generate security.
2.5.2 process of combining expressions and operators
Additional information of combined direction Operators
Not combined with clone new clone and new
Left [array ()
Non-union ++ -- increment/decrease Operator
Not associated ~ -(Int) (float) (string) (array) (object) (bool) @ Type
Uncombined instanceof type
Right join! Logical operators
Left */% Arithmetic Operator
Left +-. Arithmetic Operators and string Operators
Left <> bitwise Operator
Non-union <<=>=<> comparison operator
Not combined =! ===! = Comparison operator
Left & bit operators and references
Left ^ bit operator
Left | bitwise Operator
Left & logical operators
Left | logical operator
Left? : Ternary Operator
Right = + =-= * =/=. = % = & =|=^= <<=>> = value assignment operator
Left and logical operators
Left xor logical operator
Left or logical operators
Left, used in multiple places
. 6 control structure
2.6.1 if statement
1. if (expr)
Statement
Else
2. elseif/else if 2.6.2 switch statement
Copy codeThe Code is as follows:
<? Php
If ($ a = 5 ):
Echo "a equals 5 ";
Echo "...";
Elseif ($ a = 6 ):
Echo "a equals 6 ";
Echo "!!! ";
Else:
Echo "a is neither 5 nor 6 ";
Endif;
?>
The switch statement is similar to a series of IF statements with the same expression. In many cases, you need to compare the same variable (or expression) with many different values and execute different codes based on the value it equals. This is exactly the purpose of the switch statement.
Copy codeThe Code is as follows:
<? Php
If ($ I = 0 ){
Echo "I equals 0 ";
} Elseif ($ I = 1 ){
Echo "I equals 1 ";
} Elseif ($ I = 2 ){
Echo "I equals 2 ";
}
Switch ($ I ){
Case 0:
Echo "I equals 0 ";
Break;
Case 1:
Echo "I equals 1 ";
Break;
Case 2:
Echo "I equals 2 ";
Break;
}
?>
2.6.3 while/do... while loop
While (expr)
Block
Do
Block
While (expr );
Copy codeThe Code is as follows:
<? Php
Do {
If ($ I <5 ){
Echo "I is not big enough ";
Break;
}
$ I * = $ factor;
If ($ I <$ minimum_limit ){
Break;
}
Echo "I is OK ";
/* Process I */
} While (0 );
?>
2.6.4 for Loop
For (expr1; expr2; expr3)
Block
Expr1: When a FOR loop is first encountered, it is executed once. Loop iteration starts after execution.
Expr2: Calculate it before each iteration. If it is true, the code block is executed.
Expr3-calculate it after each iteration
Copy codeThe Code is as follows:
<? Php
/* Example 1 */
For ($ I = 1; $ I <= 10; $ I ++ ){
Echo $ I;
}
/* Example 2 */
For ($ I = 1; $ I ++ ){
If ($ I> 10 ){
Break;
}
Echo $ I;
}
/* Example 3 */
$ I = 1;
For (;;){
If ($ I> 10 ){
Break;
}
Echo $ I;
$ I ++;
}
/* Example 4 */
For ($ I = 1, $ j = 0; $ I <= 10; $ j + = $ I, print $ I, $ I ++ );
?>
2.6.5 foreach loop: used for specific types. More details in Unit 5
2.6.6 interrupt loop: break and continue