PHP basic syntax Chapter 2

Source: Internet
Author: User
1. how to define a constant keyword: define Syntax define (& amp; #39; constant name & amp; #39;, & amp; #39; constant value & amp; #39;) & lt ;? $ A123; define (& amp; #39; I & amp; #39;, $ a); echoI ;? & Gt; 2, array 1 defines an array keyword array syntax array (key & gt

I. how to define a constant

 

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

 

2. array

 

1 define an array

 

Keyword array syntax array (key => value, key2 => value2, key3 => value3) key can be an integer or string, and value can be any value

 

'Bar', 123 => true); echo $ arr ['too'].'
'; Echo $ arr [123];?>

 

 

 

Print array method print_r keyword: print_r syntax print_r (to print the array name), mainly used for debugging

 

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

 

Output method echo $ array name [key]

 

'000000', 'Stu _ name' => 'Zhao '); echo $ stu ['Stu _ no'].'
'; Echo $ stu ['Stu _ name'];?> Output result

 

10010

Lao Zhao

 

Another way to define an array is to directly input the value key as an integer arranged from 0.

 

Array ('value', 'value2', 'value3 ', 'value4 ')

 

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

 

Output:

 

'; Echo $ stu [1].'
'; Echo $ stu [2].'
'; Echo $ stu [3].'
';?> Output result

 

Pipi

Lele

Pope

Lao Zhao

 

 

Loop output method for statement syntax

 

For (cyclic condition) example: $ I = 0; $ I <4; $ I ++

 

{

 

Echo $ array name [loop variable name]

 

}

 

';}?> Output result

 

Pipi

Lele

Pope

Lao Zhao

 

While loop syntax

 

$ Condition variable name = condition variable value

 

While (condition statement) example $ I <4

 

{

 

$ Array name [$ condition variable name];

 

$ Condition variable ++

 

}

 

Output result: Pi pilele, Pope Zhao

 

Add elements at 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;

 

Output result: Pi pilele, Pope Zhao Haomin, Su Chao, Lu Teng

 

Create a range array range and count to obtain the number of elements in the array

 

Syntax $ array name = range (range starts, range ends)

 

Count ($ array name)

 

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

 

Output result a B c d e f g h I j k l m n o p q r s t u v w x y z

 

Three-filled array

 

Array_pad

 

Syntax

 

Array_pad ($ array name, array length, fill default value)

 

Output result 0 1 2 3 0 0 0

 

5. delete and insert the replacement element array_splice in the array.

 

Two parameters in array_splice indicate that the deletion of four parameters indicates insertion or replacement. (when the third parameter is 0, it indicates insertion. if it is not 0, it indicates replacement)

 

Delete syntax array_splice ($ array name, delete end subscript) subscript is equal to delete before this Key

 

Output result 5 6 7 8 9 10 11 12

 

Insert syntax: array_splice ($ new array name inserted, subscript, 0, $) subscript: insert a new array before this subscript to the previous one

 

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

 

Replace syntax array_splice ($ replace the array name, key, key2, $ replace the new array name) subscript 1 subscript 2 combined to indicate starting from the subscript key number subscript key2 elements are replaced by the new array

 

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

 

Six elements are replaced with a, B, and c elements starting from 3.

 

 

 

This article is from the "PHP study notes" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.