034 PHP foundation, variables, operators, process control

1: Basic overview of php

The syntax of PHP absorbs the characteristics of popular computer languages such as C language, Java and Perl, which is easy for ordinary programmers to learn
The main goal of PHP is to allow web developers to write dynamic pages quickly
Simply put, PHP is mainly used to develop various types of website programs
Script suffix: php
Mixed with html language, the suffix of the script is still php
To be more specific, PHP can be used to collect form data, generate dynamic web pages, process strings, dynamically output images, process server files, interact with databases, and track sessions,
Handle xml files, support a large number of network protocols, and other related operations on the server
PHP is a script language and an interpretive language. It can run on the server without compiling in advance
PHP can run on windows, Linux and other operating systems
windows: phpstudy
Linux: separate installation

1.1: overview of web working principle:

1,browser
2,input url address
3,Show site content

1.2: PHP running environment installation

Apache + PHP + MySQL yes PHP More popular living environment
LAMP

1.3: php language markup

Start tag:<?php
 End tag:?>

1: <?php ?>This means entering PHP Mode, content outside the start and end tags will be deleted PHP Parser ignored
2: Can be embedded directly into html Code, and can be embedded into html Anywhere in the code
3: In a html Any number of can be embedded in the document PHP sign
4: At the end of the file PHP The code snippet end tag can be omitted

1.4: instruction separator [;]

php Use a semicolon to indicate the end of a sentence
 End flag?>It implies a semicolon, so PHP The last line of code can be left without a semicolon

1.5 notes:

The comments will not be displayed in the browser's source code, PHP The running environment is the server. What we see on the browser side is PHP Engine running PHP Execution results after code
/* multiline comment  */
// Single-Line Comments 

1.6: Processing in case of blank

Blank characters include: spaces, tabs tab,Line feed
	These in PHP It doesn't matter (note that they are all input in English input method). You can expand a sentence into one line or compact it in one line,
	The rational use of spaces and blank lines can enhance the clarity and readability of the code. If the use is unreasonable, it will burden the reading.

1.7: simple php statement

1 phpinfo();
2 echo	For outputting simple variables
3 var_dump();
	Used to output variable values and their variable types

2: php variable

2.1: General

Variables are containers that temporarily store values
 in addition PHP Scripting language is a weakly typed language. Unlike other languages, the data type of variables or constants is determined by the context of the program

2.2 declaration of variables:

PHP One of its features is that it does not require a variable to be declared before using it. When you assign a value to a variable for the first time, you create the variable. The variable is used to store the value,
For example, numbers, text, strings, arrays, etc. once a variable is set, we can reuse it in the script
PHP Variables in must use a dollar sign $ Followed by the variable name, using the assignment operator(=)Assign a value to a variable
 Reference assignment:
	$c = &$a;
	Equivalent to giving $a An alias, $c The value of changes, $a It also changes; $a The value of changes, $c It also changed. Bound together, all pointing to the same memory address

2.3 release variables:

unset($name);

2.4: naming of variables:

1: Strictly case sensitive
2: Letters, numbers, underscores, cannot start with numbers
	$name1
	$_name23
3: Try not to use it php Keyword as variable name

2.5 variable variables:$$

$$name

2.6: reference and assignment of variables:&$

2.7: PHP syntax error level:

Error			End script execution
Warr~			Prompt only without affecting execution
Notice

2.8: variable type:

Variable type refers to the type of data saved in the variable. Data with the same type can operate with each other. Use VaR_ Dump (variable name) can output the type of variable.

2.8.1: Boolean type

true
false
 The following will be treated as false,Others will be considered true
	1)Boolean value false
	2)0
	3)Floating point 0.0
	4)Blank string and string 0
	5)Array without members
	6)NULL

2.8.2: int

Store integer

2.8.3: float

Store decimal

2.8.4: string

Definition of string
	''
	""
	Note: if there is a single quotation mark in the string defined by a single quotation mark, an escape character is required\
		  In the string defined by single quotation marks, $Symbol as is output
		  In the string defined by double quotation marks, $The symbol is the beginning of the variable, and the variable is used as a whole{}Enclose
		  Enter special characters in the string defined by single and double quotation marks, including[' '' $...]Need escape
		  Delimiter:
			The beginning and end of the delimiter cannot be followed by any characters, including white space characters and comments

2.9: constant: constant quantity

define("NAME","XXX");
		echo NAME;
		Predefined constants:
		__FILE__				Current file name (including path)
		__LINE__				Current number of rows
		__FUNCTION__			Current function name
		__CLASS__				Current class name
		__METHOD__				The method name of the current object
		PHP_OS					UNIX or WINNT etc.
		PHP_VERSION				current PHP Server version
		DIRECTORY_SEPARATOR		\or/  The separator of the directory depends on the operating system

 
 

3: php operator

3.1: arithmetic operator

<?php
$a = 10;
$b = 5;
$c = -$a;
echo $c;
echo "<hr />";
$c = $a + $b;
echo $c;
echo "<br />";

$c = $a - $b;
echo $c;
echo "<br />";

$c = $a * $b;
echo $c;echo "<br />";

$c = $a / $b;
echo $c;echo "<br />";

$c = $a % $b;
echo $c;echo "<hr />";

echo $a++;		// 10 output first and then increase automatically
echo ++$a;		// 12 auto increment first, then output
echo "<br />";
echo $a;		// 12
echo "<br />";
echo $a--;		// 12
echo --$a;		// 10
?>

3.2: logical operators

wrong			!				Reverse
 And			and   &&		$a and $b At the same time true ---> true
 or			or	||			$a and $b Either is true ---> true
 XOR		xor				$a and $b Either is true,But not at the same time ---> true
<?php
$a = true;
$b = false;
$c = !$a;
var_dump($c);		// bool(false) 

$c = $a and $b;
var_dump($c);		// true

var_dump($c = $a and $b);

?>

3.3: comparison operator

Compare whether the units involved in the operation are the same. The same is true and the difference is false.
			$a == $b be equal to TRUE,If after type conversion $a be equal to $b.  
			$a === $b Congruent TRUE,If $a be equal to $b,And they are of the same type. 
			$a != $b Unequal TRUE,If after type conversion $a Not equal to $b.  
			$a <> $b Unequal TRUE,If after type conversion $a Not equal to $b.  
			$a !== $b Incomplete equivalence TRUE,If $a Not equal to $b,Or they are of different types. 
			$a < $b Small and TRUE,If $a Strictly less than $b.  
			$a > $b greater than TRUE,If $a Strictly greater than $b.  
			$a <= $b Less than or equal to TRUE,If $a Less than or equal to $b.  
			$a >= $b Greater than or equal to TRUE,If $a Greater than or equal to $b.  
			$a <=> $b Spacecraft operator (combinatorial comparator) $a Less than, equal to, greater than $b Returns a value less than, equal to or greater than 0 integer Value. PHP7 Start offering.  
			$a ?? $b ?? $c NULL The merge operator exists first from left to right and is not NULL Operand for. If none is defined and not NULL,Then return NULL. PHP7 Start offering 
<?php
$a = 0;
$b = false;
$c = 0.0;
$d = 1/3;
$e = 2/6;
$f = true;

echo '$a == $b:';
var_dump($a == $b);		// bool(true)
echo "<br />";

echo '$a === $b:';
var_dump($a === $b);	// bool(false)
echo "<br />";

echo '$a != $b:';
var_dump($a != $b);		// bool(false)
echo "<br />";

?>

3.4: other operators

?:			Ternary operator 
				$a=10>20?10:20;
``			Execute string as command
@			Shielding tips
<?php
$a = 10<20?10:20;
echo $a;				// 10
echo "<hr />";
echo `whoami`;			// win2008-1\administrator
echo "<hr />";
echo @$name;			// The error prompt is masked
?>

 
 

4: php loop and control

4.1 sequential execution:

php When the code is executed, it is executed in sequence

4.2: branch execution

4.2.1 two way branch:

if (Judgment conditions){Execute statement block 1}else{Execute statement block 2}

4.2.2: multidirectional branching

if (Judgment conditions){Execute statement block 1}elif{Execute statement block 2}elif{Execute statement block 3}...else{Execute statement block}
switch...case...default

switch:
rand random number 1-7 is different every time you refresh the web page

<?php
$day = rand(1,7);
echo $day;
echo "<br />";
switch($day){
	case 1:
		echo "monday";
		break;
	case 2:
		echo "tuesday";
		break;
	case 3:
		echo "wednesday";
		break;
	case 4:
		echo "thursday";
		break;
	default:
		echo "what?";
}
?>

4.3: cycle execution:

4.3.1: while

When the conditions are met, execute
 Counter, variable
 Judgment conditions

4.3.2: do{}while()

Execute first, then judge
<?php
	$a = 0;
	do{
		echo ++$a;
	}while($a < 10)
?>

4.3.3: for loop:

for(;;){}
<?php
for($a=0;$a<10;$a++){
	echo $a."<br />";
}
?>

4.4 process control:

4.4.1: break

break
break 2;				Jump out of two-tier cycle
<?php
for($i=0;$i<10;$i++){
	echo $i."<hr />";
	for($j=0;$j<5;$j++){
		echo $j."<br />";
		if($j==3){
			echo "xxxxxx";
			break 2;		// Jump out of two cycles
		}	
	}
	echo "<hr />";
}
?>

4.4.2: continue: skip this cycle

<?php
for($i=0;$i<10;$i++){
	echo $i."<hr />";
	for ($j=0;$j<5;$j++){
		if ($j == 3){
			continue;			// Skip this (third) cycle
		}
		echo $j."<br />";
	}
}
?>

4.4.3: die(); And exit();

They all end the script directly

<?php
for($i=0;$i<10;$i++){
	echo $i."<hr />";
	for ($j=0;$j<5;$j++){
		if($j==3){
			# die("the end");
			exit("the end");
		}
		echo $j."<br />";
	}
}
?>

Keywords: PHP Apache Cyber Security

Added by Sangre on Thu, 20 Jan 2022 13:54:26 +0200