


PHP and Mysql web application development core technologies - Part 1 Php basics - 2 PHP language introduction_PHP tutorial
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
$hour = 16;
$kilometres = 4;
$content = "cookie";
echo " 4pm in 24 hour time is {$hour}00 hours.
n";
echo <<
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
$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
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
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
block
while (expr);
Copy code
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
/* 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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
