Vous êtes sur la page 1sur 126

ZCE

De l'esprit à la machine
L'approche Professo-Académique

Séances
Basics

www.maroclance.com
www.MarocLance.com
 Typologie
www.MarocLance.com

◦ Toute instruction se termine par un point-virgule

◦ Sensible à la casse
 Sauf par rapport aux fonctions
www.MarocLance.com
Astuces
◦ Last ;

www.MarocLance.com
 Astuces
www.MarocLance.com

<?php
echo 'This is a test';
?>

<?php echo 'This is a test' ?>

<?php echo 'This is a test';

<?php echo 'This is a test'


 Astuces
www.MarocLance.com

<?php
for($i = 0; $i < 10; $i ++)
echo $i

?>
<?php
for($i = 0; $i < 10; $i ++)
echo $i
<?php
for($i = 0; $i < 10; $i ++)
echo $i ;
 Principe
www.MarocLance.com

◦ Les scripts PHP sont généralement intégrés dans le code d’un


document HTML

◦ L’intégration nécessite l’utilisation de balises

 avec le style xml : <? ligne de code PHP ?>

 Avec le style php: <?php ligne de code PHP ?>

 avec le style JavaScript :


<script language=«php»> ligne de code PHP </script>

 avec le style des ASP : <% ligne de code ASP %>


www.MarocLance.com
 Astuces
www.MarocLance.com

<%="ZZCC" %>
<?="ZZCC"?>

<?="ZZCC” ; ?>

<?="ZZCC” ; echo ’yes’ ;?>


<?="ZZCC” ; echo ’yes’ ?>
 Astuces
www.MarocLance.com

<%="ZZCC" ?>
<? ="ZZCC”%>

<?php="ZZCC” ; %>

<?php echo "ZZCC” ;%>


 Astuce avancé
www.MarocLance.com

<\xf3php echo 'foo'; \x3f>


 Les commentaires
www.MarocLance.com

◦ /* Voici un commentaire! */
◦ // un commentaire sur une ligne
 Astuce
 comment with #
www.MarocLance.com

 <?php
/*
echo 'This is a test'; /* This comment will cause a problem */
*/
?>
 <?php
/* echo 'This a test'; //This oblem */
/*** echo 'This a test'; This oblem */?>
• <?php

/** echo 'This a test'; This oblem */ **/


/** echo 'This a test'; This oblem /* **/

/** echo 'This a test'; This oblem /***/?>


Closures

www.MarocLance.com
www.MarocLance.com
 $_GLOBALS
$_COOKIE
www.MarocLance.com

 $_ENV
 $_FILES
 $_GET
 $_POST
 $_REQUEST
 $_SERVER
 $_SESSION
www.MarocLance.com
 PHP est un langage dit « de typage faible et
dynamique ».
www.MarocLance.com

 L’utilisation en lecture d’une variable non


initialisée n’est pas non plus un problème.
Une variable inexistante renverra la valeur
NULL et, selon votre configuration, pourra ne
pas afficher d’erreur. Il vous faut donc faire
attention à l’orthographe de vos variables.
www.MarocLance.com
www.MarocLance.com
 string gettype($mavar)
www.MarocLance.com
 is_integer($var) ou is_int($var)
is_double($var)
www.MarocLance.com

 is_string($var)
 is_bool($var)
 is_array($var)
 is_object($var)
 is_resource($var)
 is_null($var)
 is_scalar
basic1.php

www.MarocLance.com
 This is the simplest type.
A boolean expresses a truth value. It can be
www.MarocLance.com

either TRUE or FALSE.


 Astuce
To specify a boolean literal, use the
www.MarocLance.com

keywords TRUE or FALSE. Both are case-


insensitive.
 // this is not necessary...
if ($show_separators == TRUE) {
www.MarocLance.com

echo "<hr>\n";
}

// ...because this can be used with exactly th


e same meaning:
if ($show_separators) {
echo "<hr>\n";
}
 (bool) variable
Var_dump
www.MarocLance.com


Astuce
When converting to boolean, the following values are
considered FALSE:
www.MarocLance.com

 the boolean FALSE itself


 the integer 0 (zero)
 the float 0.0 (zero)
 the empty string, and the string "0"
 an array with zero elements
 an object with zero member variables (PHP 4 only)
 the special type NULL (including unset variables)
 SimpleXML objects created from empty tags

Every other value is considered TRUE (including


any resource).
Astuce
 -1?
www.MarocLance.com
Basci2.php

www.MarocLance.com
astuces
www.MarocLance.com


<?php
$a = !!array();
$s = !!"testing";
$s = !"";
$s = !"hello";
?>
astuces
www.MarocLance.com


<?php
$a = !array();
$a = !array('a');
$s = !"";
$s = !"hello";
?>
If() else
www.MarocLance.com

 Basic3.php
An integer is a number of the set ℤ = {..., -2, -
1, 0, 1, 2, ...}.
www.MarocLance.com
Integers can be specified in decimal (base 10),
hexadecimal (base 16), octal (base 8) or
www.MarocLance.com

binary (base 2) notation, optionally preceded


by a sign (- or +).
Integers can be specified in decimal (base 10),
hexadecimal (base 16), octal (base 8) or
www.MarocLance.com

binary (base 2) notation, optionally preceded


by a sign (- or +).
- To use octal notation, precede the number
with a 0 (zero).
www.MarocLance.com

- To use hexadecimal notation precede the


number with 0x.
- To use binary notation precede the number
with 0b .
<?php
$a = 1234; // decimal number
www.MarocLance.com

$a = -123; // a negative number


$a = 0123; // octal number (equivalent to 83
decimal)
$a = 0x1A; // hexadecimal number (equivale
nt to 26 decimal)
?>
<?php
$a = 1234; // decimal number
www.MarocLance.com

$a = -123; // a negative number


$a = 0123; // octal number (equivalent to 83
decimal)
$a = 0x1A; // hexadecimal number (equivale
nt to 26 decimal)
?>
<?php
var_dump(01090);
www.MarocLance.com

?>
<?php
var_dump(25/7); // float(3.5714285714
www.MarocLance.com

286)
var_dump((int) (25/7)); // int(3)
var_dump(round(25/7)); // float(4)
?>

Astuce
Floor
- (int) or (integer) casts
- Intval(http://www.php.net/manual/en/functi
www.MarocLance.com

on.intval.php)
- <?php
$foo = 1 + "10.5"; // $foo is float (11.5)
www.MarocLance.com

$foo = 1 + "-1.3e3"; // $foo is float (-1299)


$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1; // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)
?>
 Floating point numbers (also known as
"floats", "doubles", or "real numbers")
www.MarocLance.com
 <?php
$a = 1.234;
www.MarocLance.com

$b = 1.2e3;
$c = 7E-10;
?>
 <?php
$foo = "0"; // $foo is string (ASCII 48)
www.MarocLance.com

$foo += 2; // $foo is now an integer (2)


$foo = $foo + 1.3; // $foo is now a float (3.
3)
$foo = 5 + "10 Little Piggies"; // $foo is integ
er (15)
$foo = 5 + "10 Small Pigs"; // $foo is integ
er (15)
?>
 <?php
$a = 'car'; // $a is a string
www.MarocLance.com

$a[0] = 'b'; // $a is still a string


echo $a; // bar
?>
 <?php
$foo = 10; // $foo is an integer
www.MarocLance.com

$bar = (boolean) $foo; // $bar is a boolean


?>
 The casts allowed are:
 (int), (integer) - cast to integer
www.MarocLance.com

 (bool), (boolean) - cast to boolean


 (float), (double), (real) - cast to float
 (string) - cast to string
 (array) - cast to array
 (object) - cast to object
 (unset) - cast to NULL (PHP 5)
 (binary) casting and b prefix forward support
was added in PHP 5.2.1
 settype("12", 'integer')
www.MarocLance.com
 Intval
Doubleval
www.MarocLance.com

 Floatval
 strval
 The special NULL value represents a variable
with no value. NULL is the only possible value
www.MarocLance.com

of type null.
A variable is considered to be null if:
 it has been assigned the constant NULL.
www.MarocLance.com

 it has not been set to any value yet.

 it has been unset().


is_null() and unset().
www.MarocLance.com
Astuce
a = array();
www.MarocLance.com

$a == null
$a === null
is_null($a)
www.MarocLance.com
 $_GLOBALS
$_COOKIE
www.MarocLance.com

 $_ENV
 $_FILES
 $_GET
 $_POST
 $_REQUEST
 $_SERVER
 $_SESSION
 http://www.php.net/manual/en/reserved.vari
ables.php
www.MarocLance.com
Astuce
$täyte = 'mansikka';
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
 <?php
$foo = 'Bob'; // Assign the value 'Bob' to $foo
www.MarocLance.com

$bar = &$foo; // Reference $foo via $bar.


$bar = "My name is $bar"; // Alter $bar...
echo $bar;
echo $foo; // $foo is altered too.
?>
 Astuce
www.MarocLance.com

<?php
$foo = 25;
$bar = &$foo; // This is a valid assignment.
$bar = &(24 * 7); // Invalid; references an unnamed expression.

function test()
{
return 25;
}

$bar = &test(); // Invalid.


?>
Astuce

www.MarocLance.com
www.MarocLance.com
 <?php
$a = 1;
$b = 2;
www.MarocLance.com

function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'
];
}
Sum();
echo $b;
?>
www.MarocLance.com
www.MarocLance.com
 <?php
define('FOO_BAR','It works!');
www.MarocLance.com

define('FOO_FOO_BAR','It works again!');


// prints 'It works!'
$changing_variable = 'bar';
echo constant('FOO_' . strtoupper($changing_variable));
// prints 'It works again!'
$changing_variable = 'foo_bar';
echo constant('FOO_' . strtoupper($changing_variable));
?>
Scalable only

www.MarocLance.com
defined

www.MarocLance.com
 http://www.php.net/manual/en/language.co
nstants.predefined.php
www.MarocLance.com
 <?php
function foo ()
www.MarocLance.com

{
return 5;
}
?>

 <?php
$first ? $second : $third
?>
Basic4.php

www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
 <?php
www.MarocLance.com

echo (5 % 3)."\n"; // prints 2


echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2

?>
 <?php
www.MarocLance.com

echo (5 % 3)."\n"; // prints 2


echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2

?>
 <?php
www.MarocLance.com

$a = ($b = 4) + 5;
 // $a is equal to 9 now, and $b has been set
to 4.

?>
www.MarocLance.com
www.MarocLance.com
 Astuce
Bitwise XOR operations on strings
<?php
www.MarocLance.com

echo 12 ^ 9; // Outputs '5'


echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
// ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8
echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
// 'a' ^ 'e' = #4
echo 2 ^ "3"; // Outputs 1
// 2 ^ ((int)"3") == 1
echo "2" ^ 3; // Outputs 1
// ((int)"2") ^ 3 == 1
?>
www.MarocLance.com
 <?php
var_dump(0 == "a"); // 0 == 0 -> true
www.MarocLance.com

var_dump("1" == "01"); // 1 == 1 -> true


var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
 <?php
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
// ternary operator associativity differs from C/C++
www.MarocLance.com

$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2

$a = 1;
$b = 2;
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5

// mixing ++ and + produces undefined behavior


$a = 1;
echo ++$a + $a++; // may print 4 or 5
?>
 <?php
$a = 1;
echo $a + $a++; // may print either 2 or 3
www.MarocLance.com

$i = 1;
$array[$i] = $i++; // may set either index 1 or 2
?>
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
 <?php if ($a == 5): ?>
A is equal to 5
www.MarocLance.com

<?php endif; ?>


 <?php
if ($a == 5):
www.MarocLance.com

echo "a equals 5";


echo "...";
elseif ($a == 6):
echo "a equals 6";
echo "!!!";
else:
echo "a is neither 5 nor 6";
endif;
?>
 <div>
<?php switch($variable):
case 1: ?>
www.MarocLance.com

<div>
Newspage
</div>
<?php break;?>
<?php case 2: ?>
</div>
Forum
<div>
<?php break;?>
<?php endswitch;?>
</div>
 <?php
/* example 1 */
www.MarocLance.com

$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
/* example 2 */
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;
?>
 Break n;
Continue n;
www.MarocLance.com

 Case x: case y : case z


 Case x; and not case x :
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
declare

www.MarocLance.com
 Goto
www.MarocLance.com

goto jumpToHere;
echo 'Hello';

jumpToHere:
echo 'World';
Echo vs print

www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
 Function and variables scopes
www.MarocLance.com
 Astuce
Func($a,$b=$a) n
www.MarocLance.com

 Func($a,$b=5,$b=4,$b=7) y
 Function and variables scopes
//global
www.MarocLance.com
 Dynamic arguments
www.MarocLance.com
 Dynamic arguments
www.MarocLance.com
 Variable function
$a=’func’
www.MarocLance.com

$a($n);
 <?php
$greet = function($name)
www.MarocLance.com

{
printf("Hello %s\r\n", $name);
};

$greet('World');
$greet('PHP');
?>
 <?php
function foo()
{
www.MarocLance.com

function bar()
{
echo "I don't exist until foo() is called.\n";
}
}
/* We can't call bar() yet
since it doesn't exist. */
foo();
/* Now we can call bar(),
foo()'s processing has
made it accessible. */
bar();
?>
 Folder:
◦ Absolute
www.MarocLance.com

◦ Relative
◦ Relative to current
<?php
namespace ns;
www.MarocLance.com

?>
Astuce
//
<?php

namespace ns;
 Astuce
www.MarocLance.com

<?php
$a=true;
namespace ns;
Astuce

<?php
$a;
namespace ns;
 Astuce : rule
all except declare keyword
www.MarocLance.com
 Astuce : rule
all except declare keyword
www.MarocLance.com
 The performance of the PHP script can be improved drastically.
The execution process of PHP is done in two steps, i.e., first the
PHP code, written in plain-text, is compiled to opcodes and then
www.MarocLance.com

those opcodes are executed.


When you have one PHP script, as long as it is not modified, the
opcodes will always be the same. Hence, in the compilation
phase, each time that script has to be executed. It is a waste of
CPU time. To prevent this redundant compilation, there are
various opcode caching mechanisms.
Once the PHP script has been compiled to opcodes, these
opcodes are kept into the RAM. These are directly used from
memory the next time the script is executed. This prevents the
compilation from being done again and again.

Vous aimerez peut-être aussi