PHP Basic Syntax Chapter 2_PHP Tutorial

WBOY
Release: 2016-07-13 17:44:55
Original
1018 people have browsed it

1. How to define a constant

Keyword: define Syntax define('constant name', 'constant value')

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

'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

'10010','stu_name'=>'Lao Zhao'); print_r ($stu); ?> Output result Array ([stu_no] => 10010 [stu_name] => Lao Zhao)

Output method echo $array name[key]

'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')

The output result is Array ([0] => Pipi[1] => Lele[2] => Pope[3] => Lao Zhao)

Output separately:

'; 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]

}

'; } ?> 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++

}

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;

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)

Output result 1 2 3 4 5 6 7 8 9 10 11 12

3. Fill array

array_pad

Grammar

array_pad($array name, array length, padding default value)

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

Insertion syntax array_splice($inserted array name, subscript, 0, $inserted new array name) The subscript is to insert a new array before this subscript reaches the previous one

Output result 0 1 2 3 4 a b c 5 6 7 8 9 10 11 12

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

The output result is 0 1 2 a b c 9 10 11 12

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478717.htmlTechArticle1. How to define a constant keyword: define syntax define (constant name, constant value)? $a= 123; define(I,$a); echo I; ? 2. Array 1 defines an array key array syntax array(key...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template