Académique Documents
Professionnel Documents
Culture Documents
De l'esprit à la machine
L'approche Professo-Académique
Séances
Basics
www.maroclance.com
www.MarocLance.com
Typologie
www.MarocLance.com
◦ 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
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
<%="ZZCC" %>
<?="ZZCC"?>
<?="ZZCC” ; ?>
<%="ZZCC" ?>
<? ="ZZCC”%>
<?php="ZZCC” ; %>
◦ /* 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
$_ENV
$_FILES
$_GET
$_POST
$_REQUEST
$_SERVER
$_SESSION
www.MarocLance.com
PHP est un langage dit « de typage faible et
dynamique ».
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
echo "<hr>\n";
}
Astuce
When converting to boolean, the following values are
considered FALSE:
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
?>
<?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
$b = 1.2e3;
$c = 7E-10;
?>
<?php
$foo = "0"; // $foo is string (ASCII 48)
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
$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
<?php
$foo = 25;
$bar = &$foo; // This is a valid assignment.
$bar = &(24 * 7); // Invalid; references an unnamed expression.
function test()
{
return 25;
}
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
{
return 5;
}
?>
<?php
$first ? $second : $third
?>
Basic4.php
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
<?php
www.MarocLance.com
?>
<?php
www.MarocLance.com
?>
<?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
$a = 1;
$b = 2;
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
$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
<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
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