The main theme is
. variable extension System in PHP string
. More data types available in PHP
. Conversions between types
. Enter and use variables and constants
. How to build an expression in PHP and the operators needed to build an expression
. Use the control structure available in the language
.1 more about the input string
Copy Code code as follows:
<?php
$hour = 16;
$kilometres = 4;
$content = "Cookie";
echo "4pm in-hour time is {$hour}00 hours.<br/>\n";
Echo <<<done
There are {$kilometres}000m in {$kilometres}km.<br/>
The jar is now, indeed, full of ${content}s.<br/>
Done;
?>
Output: 4pm in hour time is 1600 hours.
There are 4000m in 4km.
The jar is now, indeed, and full of cookies.
If you want to generate an exact sequence of characters in the output {$, you need to use, {\$ to escape it.
.2 more introduction to data types
1. Array: Use the array method to declare an array. It obtains a set of initial values and returns the array object that holds all of these values, by default, assigns the integer name or key (key) starting from 0 to the value in the array
, or you can specify the index of the new item to add. $frunit [120]= "Nespola"; but you can also use a string value to specify a key, instead of using the default number assigned to it.
$myfavourite =array ("Car" => "Ferrari", "number" =>21, "City" => "Ouagadougou");
Array operator Example name result
$a + $b union $a and $b.
$a = = $b equal if $a and $b have the same key/value pair. TRUE.
$a = = = $b congruent if $a and $b have the same key/value pairs and the order and type are the same, true.
$a!= $b is TRUE if $a is not equal to $b.
$a <> $b is TRUE if $a is not equal to $b.
$a!== $b
Copy Code code 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 $a
echo "Union of \ $b and \ $a: \ n";
Var_dump ($c);
?>
After execution, this script will display:
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 (a) "Strawberry" ["C"]=> string (6) Cherry The 2.2.2 object will be used in the object-oriented program design in unit fourth. 2.2.3 Special types and value NULL are special types and values in PHP that represent "no value." It is null:. They are set to case-sensitive keyword null; they are never assigned. Use the Unset method to explicitly clear them. Resources: Sometimes, PHP needs to deal with objects that are not necessarily from PHP, such as the database or the handle of an operating system object. They are called special variables for resources.
.3 Force Type conversions
2.3.1 Foundation
Implicit coercion type conversions: The most common scenario when an implicit coercion type conversion is encountered is:
. Binary operator
. Boolean expressions and expression operators
-specific methods that require a string-specific methods and operators, such as Echo\print or string connectors (.)
Show coercion type conversions: a type prefix enclosed in parentheses for a variable (or expression) that PHP will attempt to convert for you.
(int) \ (interger)
(string)-Convert to text string
(object)-Convert to Object
2.3.2 Special Coercion type conversion
Convert to Integer
(int) 4.999
Note: null is always converted to an integer value of 0.
Converting to floating point numbers
(float) true=1.0
The result of converting an array, object, or resource to a floating-point value is undefined, and do not attempt to do this new conversion or believe that the conversion result
Convert to String
You can use a type conversion character (string) or call Strval to convert a variable to a string.
Boolean true to string 1,false converted empty string ("")
Null to an empty string (' ").
Converting to arrays
You can use type conversion (array) or function Arraryr to convert a variable or expression to an array
Null and other variables that are not set are converted to an empty array with 0 elements
Convert to Object
You can use type conversion (object) to convert a variable or an expression to an object.
Converting an object to an object simply returns a handle to the same object. Creates an object of type Stdclass for all other types.
2.3.3 useful coercion type conversion functions
Is_type ()
Is_integer,.is_float,.is_bool,is_null,.is_object. Returns a Boolean that indicates whether a particular variable belongs to the appropriate type.
GetType () is a very useful routine that tells you what PHP currently considers a variable or an expression to be. Use of this conversion function is not recommended.
Settype () uses two parameters: the variable to convert and the type to convert to, which represents a string.
.4 Variables and constants
2.4.1 Define Constants
In a PHP program, you use the language structure define to define constants, and constant names do not begin with the character $, and their values can only be of a specific type: integers, floating-point numbers, strings, and Booleans
2.4.2 by value and by reference variables
By default, most variables and all constants are assigned by value. When you assign the value of a variable to another variable, its value is copied. This method applies to all types except objects
For object variables and resources, all copied content is a handle to the underlying object or resource, but the underlying object of the operation is the same.
An alternative way to assign a variable's value to another variable is to assign a value by reference. Completed with the & prefix.
$a = 123;
$b =& $a;
Range of 2.4.3 variables
Function-level variables, internally declared variables are only valid within this function.
Variables declared outside the function
Super global variable
Lifetime of 2.4.4 variables
PHP doesn't memorize anything between calls, whether it's executing the same script or a different script.
2.4.5 Predefined variables
PHP provides a number of predefined variables that give information about the operating environment, most of which are super global arrays such as:
$GLOBALS-it contains references to all variables that are globally available within the script being executed
Information about the surrounding environment where the $_server-script resides
$_session, $_cookie-It contains information about managing visitors and how storage is referred to as "cookies"
$_request-it contains $_post, $_get, and $_session arrays
$_env-it contains the environment variables for the process where the PHP language engine is located. The key of the array is the name of the environment variable.
$php _errormsg-it to save the most recent error message generated by the PHP language engine when it executes the current script.
.5 Expressions and operators
2.5.1 Operator: Combining expressions
Assign value:
Arithmetic operator
Example name result
-$a take negative values of the inverse $a.
$a + $b addition $a and $b.
$a-the difference between $b subtraction $a and $b.
$a * $b multiplication $a and $b.
$a/$b division $a divided by $b quotient.
$a% $b modulo $a divided by the remainder of the $b.
comparison operator
Example name result
$a = = $b equal to TRUE if $a equals $b.
$a = = = $b congruent TRUE if $a equals $b, and they are of the same type. (PHP 4 Introduction)
$a!= $b Unequal TRUE If $a is not equal to $b.
$a <> $b is not equal to TRUE if $a is not equivalent to $b.
$a!== $b are not congruent TRUE if $a is not equal to $b, or they are of different types. (PHP 4 Introduction)
$a < $b small and TRUE if $a are strictly smaller than $b.
$a > $b is greater than TRUE if $a strictly $b.
$a <= $b is less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b 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 $a and $b are true.
$a or $b or (logical OR) true if either $a or $b is true.
$a XOR $b xor (Logical XOR OR) True if either $a or $b is true, but not at the same time.
$a not (logical not) True if the $a is not true.
$a && $b and (logical AND) true if $a and $b are true.
$a | | $b or (logical OR) true if either $a or $b is true.
Bitwise operator
Operator name Result
$a & $b and (bitwise) will set the $a and the bits of the $b 1 to 1.
$a | | $b or (bitwise OR) sets the bit 1 in the $a or $b to 1.
Xor ^ $b XOR (bitwise exclusive OR) sets the different bits in the $a and $b to 1.
Not $a not (bitwise not) sets a bit of 0 in the $a to 1, and vice versa.
$a << $b shift left (move right) moves the bits in the $a to the left $b times (each time a move is "multiplied by 2").
$a >> $b shift Right (move) moves the bits in the $a to the right $b times (each move is "divided by 2").
String operators
The connection operator. It operates on two strings and returns a single string that joins them together
Array operators
Example name result
$a + $b union $a and $b.
$a = = $b equal if $a and $b have the same key/value pair. TRUE.
$a = = = $b congruent if $a and $b have the same key/value pairs and the order and type are the same, true.
$a!= $b is TRUE if $a is not equal to $b.
$a <> $b is TRUE if $a is not equal to $b.
$a!== $b is not congruent with TRUE if $a is not all equal to $b.
Other operators
Auto increment and auto decrement 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 lets PHP ignore the failure of a particular function call.
Last operator-shell command execution program. In order to do this, you need to enclose the command between the anti-apostrophe ('), so that the command is passed to the shell for execution. But that would create security.
2.5.2 the process of combining expressions and operators
Combining directional operators with additional information
Non-binding clone new clone and new
Left [Array ()
Non-associative + +--increment/decrement operator
Non-binding ~-(int) (float) (string) (array) (object) (BOOL) @ type
Non-binding instanceof type
Right combination! logical operators
Left */% arithmetic operator
Left +-. Arithmetic operators and string operators
Left << >> Bitwise operators
Non-union < <= > >= <> comparison operator
Non-binding = =!= = =!== comparison operator
Left & bitwise operators and references
Left ^ bit operator
Left | Bitwise operators
Left && logical operators
Left | | logical operators
Left? : Ternary operator
right = + = = *=/=. =%= &= |= ^= <<= assignment operator
Left and logical operators
Left XOR logical operator
Left or logical operator
Left, multiple places to use
.6 Control Structure
2.6.1 If statement
1. if (expr)
Statement
Else
2. Elseif/else if 2.6.2 switch statement
Copy Code code 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
?>
A switch statement is similar to a series of IF statements that have the same expression. There are many situations where you need to compare the same variable (or expression) with a number of different values and execute different code based on which value it equals. This is the purpose of the switch statement.
Copy Code code 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
Todo
Block
while (expr);
Copy Code code as follows:
<?php
do {
if ($i < 5) {
echo "I am not a 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: The first time you encounter A for loop to execute it. Start the loop iteration after the execution completes.
EXPR2: Compute it before each iteration. If true, the code block is executed.
Expr3-calculates it after each iteration
Copy Code code as follows:
<?php
/* Example 1 * *
for ($i = 1; $i <= $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 <= $j + = $i, print $i, $i + +);
?>
2.6.5 foreach Loop: for a specific type. More explanations in Unit 5
2.6.6 Interrupt loops: Break and continue