Home > Backend Development > PHP Tutorial > PHP and Mysql web application development core technologies - Part 1 Php basics - 2 PHP language introduction_PHP tutorial

PHP and Mysql web application development core technologies - Part 1 Php basics - 2 PHP language introduction_PHP tutorial

WBOY
Release: 2016-07-21 15:26:29
Original
866 people have browsed it

The main topic is
. Variable expansion system in php strings
. More data types available in php
. Conversion between types
. Entering and using variables and constants
. How to build expressions in php and the operators needed to build expressions
. Using the control structures available in the language
.1 More introduction to input strings

Copy code The code is as follows:

$hour = 16;
$kilometres = 4;
$content = "cookie";
echo " 4pm in 24 hour time is {$hour}00 hours.
n";
echo <<There are {$kilometres }000m in {$kilometres}km.

The jar is now, indeed, full of ${content}s.

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 wish to To generate the exact character sequence {$ 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 gets a set of initial values ​​and returns an array object that holds all these values. By default, an integer name or key starting from 0 is assigned to the value
in the array. You can also specify new ones to be added. The index of the item. $frunit[120]="nespola";But you can also specify the key using a string value instead of the default number assigned to it.
$myfavourite=array("car"=>"ferrari","number"=>21,"city"=>"ouagadougou");

Array operator example name result
$a + $b union the union of $a and $b.
$a == $b equal TRUE if $a and $b have the same key/value pair.
$a === $b congruent TRUE if $a and $b have the same key/value pairs and are of the same order and type.
$a != $b does not equal TRUE if $a does not equal $b.
$a <> $b does not equal TRUE if $a does not equal $b.
$a !== $b

Copy code The code is as follows:

$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(10) "strawberry" ["c"]=> string(6) "cherry" } 2.2.2 Objects will be used in object-oriented programming in the fourth unit. 2.2.3 Special types and values ​​NULL is a special type and value in PHP, which means "no value". It is null if it meets the following requirements: . They are set to the case-sensitive keyword null; . They have never been assigned a value .Clear them explicitly using the unset method. Resources: Sometimes, PHP needs to handle objects that do not necessarily come from PHP, such as handles to database or operating system objects. They are called special variables of resources.
.3 Casting
2.3.1 Basics
Implicit cast: The most common situations when encountering implicit cast are:
. Binary operation operator
. Boolean expressions and expression operator
. Specific methods that require strings - specific methods and operators, such as echoprint or string concatenation (.)
Show mandatory 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 cast type conversion
Convert to integer
(int) 4.999
Note: null is always converted to the integer value 0.
Convert to float Points
(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 trust the result of such conversion
Convert to character String
can use the type conversion operator (string) or call strval to convert the variable into a string.
Boolean true is converted to string 1, false is converted to empty string ("")
null is converted to Empty string ('").
Convert to array
You can use type conversion (array) or the function arrayr to convert a variable or expression into an array
Null and other unset variables are converted to 0 Convert an empty array of elements to an object
You can use type conversion (object) to convert a variable or expression to an object. Create an object of type stdClass.
2.3.3 Useful cast function
is_type()
.is_integer,.is_float,.is_bool,is_null,.is_object. Returns a Boolean type, indicating whether a specific variable belongs to The appropriate type.
gettype() is a very useful routine that tells you what type PHP currently thinks a variable or expression is. It is not recommended to use this conversion function with two parameters: The variable to be converted and the type to be converted to, which represents a string
.4 Variables and constants
2.4.1 Define constants
In PHP programs, use the language structure define to define constants, and the constant names are not Start with the character $, and their values ​​can only be of specific types: integers, floats, strings and booleans
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 variable, its value is copied. This method works for all types except objects
For object variables and resources, all the content copied is the handle of the underlying object or resource, but the underlying object of the operation is the same.
Another option for assigning the value of a variable to another variable is assignment by reference. Done with the & prefix.
$a=123;
$b=&$a;
2.4.3 Scope of variables
Function level variables, internally declared variables are only legal within this function.
Variables declared outside the function
Super global variables
2.4.4 Lifetime of variables
Whether executing the same script or different scripts, PHP will not remember anything between calls.
2.4.5 Predefined variables
php provides many predefined variables, which give information about the operating environment, most of which are super global arrays. For example:
$GLOBALS-it contains globally available variables within the executing script. References to all variables
$_SERVER - information about the environment surrounding the script
$_SESSION, $_COOKIE - it contains information about managing visitors and about storage methods called "cookies"
$_REQUEST - it contains $_post, $_GET and $_session arrays
$_ENV - It contains the environment variables of the process where the php language engine is located. The keys of the array are the names of the environment variables.
$php_errormsg - It saves the latest error message generated by the PHP language engine when executing the current script.
.5 Expressions and operators
2.5.1 Operators: Combination expressions
Assignment:
Arithmetic Operator
Example Name Result
-$a Negates the negative value of $a.
$a + $b adds the sum of $a and $b.
$a - $b subtracts the difference between $a and $b.
$a * $b multiplies the product of $a and $b.
$a / $b division The quotient of $a divided by $b.
$a % $b modulo the remainder of $a divided by $b.
Comparison Operators

Example Name Result
$a == $b equals TRUE if $a equals $b.
$a === $b is congruent TRUE if $a is equal to $b and they are of the same type. (Introduced in PHP 4)
$a != $b not equal TRUE if $a is not equal to $b.
$a <> $b not equal TRUE if $a is not equal to $b.
$a !== $b Not Congruent TRUE if $a is not equal to $b, or their types are different. (Introduced in PHP 4)
$a < $b is less 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 (logical AND) TRUE if $a and $b are both TRUE.
$a or $b Or (logical OR) TRUE if either $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 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 either $a or $b is TRUE.
Bitwise Operator

Operator Name Result
$a & $b And (bitwise AND) will set the bits in $a and $b that are both 1 to 1.
$a|| $b Or (bitwise OR) will set the bit in $a or $b that is 1 to 1.
xor ^ $b Xor (bitwise exclusive OR) will set different bits in $a and $b to 1.
Not $a Not (bitwise not) Sets bits in $a that are 0 to 1, and vice versa.
$a << $b Shift left Moves the bits in $a to the left $b times (each move means "multiply by 2").
$a >> $b Shift right Moves the bits in $a to the right $b times (each move means "divide by 2").
String Operator
Concatenation Operator. It operates on two strings and returns a single string concatenating the two together.
Array Operator

Example Name Result
$a + $b union the union of $a and $b.
$a == $b equal TRUE if $a and $b have the same key/value pair.
$a === $b congruent TRUE if $a and $b have the same key/value pairs and are of the same order and type.
$a != $b is not equal TRUE if $a is not equal to $b.
$a <> $b does not equal TRUE if $a does not equal $b.
$a !== $b is not equal TRUE 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 specific function call.
The last operator - shell command executor. For this purpose, the command needs to be enclosed in backticks (`) so that the command is passed to the shell for execution. However, this creates security.
2.5.2 The process of combining expressions and operators

Additional information on combined direction operators
Non-associative clone new clone and new
Left [array()
Non-associative + + -- Increment/decrement operator
non-associative~ - (int) (float) (string) (array) (object) (bool) @ type
non-associative instanceof type
right associative! Logical operation Operator
left*/% Arithmetic operator
left+-. Arithmetic operator and string operator
left<< >> Bit operator
non-associative< < = > > 🎜>Left| Bit operator
Left&& Logical operator
Left|| Logical operator
Left? : Ternary operator
Right= += -= *= /= .= % = &= |= ^= <<= >>= assignment operator
left and logical operator
left xor logical operator
left or logical operator
left, multiple Used everywhere
.6 Control structure
2.6.1 if statement
1. if (expr)
statement
else
2. elseif/else if 2.6.2 switch statement



Copy code
The code is as follows: 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;
?>


switch statement and a series of IF statements with the same expression resemblance. There are many situations where you need to compare the same variable (or expression) with many different values ​​and execute different code depending on which value it equals. This is exactly what the switch statement is for.


Copy code
The code is as follows: 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 code
The code is as follows:

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 Execute it once when a FOR loop is encountered. After the execution is completed, the loop iteration begins.
expr2: Calculate it before each iteration. If true, execute the code block.
expr3 - calculate it after each iteration
Copy code The code is as follows:

/* 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 explained in Unit 5
2.6.6 Breaking Loops: break and continue

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323903.htmlTechArticleThe main topic is. Variable extension system in php string. More data types available in php. Types Conversion between. Entering and using variables and constants. How to build expressions in php and construct...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template