Share some of the tips and tricks you'll encounter in PHP work "1"

Source: Internet
Author: User

A reference to a foreach

$arr = Range (1,3//foreach as &foreach  as $val) {} print_r ($arr);

What does the above code output?

Array (    [01     [12     [22  )

This is because of the reference to the variable in the source of the foreach. Causes its effect, assigns the address to the last

Cases:

TMP is a reference to Var, which points to the storage space of Var, and when TMP changes, VAR also changes



Echo $var;
20

Workaround:

As   &$value) {}  unset ($value);  As //     
As   &as//    
As   &$value) {} as  &//[[+]    

Two good at using Array_walk and foreach, for

<?PHP/** * Array_walk and foreach, for efficiency comparison.  * We want to test the problem of the efficiency of foreach, for, and Array_walk. *///produces an array of 10000. $max =10000; $test _arr= Range (0, $max); $temp;//we tested each of the three ways to find the time for these numbers plus 1. //for Method$t 1 = microtime (true); for($i =0; $i < $max; $i + +) {$temp= $temp +1;} $t 2= Microtime (true); $t= $t 2-$t 1;echo"with for, there is no array operation to spend: {$t}\n"; $t 1= Microtime (true); for($i =0; $i < $max; $i + +) {$test _arr[$i]= $test _arr[$i] +1;} $t 2= Microtime (true); $t= $t 2-$t 1;echo"use for and directly manipulate the array to spend: {$t}\n"; $t 1= Microtime (true); for($i =0; $i < $max; $i + +) {AddOne ($test _arr[$i]);} $t 2= Microtime (true); $t= $t 2-$t 1;echo"use the FOR Call function to operate on array operations: {$t}\n"; $t 1= Microtime (true);foreach($test _arr as$k = &$v) {$temp= $temp +1;} $t 2= Microtime (true); $t= $t 2-$t 1;echo"using foreach does not cost the array operation: {$t}\n"; $t 1= Microtime (true);foreach($test _arr as$k = &$v) {$v= $v +1;} $t 2= Microtime (true); $t= $t 2-$t 1;echo"Direct array operation using foreach: {$t}\n"; $t 1= Microtime (true);foreach($test _arr as$k = &$v) {AddOne ($v);} $t 2= Microtime (true); $t= $t 2-$t 1;echo"Use the foreach Call function to set the array operation: {$t}\n"; $t 1= Microtime (true); Array_walk ($test _arr,'AddOne'); $t 2= Microtime (true); $t= $t 2-$t 1;echo"spend with Array_walk: {$t}\n"; function AddOne (&$item) {$item= $item +1;}

Results of execution:
Using for, no array operation is spent: 0.15388584136963
Using foreach without an array operation costs: 0.076934814453125

Use for and operate directly on an array cost: 0.14769005775452
Using a foreach Direct array operation: 0.076115131378174

Using the for Call function for array operations costs: 0.32393312454224
Use the foreach Call function to set an array operation: 0.25716996192932
Cost of using Array_walk: 0.17966890335083

In the course of the 10,000-digit operation, we can draw the conclusion that:
The efficiency of foreach is much higher than for a for, perhaps a big reason is that for a lot of conditional judgment. So you can use foreach in the future to make it 1 time times more efficient.
If a function is to be called within a loop, it is best to use Array_walk, which is 1 time times more efficient than the for, and 43% more efficient than foreach.
There is also a hint that if you have this program is very high efficiency requirements, do not call the function in a very deep loop, to invoke the function is also used Array_walk, the best direct code written in the loop inside.

At the same time, let's look at the difference between these two functions:

Array_walk is mainly to operate on each value in the array, the result of the operation affects the original arrays Array_map is mainly an array of values after the operation of the returned arrays to get a new array wallk can not return the value of the map to have, because to populate the array

Third, why use a federated index?

1 in a multi-conditional query, the Federated index is more efficient. Within the allowed range, multiple federated indexes should be created based on the business when a multi-conditional condition query is made. This efficiency is faster than creating multiple indexes separately

2 If the first column, or all, of the federated index appears in the query condition, you can take advantage of the Federated index.

In other words: The first column of the Federated Index does not appear in the query condition, and the second column with the Federated Index, or the third column, does not take advantage of the Federated Index query.

A single index: An index query can be leveraged wherever the index column appears in the Condition column.

My local computer resembles the following table:

CREATE TABLE ' t_log_change_name ' (' ID ')int( One) not NULL auto_increment, ' player_id 'int( One) Not NULL DEFAULT'0',' old_name ' varchar (255) Not NULL DEFAULT"', ' Day 'int( One) Not NULL DEFAULT'20110101',PRIMARY key (' ID '), key ' idx_player_id ' (' player_id ', ' Day ') USING BTREE) ENGINE=innodb DEFAULT Charset=utf8 row_format=compact;

When I use federated criteria queries,

Select  from where player_id =344 and ' Day ' >='20161012';

The result of course is no problem, directly select the desired data, affect the behavior of 3 lines

When I use only the first column for a conditional query,

Select  from where player_id =344;

It is in the first column of the index, so it can be hit-indexed

When I use the second column conditional query,

Select  from where ' Day ' >='20161012';

I'm sorry, it belongs to the second column of the index, so the index cannot be hit, and all rows are retrieved once

So be sure to build the right index based on your business needs.
, and note the order of the Federated index, the failure condition.

Share some of the tips and tricks you'll encounter in PHP work "1"

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.