0x01 PHP data type
Chapter 1: Basic Data Types
Integer int
Floating-point type float

$f1 = 1.23;
$f2 = 1.23e10;
$f3 = PHP_INT_MAX + 1; When an integer exceeds the maximum storage capacity, it will use a floating-point type for storage
var_dump($f1, $f2, $f3);
String String
Boolean type Boolean
Boolean type: two values true and false, used for judgment and comparison
Chapter 2: Complex Data Types
Array array
Object object
Chapter 3: Special Data Types
Resource type Resource
Null
0x02 PHP operator
Original code & one's complement & two's complement
The original code, one's complement, and two's complement of a positive number are the same
One's complement: the sign bit remains unchanged, and the other bits are inverted
Two's complement: one's complement + 1
5
Original code: 10000101 The original code, one's complement, and two's complement of a positive number are the same
One's complement: 11111010 The sign bit remains unchanged, and other bits are inverted
Two's complement: 11111011 One's complement + 1
Bitwise operators
Note:
1、When the system performs bitwise operations, it always uses the two's complement
2、After the operation is completed, it must be converted to the original code to be the final data to be displayed
Bitwise operations: take the smallest unit bit of the computer for operation
&: Bitwise AND, 1 if both are 1, otherwise 0
|: Bitwise OR, 1 if at least one is 1, otherwise 0
~: Bitwise NOT, 0 if a bit is 1, otherwise the opposite
^: Bitwise XOR, 0 if the same, 1 if different
<<: Bitwise left shift, the entire bit (32 bits), move one bit to the left, and fill the right with 0
>>>: Bitwise right shift, the entire bit (32 bits), move one bit to the right, and fill the left with the sign bit (positive number fills 1, negative number fills 0)
0x03 PHP flow control
Control classification
1、Sequential structure
Code is executed from top to bottom in order (the most basic structure of code execution)
2、Branch structure
Given a condition, there are multiple executable code blocks, and a specific code block is executed according to the condition
If branch
Given a condition, set multiple (two) cases for the condition, and limit the specific execution section through condition judgment
Simplified if: only one code block, which can be executed or not
if(condition expression){
//The content to be executed when the condition is satisfied //Sequential structure}
$day="sunday1";
if($day=="sunday"){
echo 'go out';
}
Basic if: has two sides, either satisfying or not meeting the condition has corresponding execution code
if(condition expression)
{
//The content to be executed when the condition is satisfied }
else{
//The code block to be executed under the condition of dissatisfaction
}
Complex if: after the judgment condition, there are usually two results, satisfaction or dissatisfaction, and further condition judgment is made after dissatisfaction
if(condition expression1)
{//The code block to be executed when the condition expression is met}
elseif(condition expression)
{//The code block to be executed when condition 2 is met}
else{//The code block to be executed when condition 1 and 2 are not met}
Switch branch
There is a set of scenarios, through a condition, usually with multiple values, but each value will have a corresponding different code block
Switch judgment method: conditions are placed inside the branch structure for judgment
Basic syntax of switch:
switch(condition expression){
//All condition judgments: proceed one by one
case value1:
The code block to be executed;
break;
The match condition will automatically execute all the codes below in order (except case codes), and break identifier needs to be executed by the terminal to end the switch.
case value 2:
Code block to be executed:
break;}
//Branch structure: switch branch
//Do different things based on the date
$day=1;
switch ($day){
//$day is a specific value
case 1:
echo '1';
break;
case 2:
echo '2';
break;
case 3:
echo '3' ;
break;
default:
echo 'error';
break;
}
3. Loop structure
Within a certain condition control range, the specified code block can be executed repeatedly
for loop: executes through condition, start, and end judgment
for(condition expression 1; condition expression 2; condition expression 3)
{
//Condition expression 1: defines initialization condition, can have multiple assignment statements, separated by commas
//Condition expression 2: boundary judgment, limits the number of loop executions
//Condition expression 3: used to execute condition change
//Loop body
}
for loop execution principle
1. Execute condition expression 1: define initialization condition (execute 1 time)
2. Execute expression 2: judge condition (N times)
2.1 If the condition is met, execute the loop body
2.2 If the condition is not met, execute the loop
3. Execute loop body (N times)
4. Execute condition expression 3: variable change (N times)
5. Execute expression 2: judge condition (N times)
6. Repeat steps 342: until the second step does not meet the condition and the loop ends
for loop special usage: unconditional for loop, which will enter an infinite loop, avoid this
for(;;)
{
echo "hello world";
}
while loop: terminates through condition judgment
while basic syntax:
condition initialization
while(condition expression){
//Condition expression is to judge the boundary condition
loop body; //Change of loop condition
//while loop
$i=1;
while ($i<=10)
{
echo $i,'<br>';
$i++;
}
for and while selection
1. If it is based on a fixed known condition (numeric and with regular changes), use the for loop
2. while can make flexible condition judgments (while is used more often)
do while loop: similar to while
while first performs a condition judgment, then executes the loop body, it may fail directly if the condition is not met on the first try. do while first executes the loop body, then judges the condition (at least the loop body will be executed once)
do-while basic syntax:
do{}while()
//do-while loop //Output even numbers between 1-10 $i=1; do { if ($i%2!=1) { // !=1 indicates an even number echo $i,'<br/>'; } //Condition change every time $i++; }while($i<=10);
Loop control: Control the loop itself within the loop
Interrupt control: Restart the loop, and the loop body will also execute other content
continue level //Default is 1 (loops can be nested multiple levels)
Termination control: The loop ends directly
break level: Default is 1
//Loop control
//Output numbers before 100 that are multiples of 5
$i=1;
while($i<=100){
if ($i%5!=0){
//Restart the loop
$i++;
continue;
}
echo $i++,'<br>';
}
Because loops often encounter nesting (loops within loops), if there are certain conditions within the loop that can clearly determine that the current loop (which is the outer loop) does not need to continue executing, then loop control can be used to achieve this, and the inner loop can also control the outer loop, which is achieved through hierarchical parameters
continue2: The current inner loop does not execute after the current loop, and if there is another loop body in the outer loop, it also does not execute and starts over
break2: The current inner loop ends, and the outer loop also ends (if there is no external loop affected, continue to execute)
foreach loop: specifically for arrays
Flow control alternative syntax
Flow control syntax: alternative syntax for branch and loop structures
PHP itself is a script language embedded in HTML, and some syntax related to judgment or loop structures needs to be written in HTML, which must comply with PHP tag specifications. It requires a mix of HTML and PHP, and using raw PHP code would be very unattractive
Print a 99 multiplication table, using a table to display
<table border=1>
<?php for ($i=1;$i<10;$i++){?>
<tr>
<?php for($j=1;$j<=$i;$j++){?>
<td><?php echo $i."*".$j.'='.$i*$j;?>
</td>
<?php }?>
</tr>
<?php }
?>
</table>
//These curly braces {} in PHP written to HTML are very unattractive, so PHP provides an alternative mechanism that allows it to write without curly braces
for(;;){ for(;;): endfor;
PHP should only output data in HTML, and the output is usually accompanied by conditional judgment and loop operations, therefore, PHP provides alternative syntax for corresponding branch structures and loop structures, all of which follow a single pattern
Left curly brace { :
Replace the actual label with end+
Introduction to common flow control algorithms and solutions in high concurrency scenarios
php hiring challenge question in hacker earth(How to take input?)
GuLoader malicious software downloader: from 'COVID-19' phishing to 'SF Express' phishing
1. Send authentication and scheduled task logs (auth, authpriv, cron)
php hiring challenge question in hacker earth
Flexible, quick, and low-maintenance cost data integration method: Data Federation Architecture

评论已关闭