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

web interpreted language
Auto. memory management
Weakly typed
Highly dynamic
Light and efficient
Cross-plateform
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
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( "1" => "Seb", 2 => "Leo");
2
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(new RecursiveIteratorIterator(
new RecursiveArrayIterator($input)), FALSE);
var_dump($output);

array('a', 'b', 'c', 'd', 'e', 'f', 'g');
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>
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
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);
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
...
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
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
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
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)1
D)an error

PHP Tips for certification - OdW13

  • 1.
  • 2.
    .:PHP:.       web interpreted language Auto.memory management Weakly typed Highly dynamic Light and efficient Cross-plateform
  • 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.
    PHP gotcha if (DONT_EXIST){ echo "true"; } else { echo "false"; }
  • 5.
    PHP gotcha revealed if(DONT_EXIST) { echo "true"; } else { echo "false"; } if ("DONT_EXIST") if (true)
  • 6.
    PHP gotcha $text ="foo and bar"; if (strpos($text, "foo") { echo "foo is here"; } else { echo "foo is not here"; }
  • 7.
  • 8.
    PHP gotcha revealed echo(true?'true':false?'t':'f'); echo ( (true?'true':false) ?'t':'f' );
  • 9.
    PHP gotcha $a ='foobar'; $a[10] = 'foo'; var_dump($a);
  • 10.
    PHP gotcha revealed $a= 'foobar'; $a[10] = 'foo'; var_dump($a); string(11) "foobar f"
  • 11.
    PHP gotcha $a =11; $a = $a++ + ++$a; echo $a++;
  • 12.
    PHP gotcha revealed $a= 11; $a = $a++ + ++$a; echo $a++; $a = 11; $a = 11 + 13; //24 echo 24;
  • 13.
    PHP gotcha $array =array(1 => "Julien", "1" => "Seb", "Mag", 2 => "Leo"); echo count($array);
  • 14.
    PHP gotcha revealed $array= array(1 => "Julien", "1" => "Seb", "Mag", 2 => "Leo"); echo count($array); $array = array( "1" => "Seb", 2 => "Leo"); 2
  • 15.
    PHP gotcha $a ='foo'; $b = 'bar'; echo $a . print('baz') . $b;
  • 16.
    PHP gotcha revealed $a= 'foo'; $b = 'bar'; echo $a . print('baz') . $b; echo $a . print("baz" . $b); "bazbarfoo1"
  • 17.
  • 18.
    PHP gotcha revealed echo0123 + 123 + 0; echo 83 + 123 + 0;
  • 19.
    PHP gotcha $a ='baz'; $a++; echo $a;
  • 20.
    PHP gotcha revealed $a= 'baz'; $a++; echo $a; "bba"
  • 21.
    PHP Tips  PHPis full of functions and extensions  Let's oversee some useful features
  • 22.
    PHP Tip  ArrayFlattening $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.
    PHP Tip  AutomaticHTML 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.
    PHP Tip  Getusefull 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.
    PHP Tip  Applya 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.
    PHP Tip  Lista 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.
    PHP Certification  70Questions – 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.
    PHP certif quickoverview 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.
    PHP certif quickoverview 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.
    PHP certif quickoverview In SimpleXML, you can use ______ method on SimpleXmlElement to get all its children
  • 31.
    PHP certif quickoverview 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