Like opening a long hidden treasure chest, this session will bring many jewels back to the programming light. We'll cover a number of lesser known PHP function and MySQL functionalities, that will help at daily tasks. They will be applied in various fields, including security, performances, standard compliance and simply fun to program.
2. Agenda
Tips and Tricks from PHP
No need for anything else than standard distribution
All for PHP 5
( but lots of it is also valid in PHP 4 )
No petting the elePHPant until the end of the session
3. Who is speaking?
Damien Séguy
Nexen Services, Open Source hosting company
PHP / MySQL expert services
Redacteur en chef of www.nexen.net
PHather of thousands plush Elephpants
http://www.nexen.net/english.php
6. Random stuff
Array
<?php (
$a = range('d','a'); [0] => c
shuffle($a); [1] => d
[2] => b
print_r($a); [3] => a
)
print str_shuffle('abcdef'); Array
// eabdcf (
[0] => 0
print_r(array_rand($a,3)); [1] => 1
?> [2] => 3
)
7. Random stuff
rand() and mt_rand()
array_rand() : extract info from an array
extract keys!
shuffle() : shuffle an array before deal
str_shuffle() : shuffle a string
8. Arrays combinaisons
array_combine() : turn two arrays into one
Inverse to array_keys() and array_values()
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
Array
print_r($c);
(
?>
[green] => avocado
[red] => apple
[yellow] => banana
)
13. JOIN is faster than LIMIT
mysql> SELECT cols FROM TABLE LIMIT 1000, 10;
14. JOIN is faster than LIMIT
mysql> CREATE TABLE `table_limit` (
`row` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id` int(11) unsigned DEFAULT NULL,
UNIQUE KEY `id` (`id`) );
mysql> INSERT INTO table_limit
SELECT 0, id FROM table ORDER BY col;
mysql> SELECT col FROM table_limit JOIN table
ON bl_content.id = bl_content_nav.id AND
table_limit.row
BETWEEN @offset AND @offset + 10
16. Arrays sorted as SQL cols
Array
(
array_multisort() : sort several [0] => 2
arrays at once [1] => 3
[2] => 4
Works like a ORDER BY
[3] => 5
<?php )
$ar1 = array(5,4,3,2); Array
$ar2 = array('a','b','c','d'); (
array_multisort($ar1, $ar2); [0] => d
array_multisort($ar1, [1] => c
SORT_ASC,SORT_STRING, [2] => b
$ar2); [3] => a
?> )
17. GROUP BY PHP!
DISTINCT : array_unique()
COUNT(*) and GROUP BY : array_count_values()
<?php
$array = array(1, quot;heiquot;, quot;1quot;, quot;takkquot;, quot;heiquot;);
print_r(array_count_values($array));
Array
?>
(
[1] => 2
Hint : array_count_values() is always faster that
[hei] => 2
array_unique()... [takk] => 1
)
19. Hide those loops to me!
<?php
$array = range(1,3);
foreach($array as $id => &$value) {
$value++; Array
} (
[0] => 2
?>
[1] => 3
[2] => 4
)
Declare your objects as Iterators
20. Hidden loops
array_map : apply a function to all elements
array_walk_recursive : same as above, and multi-dimensional
array_filter : extract all elements using a custom function
21. Fast dir scans
scandir(‘/tmp’, true);
Include name sorting
Replace opendir, readdir, closedir and a loop!
glob(‘*.html’);
Simply move the loop out of sight
24. URL operations
parse_url()
Break down into details
Do not make any check
parse_str()
Split a query string
Separate and decode, as long as it can
Fills up an array or $GLOBALS
26. URL validations
scheme : list your own
host : checkdnsrr() to check
path : realpath() + doc root (beware of mod_rewrite)
query : parse_str()
beware of the second argument!
don’t handle &
27. URL rebuilding
http_build_query() : PHP 5 only
rebuild your query
takes into account encoding and arrays
<?php
print http_build_query(
array_merge($_GET ,
array(' de ' => ' ')));
?>
+de+=%E5%AF%B9%E4%BA%86%EF%BC%81
32. Fast file exports
file_put_contents($file_name, $content);
accept contexts and streams
Complementary to file_get_contents()
fputcsv() : create CSV style files
complementary to fgetcsv()
33. Assertions
Include tests during execution
Assertion are an option (default is on) :
Most clever way than removing than echo/var_dump
Common practice in other languages
Programmation by contract
35. Debugging
get_memory_usage()
memory_limit is now on by default
Better memory handling
get_peak_memory_usage()
sys_getloadavg() : no more need for exec(’uptime’);
36. Debugging
get_included_files()
get_defined_constants/functions/vars()
get_declared_classes()
get_debug_backtrace()
function stack and their arguments
file and line calling