PHP and Mysqlweb application development core technologies part 1 introduction to Php basics 2 php language

  • 2020-05-09 18:19:39
  • OfStack

The main theme is
The variable in the.php string extends the system
More data types available in.php
.conversion between types
Enter and use variables and constants
How do I build expressions in php and the operators needed to build them
. Use the control structures available in the language
More on input strings.1

 
<?php 
$hour = 16; 
$kilometres = 4; 
$content = "cookie"; 
echo " 4pm in 24 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 24 hour time is 1600 hours.
There are 4000m in 4km.
The jar is now, indeed, full of cookies.
If you want to generate the exact sequence of characters {$in the output, you need to escape it with {\$.
More on data types.2
1. Arrays: declare arrays using the array method. It takes an initial set of values and returns the array object that holds all of them, assigning the integer name or key (key) from 0 to the values in the array by default
, you can also specify the index of the new item to add. $frunit [120] = "nespola"; But you can also specify the key using a string value, rather than the default number assigned to it.
$myfavourite=array("car"= > "=" ferrari ", "number > 21,"city"= > "ouagadougou");

Array operator example name result
$a + $b combines $a and $b.
$a == $b is equal to $TRUE if $a and $b have the same key/value pair.
$a === $b congruent if $a and $b have the same key/value pair in the same order and type.
$a! = $b = $TRUE if $a does not equal $b.
$a < > $b not equal $TRUE if $a does not equal $b.
$a !== $b

 
<?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, 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 special type and value NULL is the special type and value in php, which means "no value ". They are never assigned. They are explicitly cleared using the unset method. Resources: sometimes, php needs to handle objects that are not necessarily from php, such as handles to database or operating system objects. They are called special variables for the resource.
.3 cast type conversion
2.3.1 basis
Implicitly cast: the most common case when an implicitly cast is encountered is:
Base 2 operator
Boolean expressions and expression operators
.specific methods that require strings -- specific methods and operators, such as echo\print or string concatenation (.)
Show cast: prefixes variables (or expressions) with the type in parentheses, and php will try to cast them for you.
(int)\(interger)
(string)- converts to a text string
(object)- convert to object
2.3.2 special cast type conversion
Convert to integer
(int) 4.999
Note: null is always converted to an integer value of 0.
Convert to a floating point number
(float)true=1.0
The result of converting an array, object, or resource to a floating point value is undefined. Do not attempt this new conversion or believe in the result of such a conversion
Convert to a string
You can use the type converter (string) or call strval to convert the variable to a string.
Boolean true to string 1, false to empty string (" ")
null to empty string ('").
Convert to array
You can use type conversion (array) or the function arraryr to convert a variable or expression to an array
null and other unset variables are converted to an empty array with 0 elements
Convert to object
You can use type conversion (object) to convert variables or expressions into objects.
Converting an object to an object simply returns a handle to the same object. Create objects of type stdClass for all other types.
2.3.3 useful cast type conversion function
is_type()
.is_integer,.is_float,.is_bool,is_null,.is_object. Returns a Boolean, indicating whether a particular variable is of the appropriate type.
gettype() is a very useful routine that tells you what type php currently thinks a variable or expression is. This conversion function is not recommended.
settype() takes two arguments: the variable to be converted and the type to be converted, which represents a string.
.4 variables and constants
2.4.1 define constants
In the php program, the language structure define is used to define constants whose names do not begin with the character $, and whose values can only be of certain types: integers, floating point Numbers, strings, and bores
2.4.2 variables by value and by reference
By default, most variables and all constants are assigned by value. When the value of one variable is assigned to another, the value is copied. This works for all types except objects
For object variables and resources, all that is copied is a handle to the underlying object or resource, but the underlying object for the operation is the same.
Another option for assigning the value of a variable to another variable is to assign it by reference. Done with the ampersand prefix.
$a=123;
$b=&$a;
2.4.3 scope of variables
Function level variables, variables declared internally are only legal within this function.
A variable declared outside a function
Super global variable
2.4.4 lifetime of variables
Whether the same script is executed or a different script is executed, php does not remember anything between calls.
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 available globally within the script being executed
$_SERVER- information about the surrounding environment of the script
$_SESSION, $_COOKIE- it contains information about managing visitors and how to store them called "cookie"
$_REQUEST- it contains $_post, $_GET, and $_session arrays
$_ENV- it contains the environment variable for the process where the php language engine resides. The key of the array is the name of the environment variable.
$php_errormsg- it holds the latest error message generated by the php language engine when executing the current script.
.5 expressions and operators
2.5.1 operator: composite expression
Assignment:
Arithmetic operator
Example name result
-$a takes the negative value of minus $a.
$a + $b plus $a plus $b.
$a - $b - the difference between $a and $b.
$a * $b times the product of $a and $b.
$a / $b division $a divided by $b.
$a % $b take the remainder of $a divided by $b.
Comparison operator

Example name result
$a == $b = TRUE, if $a = $b.
$a === $b congruent TRUE, if $a is equal to $b and they are of the same type. (introduced by PHP 4)
$a! = $b not equal to TRUE, if $a does not equal $b.
$a < > $b does not equal $TRUE, if $a does not equal $b.
$a! == $b partial TRUE, if $a does not equal $b, or if they are of a different type. (introduced by PHP 4)
$a < $b is smaller than TRUE if $a is strictly less than $b.
$a > $b is greater than TRUE, if $a is strictly $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 operator
Example name result
$a and b And TRUE, if $a and $b are both TRUE.
$a or b Or (logical or) TRUE, if $a or $b either 1 is TRUE.
$a xor b TRUE if $a or $b either 1 is TRUE, but not at the same time is TRUE.
$a Not (logical non) TRUE, if $a is not TRUE.
$a&& $b And TRUE if $a and $b are both TRUE.
$a || $b Or (logical or) TRUE, if $a or $b either 1 is TRUE.
Bitwise operator

Operator name result
$a & $b And will set the bit 1 in both $a and $b to 1.
$a|| $b Or (by bit or by bit) sets the bit 1 in $a or $b to 1.
xor ^ $b Xor will set the different bits in $a and $b to 1.
Not $a Not sets the zero bit in $a to 1 and vice versa.
$a < < $b Shift left (left shift) moves the bit in $a $b to the left $b times (each move means "multiply by 2").
$a > > $b Shift right (right shift) moves the bit in $a to the right $b times (every move means "divide by 2").
String operator
Concatenation operator. It operates on two strings and returns a single 1 string connecting the two to one
Array operator

Example name result
$a + $b combines $a and $b.
$a == $b is equal to $TRUE if $a and $b have the same key/value pair.
$a === $b congruent if $a and $b have the same key/value pair in the same order and type.
$a! TRUE if $a does not equal $b.
$a < > TRUE if $a does not equal $b.
$a! == $b incomplete if $a is not 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 tells php to ignore the failure of a particular function call.
The last operator - shell command executes the program. For this, you need to enclose the command between the backapostrophe (') so that the command is passed to the shell for execution, but this creates security.
2.5.2 the process of combining expressions and operators

Combines the orientation operator to attach information
Uncombined clone new clone and new
Left [array ()
Unassociative ++ -- increment/decrement operator
Unbinding ~ - (int) (float) (string) (array) (object) (bool) @ type
Non-binding instanceof type
The right combination. Logical operator
Left * / % arithmetic operator
Left + -. Arithmetic operator and string operator
On the left < < > > An operator
The combination of < < = > > = < > Comparison operator
Non-union ==! = = = =! == comparison operator
Left & bit operators and references
Left ^ bit operator
Left | bit operator
Left && logical operator
Left |, | logical operator
On the left? : 3 meta operator
Right = += -= *= /=.= %= &= |= ^= < < = > > = assignment operator
Left and logical operator
Left xor logical operator
Left or logical operator
Left, you use it a lot
.6 control structure
2.6.1 if statement
1. if (expr)
statement
else
2. elseif/else if 2.6.2 switch statement
 
<?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 the IF statement of the 1 series that has the same expression. There are many situations where you need to compare the same variable (or expression) to many different values and execute different code based on which value it is equal to. This is exactly what the switch statement does.
 
<?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 cycle
while(expr)
block
do
block
while (expr);

 
<?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 cycle
for(expr1;expr2;expr3)
block
expr1: when the first FOR loop is encountered, execute it once. The loop iteration begins after execution.
expr2: calculate it before each iteration. If true, the code block is executed.
expr3- evaluate it after each iteration
 
<?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: for a specific type. More on this in unit 5
2.6.6 interrupt loops: break and continue

Related articles: