Differences between phpforeach and for statements _ PHP Tutorial

Source: Internet
Author: User
Differences between phpforeach and for statements. Foreach $ tararray (1 East, 2 West, 3 South, 4 North, 5 Southeast, 6 Southwest, 7 Northeast, 8 northwest, 9 North South, 10 East ,); copy the code as follows // foreach

$ Tar = array (

1 => 'East ',

2 => 'West ',

3 => 'south ',

4 => 'North ',

5 => 'Southeast ',

6 => 'southwest ',

7 => 'Northeast region ',

8 => 'Northwest ',

9 => 'North and South ',

10 => 'thes ',

);

The code is as follows:

$ TM = 'West ';

Foreach ($ tar as $ v => $ vv)

{

If ($ vv = $ TM)

{

Echo $ vv. '-'. $ v .'

';

Break;

}

// Echo $ vv;

}

// West-2

//

The code is as follows:

Echo'

';

For ($ I = 1; $ I <= count ($ tar); $ I ++)

{

If ($ tar [$ I] ==$ TM)

{

Echo $ tar [$ I]. '-'. $ I .'

';

Break;

}

}

// West-2

Conclusion: foreach and for results are exactly the same, but in terms of efficiency, foreach is better than for. The homepage for needs to know the array length and then use $ I ++ for operations, page foreach does not need to know the length of the array to automatically detect and enter the key, and value.

Http://www.bkjia.com/PHPjc/371795.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371795.htmlTechArticle//foreach $ tar = array (1 = 'East', 2 = 'West', 3 = 'south', 4 = 'North', 5 = 'south ', 6 = 'Southwest China ', 7 = 'Northeast China', 8 = 'Northwest China ', 9 = 'North and South', 10 = 'East thing',); the code is as follows...

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.