Learn PHP record "three"

Source: Internet
Author: User

Go ahead ...

1, string

string concatenation, string is no + this operator, so is:

$a = ' Hello '; $a . = ' World '; Echo $a ; # back to Hello world
$a = ' Hello '; $b $a . ' World '; Echo $b ; # result "HelloWorld

To manipulate a string, it is operated by subscript.

1<?PHP2 //gets the first character of a string3 $str= ' This is a test. ';4 $first=$str[0];5 6 //gets the third character of a string7 $third=$str[2];8 9 //gets the last character of the stringTen $str= ' This is still a test. '; One $last=$str[strlen($str)-1];  A  - //Modify the last character of a string - $str= ' Look at the sea '; the $str[strlen($str)-1] = ' E '; -  -?>

2. Array

Now you can define it directly through [] and it feels good.

$a = [

' Foot ': ' H ',

]

Delete a data

unset ($a [' foot ']);

However, the index of key is not re-established after deletion, and the code is attached:

<? PHP $a Array (1 = ' one ', 2 = ' one ', 3 = ' three '); unset ($a[2]); /* Would produce an array that would has been defined   as $a = array (1 = ' one ', 3 = ' three ');   And not   $a = array (1 = ' one ', 2 = ' three '); */

Delete all data:

foreach ($aas$i$value):    unset($a [$i]);

Print an array

Print_r ($a);

To calculate the size of an array:

$count Count ($array);

If you want to reassign value, you can do so by referencing it in the loop:

But this kind of no-brain change should seldom be used;

$arr = [' a ' = = ' B ']; foreach ($arr as$key=>&$value) {    $value= ' Test ';}

Add an official example:

<? PHP foreach (array as &$value) {    $value$value * 2 ;}? >

Learn PHP record "three"

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.