PHP study notes

1. Variable variable: The value of one variable is used as the name of another variable, so the variable variable is called the variable of the variable. The variable variable is intuitively added a variable before the variable name.$

$a = “b”;  
$b=”一个变量”;     
echo $ $a;//$ $ a就是一个可变变量,相当于$b

2. Reference variable: add a "&" sign before the original variable to be assigned

$a =10;  
$b = “hello”;  
$a = & $b;//$a引用$b的地址,修改任意一个变量的值另一个会跟着改变

3. The principle of converting strings into numerical values: take out integers or floating-point numbers from the beginning of the string. If the beginning is not a number, it is 0. Boolean true is converted to 1, and false is converted to 0;

  &a =10+2.2ab8’//结果为12.2     &a = ‘10’+’ab.2.2’//结果为10

4. String concatenation operator: ' .', if either side is not a string type, it will be converted to a string first

5. PHP supports operator abbreviations

6. ' ==' indicates that the value and data type are the same

7. Logical operators , &&OR and, ||and orXOR ( xor)

8. PHP supports ternary operator

9. Single quotes indicate pure strings, double quotes can contain strings and variable names (variables in double quotes will be treated as variable values, single quotes will not)

10. Use double quotes to add spaces, you can also use {} to wrap

11. Get string characters:字符串变量[index]

12. Get the string length:strlen()

13. elseifNo spaces in between

14. foreach loop Iterates
over array elements or object elements, and is suitable for the case where the number of arrays is unknown.
Format 1:

for( 数组名 as $ value){
    
    循环语句块}

Format 2:

for(数组名 as $ key => $ value){
    
    循环语句块}

When the foreach statement traverses the array, it first points to the first element of the array.
In each loop, assign the current array element value to &value, assign the current subscript value to &key, and then move the pointer backward until the end of the traversal.
Example 1:

<?php
$sprot = array("网球","游泳","短跑","柔道");
foreach($sprot as $key => $value)
	echo $key.":".$value."<br/>";
?>
运行结果:
0:网球
1:游泳
2:短跑
3:柔道

Example 2:

<?php
$sprot = array("网球","游泳","短跑","柔道");
foreach($sprot as $value ){
    
    
	echo $value;
}
?>
运行结果:
网球
游泳
短跑
柔道

15.while loop statement
Print the table with a while loop

<table border="1" width="300" align="center">
<?php
$i=0;
while($i<3){
    
    
	echo "<tr><td>这是第 $i 行</td></tr>";
	$i++;
}
?>
</table>

16. Continue to print cells

<table border="1" width="200" align="center"><tr>
<?php
$i=0;
while($i<9){
    
    
	echo "<td>第 $i 格</td>";
	$i++;
	if($i%3!=0 || $i == 9)continue;
	echo "</tr><tr>";
}
?>
</tr></table>

17. File Inclusion Statement
In order to improve the reusability of the code, usually put some common code into a separate file, and then in the file that needs such code, use the include statement to introduce them into
(1) include statement
Format: inchude(path/filename);//Brackets Can be omitted, relative paths can be absolute paths.
When a file is included, the compiler will embed all the code of the file into the location where the include statement is located, and can also include HTML files
Example:

<?php  //demo02-test.php>
		$name = "马云";
		$age = 19;
?>

<?php  //demo01-test.php
	echo "我的名字是 $name <br>";
	include('demo02-test');
	echo "我的名字是 $name ,今年 $age 岁<br>";
?>

The above code is equivalent to:

<?php  //demo01-test.php
	echo "我的名字是 $name <br>";
		$name = "马云";
		$age = 19;
	echo "我的名字是 $name ,今年 $age 岁<br>";
?>
输出:
我的名字是 
我的名字是 马云 ,今年 19

(2) The include_once statement
is similar to include, the difference is that if the file has been included, it will not be included again, which can avoid redefinition and variable reassignment

(3) The require statement
is also used to include files, but it is different from the include statement in error handling. When the include file fails (such as when the include file does not exist), the require statement will cause a fatal error and terminate the execution of the program, while the include statement will only throw a warning message and continue the program execution

(4) The require statement
is similar to require, the difference is that if the file has been included, it will not be included again, which can avoid redefinition and variable reassignment. It is
recommended to use require and require_once as much as possible, which will be safer

2. Array
(1) Array creation

$ 数组名 = array(1,2,);

Index value: 0,1,2,3, $array name[2] means to take the third element of the array

You can also assign values ​​to the indices yourself (create arrays in full form):

$ 数组名 = array(1=>索引名1,值2=>索引名2,...)

At this time, the custom index is used to access the array value, and the default index is invalid.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324910403&siteId=291194637