SlideShare a Scribd company logo
PHP tips and tricks
  Skien, Norge, June 7th 2007
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 )

     The month of PHP functions
Who is speaking?

    Damien Séguy
       Nexen Services, eZ publish silver partner for hosting
       PHP / MySQL services
       Redacteur en chef of www.nexen.net
       Phather of an plush Elephpant
    http://www.nexen.net/english.php
Nod when you know about it
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
Random stuff

                                Array
 <?php                          (
 $a = range('a','d');               [0]   =>   c
 shuffle($a);                       [1]   =>   d
                                    [2]   =>   b
 print_r($a);                       [3]   =>   a
                                )
 print_r(array_rand($a,3));     Array
                                (
 print str_shuffle('abcdef');       [0]   => 0
 // eabdcf                          [1]   => 1
 ?>                                 [2]   => 3
                                )
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
                     )
Arrays combinaisons

         array_combine() : turn two arrays into one
         PDO::FETCH_KEY_PAIR :
         combine 2 columns in a hash

 <?php
 $html = file_get_contents("http://www.php.net/");

 //'<a href="http://php.net">PHP</a>';
 if (preg_match_all(
 '#<a href="(http://[^"/?]+/).*?>(.*?)</a>#s', $html, $r)) {
     $liste = array_combine($r[2], $r[1]);
 }
 print_r($liste);

 ?>
Arrays as SQL

        array_count_values() : makes easy stats
        Works like a GROUP BY and COUNT()

<?php
   $array = array(1, "hei", 1, "takk", "hei");
   print_r(array_count_values($array));
?>
                                     Array
          sort       r   u           (
                     r   u               [1] => 2
                                         [hei] => 2
           k    k   kr  uk
                                         [takk] => 1
           a    a   ar  ua           )
Arrays as SQL

                                        Array
                                        (
     array_multisort() : sort several       [0]   =>   2
     arrays at once                         [1]   =>   3
     Works like a ORDER BY                  [2]   =>   4
                                            [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
?>                                      )
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
Fast dir scans

Array
(
    [0]   =>   sess_um8rgjj10f6qvuck91rf36srj7
    [1]   =>   sess_u58rgul68305uqfe48ic467276
    [2]   =>   mysql.sock
    [3]   =>   ..          <?php
    [4]   =>   .           print_r(scandir('/tmp/', 1));
)                          print_r(glob('/tmp/sess_*'));
Array                      ?>
(
    [0]   => /tmp/sess_um8rgjj10f6qvuck91rf36srj7
    [1]   => /tmp/sess_u58rgul68305uqfe48ic467276
)
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

    Fill up an array or globals
URL


<?php
$url = 'http://login:pass@www.site.com/
         path/file.php?a=2+&b%5B%5D=
         %E5%AF%B9%E4%BA%86%EF%BC%81#ee';
$d = parse_url($url);
print_r($d);

parse_str($d["query"]);
var_dump($GLOBALS["b"]);
?>
URL

(
    [scheme] => http
    [host] => www.site.com
    [user] => login
    [pass] => pass
    [path] => /path/file.php
    [query] => a=2&b%5B%5D=%E5%AF%B9%E4%BA%86%EF%BC%81
    [fragment] => ee
)
array(1) {
  [0]=>
  string(9) "     "
}
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 &amp;
URL rebuilding

     http_build_query()
          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
URL testing

 <?php
    get_headers('http://localhost/logo.png');
 ?>

     Array
     (
         [0]   =>   HTTP/1.1 200 OK
         [1]   =>   Date: Mon, 12 Feb 2007 02:24:23 GMT
         [2]   =>   Server: Apache/1.3.33 (Darwin) PHP/5.2.1
         [3]   =>   X-Powered-By: eZ publish
         [4]   =>   Last-Modified: Fri, 30 Sep 2005 09:11:28 GMT
         [5]   =>   ETag: "f6f2a-dbb-433d0140"
         [6]   =>   Accept-Ranges: bytes
         [7]   =>   Content-Length: 3515
         [8]   =>   Connection: close
         [9]   =>   Content-Type: image/png
     )
PHP is dynamic

    Variables variables
    <?php
      $x = 'y';
      $y = 'z';
      $z = 'a';

      echo $x;  // displays y
      echo $$x;  // displays z
      echo $$$x; // displays a
    ?>
Variable constants

      Still one definition
      Dynamic constant value
  <?php
    define ("CONSTANT", 'eZ Conference');
    echo CONSTANT;
    echo constant("CONSTANT"); 
  ?>


      See the runkit to change constants...
Juggling with variables

     Compact() and extract()
 <?php 
  $x = 'a'; $y = 'b'; 
  $z = compact('x','y');   
 // $z = array('x'=> 'a', 'y' => 'b'); 

   $r = call_user_func_array('func', $z); 
 // calling func($x, $y); 

   extract($r); 
 // $x = 'c'; $y = 'd'; $t = 'e';
   list($x, $y, $t) = array_values($r);
 ?>
Variable functions

 <?php
  $func = 'foo'; $foo = 'bar';   $class = 'bb';

  $func($foo); // calling foo with bar
  call_user_func($func, $foo);// same as above

  call_user_func(array($class, $func), $foo); 
                        // now with objects!
  $class->$func($foo); // same as above
 ?>
Variable functions

 <?php
  $func = 'f';
  // a function
  // beware of langage construct such as empty()

  $func = array('c','m');
  // a static call to class c

  $func = array($o,'m');
  // a call to object o

  call_user_func($func, $foo);
 ?>
Object magic


  __autoload() : load classes JIT
  __sleep() and __wakeup()
    Store object and ressources in sessions
  __toString()   : to turn an object to a string
    __toArray() : get_object_vars($object);
    __toInteger() : may be?
Output buffering


   Avoid ‘already sent’ bug
   Clean it :   tidy
   Compress it : gz           <?php
                              ob_start("ob_gzhandler");
   Cache it                   echo "Hellon";
                              setcookie("c", "v");
                              ob_end_flush();
                              ?>
Caching

    auto_prepend :
      if ( filemtime($cache)+3600 < time()) {
          include($cachefile);     exit;
      }
      ob_start();
    auto_append :
        $content = ob_get_contents(); 
        file_put_contents('cache', $contents);
        ob_end_flush();

    Auto-caching upon 404 error pages
Connexion handling



  PHP maintains the connexion status
     0 Normal; 1 Aborted; 2 Timeout

  ignore_user_abort() to make a script without interruption
  connexion_status() to check status
Register for shutdown


     Function executed at script shutdown
     Correct closing of resources
     More convenient than ignore_user_abort() a library
     In OOP, better use __destruct()
     Storing calculated variables
Variables export

      var_export() : recreate PHP code for a variable

  <?php
                                              array (
                                                0 => 5,
  $array = array(5,4,3,2);
                                                1 => 4,
                                                2 => 3,
  file_put_contents('file.inc.php',
                                                3 => 2,
     '<?php $x = '.
                                              )
    var_export($array, true).
   '; ?>'
  );
  ?>
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
Assertions

<?php
assert_options(ASSERT_CALLBACK,'assert_callback');
function assert_callback($script,$line, $message){
   echo 'There is something strange 
         in your script <b>', $script,'</b> : 
         line <b>', $line,'</b> :<br />'; exit;
  }

   assert('is_integer( $x );' );
  assert('($x >= 0) && ($x <= 10); 
           //* $x must be from 0 to 10' );
  echo "0 <= $x <= 10";
?>
Debugging



  get_memory_usage()
    memory_limit is now on by default

    Better memory handling

    The fake growth of memory needs

  get_peak_memory_usage()
Debugging


  get_included_files()
  get_defined_constants/functions/vars()
  get_declared_classes()
  get_debug_backtrace()
    function stack and their arguments

    file and line calling
Debugging

   array(2) {
   [0]=>
   array(4) {
       ["file"] => string(10) "/tmp/a.php"
       ["line"] => int(10)
       ["function"] => string(6) "a_test"
       ["args"]=>
         array(1) { [0] => &string(6) "friend" }
   }
   [1]=>
   array(4) {
       ["file"] => string(10) "/tmp/b.php"
       ["line"] => int(2)
       ["args"] =>
         array(1) { [0] => string(10) "/tmp/a.php" }
       ["function"] => string(12) "include_once"
     }
   }
Slides

  http://www.nexen.net/english.php
  damien.seguy@nexen.net
  http://www.nexenservices.com/
Everyone
  loves
  PHP

More Related Content

What's hot (19)

PDF
Dependency Injection IPC 201
Fabien Potencier
 
PDF
PHP tips and tricks
Damien Seguy
 
PDF
Symfony2 - OSIDays 2010
Fabien Potencier
 
PDF
Unit and Functional Testing with Symfony2
Fabien Potencier
 
PDF
Symfony2 - WebExpo 2010
Fabien Potencier
 
PDF
Dependency Injection with PHP and PHP 5.3
Fabien Potencier
 
PDF
SPL: The Missing Link in Development
jsmith92
 
PDF
PhpBB meets Symfony2
Fabien Potencier
 
KEY
Php 101: PDO
Jeremy Kendall
 
KEY
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
PPTX
Looping the Loop with SPL Iterators
Mark Baker
 
PPT
Quebec pdo
Valentine Dianov
 
PDF
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Fabien Potencier
 
KEY
Fatc
Wade Arnold
 
PDF
PHP 5.3 Overview
jsmith92
 
PPTX
New in php 7
Vic Metcalfe
 
PPTX
Speed up your developments with Symfony2
Hugo Hamon
 
PPT
Intro to PHP
Sandy Smith
 
PDF
Doctrine 2
zfconfua
 
Dependency Injection IPC 201
Fabien Potencier
 
PHP tips and tricks
Damien Seguy
 
Symfony2 - OSIDays 2010
Fabien Potencier
 
Unit and Functional Testing with Symfony2
Fabien Potencier
 
Symfony2 - WebExpo 2010
Fabien Potencier
 
Dependency Injection with PHP and PHP 5.3
Fabien Potencier
 
SPL: The Missing Link in Development
jsmith92
 
PhpBB meets Symfony2
Fabien Potencier
 
Php 101: PDO
Jeremy Kendall
 
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
Looping the Loop with SPL Iterators
Mark Baker
 
Quebec pdo
Valentine Dianov
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Fabien Potencier
 
PHP 5.3 Overview
jsmith92
 
New in php 7
Vic Metcalfe
 
Speed up your developments with Symfony2
Hugo Hamon
 
Intro to PHP
Sandy Smith
 
Doctrine 2
zfconfua
 

Viewers also liked (7)

PDF
Php and-web-services-24402
PrinceGuru MS
 
DOC
SQL practice questions - set 3
Mohd Tousif
 
PDF
Deadlock
Mohd Tousif
 
PDF
Php tizag tutorial
PrinceGuru MS
 
PDF
Phpworks enterprise-php-1227605806710884-9
PrinceGuru MS
 
PDF
Firstcup
PrinceGuru MS
 
PDF
Unidad2
Summum7777
 
Php and-web-services-24402
PrinceGuru MS
 
SQL practice questions - set 3
Mohd Tousif
 
Deadlock
Mohd Tousif
 
Php tizag tutorial
PrinceGuru MS
 
Phpworks enterprise-php-1227605806710884-9
PrinceGuru MS
 
Firstcup
PrinceGuru MS
 
Unidad2
Summum7777
 
Ad

Similar to Php tips-and-tricks4128 (20)

PDF
PHP and MySQL Tips and tricks, DC 2007
Damien Seguy
 
PDF
lab4_php
tutorialsruby
 
PDF
lab4_php
tutorialsruby
 
PPT
PHP and MySQL with snapshots
richambra
 
PDF
Php reference sheet
Silvia Rios
 
PDF
PHP Tips & Tricks
Radek Benkel
 
PPT
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
PPTX
php is the most important programming language
padmanabanm47
 
PPT
Php course-in-navimumbai
vibrantuser
 
PPTX
Introduction to PHP Lecture 1
Ajay Khatri
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PPT
Arrays in php
Laiby Thomas
 
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
PPT
Php introduction
Osama Ghandour Geris
 
KEY
PHP security audits
Damien Seguy
 
DOCX
PHP record- with all programs and output
KavithaK23
 
PPT
PHP Workshop Notes
Pamela Fox
 
PDF
Php tutorial handout
SBalan Balan
 
PDF
Introduction to PHP
Bradley Holt
 
PPTX
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
PHP and MySQL Tips and tricks, DC 2007
Damien Seguy
 
lab4_php
tutorialsruby
 
lab4_php
tutorialsruby
 
PHP and MySQL with snapshots
richambra
 
Php reference sheet
Silvia Rios
 
PHP Tips & Tricks
Radek Benkel
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
php is the most important programming language
padmanabanm47
 
Php course-in-navimumbai
vibrantuser
 
Introduction to PHP Lecture 1
Ajay Khatri
 
PHP Functions & Arrays
Henry Osborne
 
Arrays in php
Laiby Thomas
 
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
Php introduction
Osama Ghandour Geris
 
PHP security audits
Damien Seguy
 
PHP record- with all programs and output
KavithaK23
 
PHP Workshop Notes
Pamela Fox
 
Php tutorial handout
SBalan Balan
 
Introduction to PHP
Bradley Holt
 
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
Ad

More from PrinceGuru MS (8)

PDF
Phpjedi 090307090434-phpapp01 2
PrinceGuru MS
 
PDF
Phpbasics
PrinceGuru MS
 
PDF
Php tutorial from_beginner_to_master
PrinceGuru MS
 
PDF
Php simple
PrinceGuru MS
 
PDF
Drupal refcard
PrinceGuru MS
 
PDF
Codeigniter 1.7.1 helper_reference
PrinceGuru MS
 
PDF
Class2011
PrinceGuru MS
 
PDF
Cake php 1.2-cheatsheet
PrinceGuru MS
 
Phpjedi 090307090434-phpapp01 2
PrinceGuru MS
 
Phpbasics
PrinceGuru MS
 
Php tutorial from_beginner_to_master
PrinceGuru MS
 
Php simple
PrinceGuru MS
 
Drupal refcard
PrinceGuru MS
 
Codeigniter 1.7.1 helper_reference
PrinceGuru MS
 
Class2011
PrinceGuru MS
 
Cake php 1.2-cheatsheet
PrinceGuru MS
 

Recently uploaded (20)

PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 

Php tips-and-tricks4128

  • 1. PHP tips and tricks Skien, Norge, June 7th 2007
  • 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 ) The month of PHP functions
  • 3. Who is speaking? Damien Séguy Nexen Services, eZ publish silver partner for hosting PHP / MySQL services Redacteur en chef of www.nexen.net Phather of an plush Elephpant http://www.nexen.net/english.php
  • 4. Nod when you know about it
  • 5. 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
  • 6. Random stuff Array <?php ( $a = range('a','d'); [0] => c shuffle($a); [1] => d [2] => b print_r($a); [3] => a ) print_r(array_rand($a,3)); Array ( print str_shuffle('abcdef'); [0] => 0 // eabdcf [1] => 1 ?> [2] => 3 )
  • 7. 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 )
  • 8. Arrays combinaisons array_combine() : turn two arrays into one PDO::FETCH_KEY_PAIR : combine 2 columns in a hash <?php $html = file_get_contents("http://www.php.net/"); //'<a href="http://php.net">PHP</a>'; if (preg_match_all( '#<a href="(http://[^"/?]+/).*?>(.*?)</a>#s', $html, $r)) {     $liste = array_combine($r[2], $r[1]); } print_r($liste); ?>
  • 9. Arrays as SQL array_count_values() : makes easy stats Works like a GROUP BY and COUNT() <?php $array = array(1, "hei", 1, "takk", "hei"); print_r(array_count_values($array)); ?> Array sort r u ( r u [1] => 2 [hei] => 2 k k kr uk [takk] => 1 a a ar ua )
  • 10. Arrays as SQL Array ( array_multisort() : sort several [0] => 2 arrays at once [1] => 3 Works like a ORDER BY [2] => 4 [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 ?> )
  • 11. 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
  • 12. Fast dir scans Array ( [0] => sess_um8rgjj10f6qvuck91rf36srj7 [1] => sess_u58rgul68305uqfe48ic467276 [2] => mysql.sock [3] => .. <?php [4] => . print_r(scandir('/tmp/', 1)); ) print_r(glob('/tmp/sess_*')); Array ?> ( [0] => /tmp/sess_um8rgjj10f6qvuck91rf36srj7 [1] => /tmp/sess_u58rgul68305uqfe48ic467276 )
  • 13. 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 Fill up an array or globals
  • 14. URL <?php $url = 'http://login:[email protected]/ path/file.php?a=2+&b%5B%5D= %E5%AF%B9%E4%BA%86%EF%BC%81#ee'; $d = parse_url($url); print_r($d); parse_str($d["query"]); var_dump($GLOBALS["b"]); ?>
  • 15. URL ( [scheme] => http [host] => www.site.com [user] => login [pass] => pass [path] => /path/file.php [query] => a=2&b%5B%5D=%E5%AF%B9%E4%BA%86%EF%BC%81 [fragment] => ee ) array(1) { [0]=> string(9) " " }
  • 16. 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 &amp;
  • 17. URL rebuilding http_build_query() 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
  • 18. URL testing <?php get_headers('http://localhost/logo.png'); ?> Array ( [0] => HTTP/1.1 200 OK [1] => Date: Mon, 12 Feb 2007 02:24:23 GMT [2] => Server: Apache/1.3.33 (Darwin) PHP/5.2.1 [3] => X-Powered-By: eZ publish [4] => Last-Modified: Fri, 30 Sep 2005 09:11:28 GMT [5] => ETag: "f6f2a-dbb-433d0140" [6] => Accept-Ranges: bytes [7] => Content-Length: 3515 [8] => Connection: close [9] => Content-Type: image/png )
  • 19. PHP is dynamic Variables variables <?php $x = 'y'; $y = 'z'; $z = 'a'; echo $x;  // displays y echo $$x;  // displays z echo $$$x; // displays a ?>
  • 20. Variable constants Still one definition Dynamic constant value <?php   define ("CONSTANT", 'eZ Conference');   echo CONSTANT;   echo constant("CONSTANT");  ?> See the runkit to change constants...
  • 21. Juggling with variables Compact() and extract() <?php   $x = 'a'; $y = 'b';   $z = compact('x','y');    // $z = array('x'=> 'a', 'y' => 'b');  $r = call_user_func_array('func', $z);  // calling func($x, $y);  extract($r);  // $x = 'c'; $y = 'd'; $t = 'e'; list($x, $y, $t) = array_values($r); ?>
  • 22. Variable functions <?php  $func = 'foo'; $foo = 'bar'; $class = 'bb';  $func($foo); // calling foo with bar call_user_func($func, $foo);// same as above  call_user_func(array($class, $func), $foo);  // now with objects! $class->$func($foo); // same as above ?>
  • 23. Variable functions <?php $func = 'f'; // a function // beware of langage construct such as empty() $func = array('c','m'); // a static call to class c $func = array($o,'m'); // a call to object o call_user_func($func, $foo); ?>
  • 24. Object magic __autoload() : load classes JIT __sleep() and __wakeup() Store object and ressources in sessions __toString() : to turn an object to a string __toArray() : get_object_vars($object); __toInteger() : may be?
  • 25. Output buffering Avoid ‘already sent’ bug Clean it : tidy Compress it : gz <?php ob_start("ob_gzhandler"); Cache it echo "Hellon"; setcookie("c", "v"); ob_end_flush(); ?>
  • 26. Caching auto_prepend : if ( filemtime($cache)+3600 < time()) {     include($cachefile);     exit; } ob_start(); auto_append :   $content = ob_get_contents();    file_put_contents('cache', $contents);   ob_end_flush(); Auto-caching upon 404 error pages
  • 27. Connexion handling PHP maintains the connexion status 0 Normal; 1 Aborted; 2 Timeout ignore_user_abort() to make a script without interruption connexion_status() to check status
  • 28. Register for shutdown Function executed at script shutdown Correct closing of resources More convenient than ignore_user_abort() a library In OOP, better use __destruct() Storing calculated variables
  • 29. Variables export var_export() : recreate PHP code for a variable <?php array ( 0 => 5, $array = array(5,4,3,2); 1 => 4, 2 => 3, file_put_contents('file.inc.php', 3 => 2, '<?php $x = '. ) var_export($array, true). '; ?>' ); ?>
  • 30. 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
  • 31. Assertions <?php assert_options(ASSERT_CALLBACK,'assert_callback'); function assert_callback($script,$line, $message){    echo 'There is something strange  in your script <b>', $script,'</b> :  line <b>', $line,'</b> :<br />'; exit; } assert('is_integer( $x );' );   assert('($x >= 0) && ($x <= 10);  //* $x must be from 0 to 10' );   echo "0 <= $x <= 10"; ?>
  • 32. Debugging get_memory_usage() memory_limit is now on by default Better memory handling The fake growth of memory needs get_peak_memory_usage()
  • 33. Debugging get_included_files() get_defined_constants/functions/vars() get_declared_classes() get_debug_backtrace() function stack and their arguments file and line calling
  • 34. Debugging array(2) { [0]=> array(4) { ["file"] => string(10) "/tmp/a.php" ["line"] => int(10) ["function"] => string(6) "a_test" ["args"]=> array(1) { [0] => &string(6) "friend" } } [1]=> array(4) { ["file"] => string(10) "/tmp/b.php" ["line"] => int(2) ["args"] => array(1) { [0] => string(10) "/tmp/a.php" } ["function"] => string(12) "include_once" } }
  • 35. Slides http://www.nexen.net/english.php [email protected] http://www.nexenservices.com/