1. Str_replace
Str_replace is a commonly used PHP function, used for string replacement, often see some of the new PHP to replace a batch of strings, write a lot of lines str_replace, it is appalling.
Like this example:
Php:
$str = ' Someone's habitat---www.webjx.com ';
$str = Str_replace (' Someone ', ' bad person ', $str);
$str = Str_replace (' ', ' di ', $str);
$str = str_replace (' Habitat ', ' pig litters ', $str);
$str = Str_replace (' www.webjx.com ', ' webjx.com ', $str);
Above, replaced 4 times string, actually just change the wording, one line is done:
$str = ' Someone's habitat---www.webjx.com ';
$str = Str_replace (' Someone ', ', ', ' habitat ', ' www.webjx.com '), Array (' villains ', ' di ', ' Pig litters ', ' webjx.com '), $STR);
2. Array
Often see someone with an array to write this: Echo $arr [Some_key];
The above line of code can run, it does not look very big problem, but if you php.ini error notice open, you will receive a large number of error. The PHP parser first takes "Some_key" as a constant, but if you do not define a constant like Some_key, the parser is very tolerant to treat it as a string. So the new students 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, so you can write: echo "This is a string in double quotes {$arr [' Some_key ']}";
3. Type trick
Type-juggling is quite handy, such as having a form to submit a variable, normally it should be integral type, sometimes lazy omit checksum can be the way of writing:
$intVar = (int) $_post[' Post_var '];
Another example is the array, 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; Is it convenient?
4. Lambda function
Lamda functions and Array_* series functions work wonders, take an example from the PHP manual:
Php:
<?php
$av = Array ("The", "a", "that", "this");
Array_walk ($av, Create_function (' & $v, $k ', ' $v = $v. "Mango";
Print_r ($AV);
?>
At least one for loop saved
5. Nested loops to display the table's cells
Nested loops to display the cells of a table, this is a very old topic oh, it is often to be in a cell behind a condition to judge what, consider whether to output TR or TD tag.
I introduce a method, using the Array_chunk function can be more neat output of HTML, see the example below, to output a 4 rows of 6 columns table:
<?php
$arr = Range (1, 24); This will generate 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>