Trucs & astuces PHP
.:PHP:.







web interpreted language
Auto. memory management
Weakly typed
Highly dynamic
Light and efficient
Cros...
PHP gotcha !
 Highly dynamic language
 Borrows some concepts from perl / C

 Lots of weird behaviors
 To be known

 C...
PHP gotcha

if (DONT_EXIST) {
echo "true";
} else {
echo "false";
}
PHP gotcha revealed
if (DONT_EXIST) {
echo "true";
} else {
echo "false";
}

if ("DONT_EXIST")

if (true)
PHP gotcha

$text = "foo and bar";
if (strpos($text, "foo") {
echo "foo is here";
} else {
echo "foo is not here";
}
PHP gotcha

echo (true?'true':false?'t':'f');
PHP gotcha revealed

echo (true?'true':false?'t':'f');

echo ( (true?'true':false) ?'t':'f' );
PHP gotcha

$a = 'foobar';
$a[10] = 'foo';
var_dump($a);
PHP gotcha revealed

$a = 'foobar';
$a[10] = 'foo';
var_dump($a);

string(11) "foobar

f"
PHP gotcha

$a = 11;
$a = $a++ + ++$a;
echo $a++;
PHP gotcha revealed
$a = 11;
$a = $a++ + ++$a;
echo $a++;

$a = 11;
$a = 11 + 13; //24
echo 24;
PHP gotcha
$array = array(1 => "Julien", "1" => "Seb", "Mag", 2 => "Leo");
echo count($array);
PHP gotcha revealed
$array = array(1 => "Julien", "1" => "Seb", "Mag", 2 => "Leo");
echo count($array);

$array = array( "...
PHP gotcha
$a = 'foo';
$b = 'bar';
echo $a . print('baz') . $b;
PHP gotcha revealed
$a = 'foo';
$b = 'bar';
echo $a . print('baz') . $b;

echo $a . print("baz" . $b);
"bazbarfoo1"
PHP gotcha

echo 0123 + 123 + 0;
PHP gotcha revealed

echo 0123 + 123 + 0;

echo 83 + 123 + 0;
PHP gotcha

$a = 'baz';
$a++;
echo $a;
PHP gotcha revealed

$a = 'baz';
$a++;
echo $a;

"bba"
PHP Tips
 PHP is full of functions and extensions
 Let's oversee some useful features
PHP Tip
 Array Flattening
$input = array('a', 'b', array('c', 'd'), 'e', array('f'), 'g');
$output = iterator_to_array(ne...
PHP Tip
 Automatic HTML cleaning
ob_start('ob_tidyhandler');
echo '<p>test</i>';

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML...
PHP Tip
 Get usefull info from MySQL
mysqli_report(MYSQLI_REPORT_INDEX);
$db = mysqli_connect('srv1', 'john', 'secret', '...
PHP Tip
 Apply a callback to every fetch from MySQL

$sql = "SELECT nom, prenom FROM auteurs";
$stmt = $pdo->query($sql);...
PHP Tip
 List a type of weekday coming in calendar
$begin = new DateTime('2009-11-01');
$end = new DateTime; // now
$inte...
PHP Certification
 70 Questions – 90min
 PHP 5.5
 Functions – OOP – arrays – streams – Xml – Databases –
Json – strings...
PHP certif quick overview
What's the output of this script ?
<?php
function oranges(&$oranges = 17) {
$oranges .= 1;
}
$ap...
PHP certif quick overview
What's design pattern can you recognize ?
<?php
class MyClassBuilder {
public function build() {...
PHP certif quick overview
In SimpleXML, you can use ______ method on SimpleXmlElement
to get all its children
PHP certif quick overview
What's the output of this script ?
<?php
class Foo {
const BAR = 4+1;
}
echo Foo::BAR;
A)4
B)5
C...
Upcoming SlideShare
Loading in …5
×

PHP Tips for certification - OdW13

5,608 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,608
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

PHP Tips for certification - OdW13

  1. 1. Trucs & astuces PHP
  2. 2. .:PHP:.       web interpreted language Auto. memory management Weakly typed Highly dynamic Light and efficient Cross-plateform
  3. 3. PHP gotcha !  Highly dynamic language  Borrows some concepts from perl / C  Lots of weird behaviors  To be known  Certification exam can fool you with such behaviors
  4. 4. PHP gotcha if (DONT_EXIST) { echo "true"; } else { echo "false"; }
  5. 5. PHP gotcha revealed if (DONT_EXIST) { echo "true"; } else { echo "false"; } if ("DONT_EXIST") if (true)
  6. 6. PHP gotcha $text = "foo and bar"; if (strpos($text, "foo") { echo "foo is here"; } else { echo "foo is not here"; }
  7. 7. PHP gotcha echo (true?'true':false?'t':'f');
  8. 8. PHP gotcha revealed echo (true?'true':false?'t':'f'); echo ( (true?'true':false) ?'t':'f' );
  9. 9. PHP gotcha $a = 'foobar'; $a[10] = 'foo'; var_dump($a);
  10. 10. PHP gotcha revealed $a = 'foobar'; $a[10] = 'foo'; var_dump($a); string(11) "foobar f"
  11. 11. PHP gotcha $a = 11; $a = $a++ + ++$a; echo $a++;
  12. 12. PHP gotcha revealed $a = 11; $a = $a++ + ++$a; echo $a++; $a = 11; $a = 11 + 13; //24 echo 24;
  13. 13. PHP gotcha $array = array(1 => "Julien", "1" => "Seb", "Mag", 2 => "Leo"); echo count($array);
  14. 14. PHP gotcha revealed $array = array(1 => "Julien", "1" => "Seb", "Mag", 2 => "Leo"); echo count($array); $array = array( "1" => "Seb", 2 => "Leo"); 2
  15. 15. PHP gotcha $a = 'foo'; $b = 'bar'; echo $a . print('baz') . $b;
  16. 16. PHP gotcha revealed $a = 'foo'; $b = 'bar'; echo $a . print('baz') . $b; echo $a . print("baz" . $b); "bazbarfoo1"
  17. 17. PHP gotcha echo 0123 + 123 + 0;
  18. 18. PHP gotcha revealed echo 0123 + 123 + 0; echo 83 + 123 + 0;
  19. 19. PHP gotcha $a = 'baz'; $a++; echo $a;
  20. 20. PHP gotcha revealed $a = 'baz'; $a++; echo $a; "bba"
  21. 21. PHP Tips  PHP is full of functions and extensions  Let's oversee some useful features
  22. 22. PHP Tip  Array Flattening $input = array('a', 'b', array('c', 'd'), 'e', array('f'), 'g'); $output = iterator_to_array(new RecursiveIteratorIterator( new RecursiveArrayIterator($input)), FALSE); var_dump($output); array('a', 'b', 'c', 'd', 'e', 'f', 'g');
  23. 23. PHP Tip  Automatic HTML cleaning ob_start('ob_tidyhandler'); echo '<p>test</i>'; <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN"> <html> <head> <title></title> </head> <body> <p>test</p> </body> </html>
  24. 24. PHP Tip  Get usefull info from MySQL mysqli_report(MYSQLI_REPORT_INDEX); $db = mysqli_connect('srv1', 'john', 'secret', 'my_db'); mysqli_query($db, "SELECT photo FROM Users WHERE source !='' LIMIT 1000"); PHP Warning: mysqli_query(): (00000/0): No index used in query/prepared statement SELECT photo FROM Users WHERE source !='' LIMIT 1000
  25. 25. PHP Tip  Apply a callback to every fetch from MySQL $sql = "SELECT nom, prenom FROM auteurs"; $stmt = $pdo->query($sql); $func = function ($nom, $prenom){printf('%s-%s',$nom,$prenom);}; $result = $stmt->fetchAll(PDO::FETCH_FUNC, $func);
  26. 26. PHP Tip  List a type of weekday coming in calendar $begin = new DateTime('2009-11-01'); $end = new DateTime; // now $interval = DateInterval::createFromDateString('next sunday'); $period = new DatePeriod($begin, $interval, $end); foreach ( $period as $dt ) { echo $dt->format( "l Y-m-d H:i:sn" ); } Sunday 2009-11-01 00:00:00 Sunday 2009-11-08 00:00:00 Sunday 2009-11-15 00:00:00 ...
  27. 27. PHP Certification  70 Questions – 90min  PHP 5.5  Functions – OOP – arrays – streams – Xml – Databases – Json – strings – securité – Ios – HTTP ...  Nothing known about how to pass the exam  Better know maximum of subjects
  28. 28. PHP certif quick overview What's the output of this script ? <?php function oranges(&$oranges = 17) { $oranges .= 1; } $apples = 5; oranges($apples); echo $apples++; A)16 B)51 C)15 D)6 E)52
  29. 29. PHP certif quick overview What's design pattern can you recognize ? <?php class MyClassBuilder { public function build() { return new MyClass(); } } A)builder B)factory C)singleton D)observer E)other pattern
  30. 30. PHP certif quick overview In SimpleXML, you can use ______ method on SimpleXmlElement to get all its children
  31. 31. PHP certif quick overview What's the output of this script ? <?php class Foo { const BAR = 4+1; } echo Foo::BAR; A)4 B)5 C)1 D)an error

×