PHP a few tips, comparative basis, summed up, the old birds in a different posture floating past is.
1. Str_replace
Str_replace is often commonly used PHP functions, for string substitution, often see some new PHP in order to replace a batch of strings, write a lot of str_replace, it is appalling.
Like this example:
$str = ' Someone's habitat---blog.snsgou.com '; $str = str_replace (' Someone ', ' Bad Man ', $str); $str = Str_replace (' ', ' di ', $str); $str = Str_re Place (' Habitat ', ' pig litters ', $str); $str = Str_replace (' blog.snsgou.com ', ' snsgou.com ', $str);
The above, replaced 4 times the string, the actual as long as a different wording, a line is done:
$str = ' Someone's habitat---blog.snsgou.com '; $str = str_replace (' Someone ', ' ', ' habitat ', ' blog.snsgou.com '), Array (' bad guy ', ' di ', ' pig litters ') , ' snsgou.com '), $STR);
2. Array
It is often seen that an array is written like this:
echo $arr [Some_key];
The above line of code can run, it does not seem to be a big problem, but if you put php.ini error notice open, you will receive a large number of error. PHP parser first is to take "some_key" as a constant to explain, but if there is no definition of some_key such a constant, the parser is very tolerant of it as a string to see. So the new students have better write a complete point:
echo $arr [' Some_key '];
So there's no problem, if you're going to put it in double quotes, you can't omit the quotes, you can write:
echo "This is a string mixed in double quotes {$arr [' Some_key ']}";
3. Type tricks
Type-trick is quite good, for example, there is a form submitted by the variable, under normal circumstances it should be integral type, and sometimes lazy omitted to check the wording can be this:
$intVar = (int) $_post[' Post_var '];
such as arrays, sometimes write key value to quote is not very uncomfortable ah, we can convert it to object, such as:
$arr = Array (' name ' = ' volcano ', ' sex ' = ' male '); $arr = (object) $arr; Echo $arr->name;echo $arr->sex;
Isn't it convenient?
4. Lambda functions
The use of the LAMDA function and the Array_* series function is miraculous, taking an example from the PHP manual:
<?php$av = Array ("The", "a", "that", "this"), Array_walk ($av, Create_function (' & $v, $k ', ' $v = $v. ") Mango "; ')); Print_r ($AV);? >
Save at least one for loop
5. Nested loops display the cells of the table
Nested loops show the cells of the table, this is a very old topic oh, often want to add a condition behind a cell to determine what, consider whether to output TR or TD label.
Here I introduce a method, using the Array_chunk function can compare neat output HTML, see the following example, this example to output a 4 rows and 6 columns of the table:
<?php$arr = Range (1, 24); This generates an array of arrays (1,2,3,4....24) $arr = Array_chunk ($arr, 6);//Output table?><table><?php foreach ($arr as $ Row):? ><tr><?php foreach ($row as $col): ><td><?php echo $col? ></td><?php Endforeach;? ></tr><?php Endforeach;? ></table>
Five tips for using PHP