Home > Article > Backend Development > PHP Basic Syntax Chapter 2_PHP Tutorial
1. How to define a constant
Keyword: define Syntax define('constant name', 'constant value')
$a=123; define('I',$a); echo I; ?> 2, array
1Define an array
Keyword array syntax array(key=>value,key2=>value2,key3=>value3) key can be an integer or a string, value can be any value
$arr=array('too'=>'bar',123=>true); echo $arr['too'].'
'; echo $arr[123]; ?> ;
Method of printing array print_r Keyword: print_r Syntax print_r (to print the array name), mainly used for debugging
header("Content-Type:text/html; charset=utf-8"); $stu=array('stu_no'=>'10010','stu_name'=>'Lao Zhao'); print_r ($stu); ?> Output result Array ([stu_no] => 10010 [stu_name] => Lao Zhao)
Output method echo $array name[key]
header("Content-Type:text/html; charset=utf-8"); $stu=array('stu_no'=>'10010','stu_name'=>'Lao Zhao'); echo $stu['stu_no'].'
'; echo $stu['stu_name']; ?> Output result
10010
Lao Zhao
Another way to define an array is to directly enter the value key as an integer arranged starting from 0
array('value','value2','value3','value4')
header("Content-Type:text/html; charset=utf-8"); $stu=array('Pipi','Lele','Pope','Old Zhao'); print_r($ stu); ?> The output result is Array ([0] => Pipi[1] => Lele[2] => Pope[3] => Lao Zhao)
Output separately:
header("Content-Type:text/html; charset=utf-8"); $stu=array('Pipi','Lele','Pope','Old Zhao'); echo $stu [0].'
'; echo $stu[1].'
'; echo $stu[2].'
'; echo $stu[3].'
Output result
Pippi
Lele
Pope
Lao Zhao
Loop output method for statement syntax
for (loop condition) Example: $i=0;$i<4;$i++
{
echo $array name[loop variable name]
}
header("Content-Type:text/html; charset=utf-8"); $stu=array('Pipi','Lele','Pope','Old Zhao'); for ($ i=0;$i<4;$i++) { echo $stu[$i].'
'; } ?> Output result
Pippi
Lele
Pope
Lao Zhao
while loop syntax
$Conditional variable name=Conditional variable value
while (conditional statement) example $i<4
{
$array name[$condition variable name];
$condition variable++
}
header("Content-Type:text/html; charset=utf-8"); $stu=array('Pipi','Lele','Pope','Old Zhao'); $i= 0; while ($i<4) { echo $stu[$i].' '; $i++; } ?> Output result Pipi Lele Pope Lao Zhao
Add element
to the end of the array
Syntax $array name=array[value,value1,value2,value3];
$array name[]=value to be added;
$array name[]=value to be added;
header("Content-Type:text/html; charset=utf-8"); $stu=array('Pipi','Lele','Pope','Old Zhao'); $stu[ ]='Haomin'; $stu[]='Su Chao'; $stu[]='Lu Teng'; for ($i=0;$i<8;$i++) { echo $stu[$i] .' ';
Create a range array range and count methods to get how many elements there are in the array
Syntax $array name=range(range start, range end)
count($array name)
header("Content-Type:text/html; charset=utf-8"); $stu=range(1,12); for ($i=0;$i
header("Content-Type:text/html; charset=utf-8"); $stu=range('a','z'); for ($i=0;$i
Five, delete and insert replacement elements in the array array_splice
Array_splice takes two parameters to represent deletion and four parameters to represent insertion or replacement (when the third parameter is 0, it is insertion, when it is not 0, it is replacement)
Delete syntax array_splice($array name, delete end subscript) The subscript is equal to the delete before this Key
header("Content-Type:text/html; charset=utf-8"); $stu=range('0','12'); $stu2=array_splice($stu,5); for ($ i=0;$i
Replacement syntax array_splice ($replaced array name, key, key2, $replaced new array name) Subscript 1 and subscript 2 combined together mean that the elements of subscript key2 starting from subscript key are replaced by the new array
header("Content-Type:text/html; charset=utf-8"); $stu=range('0','12'); $stu2=array('a','b',' c'); array_splice($stu,3,6,$stu2); for ($i=0;$i
Six elements starting from 3 are replaced with a, b, c
This article comes from the "PHP Study Notes" blog
http://www.bkjia.com/PHPjc/478717.html