Maxence POUTORD
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php echo "Hello World!"; ?>
</body>
</html>
Note: every line of PHP must end in ;
//A single line comment
/*
A multi-line
comment
*/
# Another one line comment (avoid)
$nothing = null;
$firstName = "John"; //with single quote
$lastName = 'WAYNE'; //with double quote
$name = "Bruce ".$lastName; //concatenation
$name .= " aka Batman"; //concatenation
$age = 51;
$young = false;
$pi = 3.14;
$object = new SomeObject();
echo "Hello $name!";
echo 'Hello ' . $firstName . $lastName .'!';
$hobbits = array("Frodo", "Bilbo", "Sam");
$lands[] = "Shire"; //$lands[0] == "Shire";
$lands[] = "Rohan"; //$lands[1] == "Rohan";
$lands[1] = "Gondor"; //$lands[1] == "Gondor";
echo "I live in $lands[1]";
$person['name'] = "Frodo";
$person['land'] = "Shire";
//Multidimensionnal array
$persons[0]['name'] = "Frodo";
$persons[0]['land'] = "Shire";
$persons[1]['name'] = "Gandalf";
$persons[1]['land'] = "Gondor";
$array[someIndex][someOtherIndex][someOtherIndex]...[] = $whatever;
$a = 42;
$b = 51;
$c = $b;
var_dump($a <= $b); //true
var_dump($b == $c); //true
var_dump($b == "51"); //true
var_dump($b === "51"); //false
if ($language == "fr") { echo "Bienvenue"; } elseif ($language == "en") { echo "Hello"; } else { echo "Ciao"; }
switch($language) { case "fr": echo "Bonjour"; Break; case "en": echo "Hello"; Break; case "it": echo "Ciao"; Break; default: echo "Bonjour"; }
for ($i = 1; $i <= 10; $i++) { echo $i; } //1 2 3 4 5 6 7 8 9 10
$i = 0; while (i <= 10) { echo $i; $i++; } //0 1 2 3 4 5 6 7 8 9 10
$fruits = array("banana", "apple", "cherry"); foreach ($fruits as $aFruit) { echo $fruit; } //banana apple cherry
$phrase = "Java is great!";
echo str_replace("Java", "PHP", $phrase); //"PHP is great!"
echo "We are ".date('d m Y');
A lot of String functions: http://php.net/manual/fr/ref.strings.phpfunction sayHello($name) { echo "Hello $name!"; } sayHello("World"); //Hello World!
function cube($var) { return $var * $var * $var; } $myVar = cube(4); //$myVar == 64
Constant | Description |
---|---|
E_ERROR | Fatal run-time errors. Execution of the script is halted |
E_WARNING | Non-fatal run-time errors. Execution of the script is not halted |
E_NOTICE | Run-time notices. The script found something that might be an error, but could also happen when running a script normally |
try {
echo 1/0;
} catch (Exception $e) {
echo "Can't divide by zero";
echo $e;
}
$headers = "From: Barack OBAMA <barack.obama@whitehouse.gov>";
$headers .= 'Content-type: text/html; charset=utf-8';
if (mail('kim-jong-un@korea-dpr.com', 'Hi guy', 'Hey, I\'m ready to fight', $headers)) {
echo "OK. Nulclear war is ready!";
} else {
echo "KO";
}
$headers = "From: My name <contact@me.fr>";
$headers .= 'Content-type: text/html; charset=utf-8';
$myFile = fopen('mailingList.txt', 'r');
while (($line = fgets($myFile)) !== false) {
mail($line, 'Hello', 'Visit my new website!', $headers)
}
fclose($myFile);
Mode | Details |
---|---|
r | Read |
r+ | Read/Write |
a | Read. Create if not exist. |
a+ | Read/Write. Create if not exist. |
4 ways:
//vars.php
$film = "Batman";
//index.php
include 'vars.php';
echo "I'm watching $film"; //I'm watching Batman
in a nutshell:
//Before
<?php echo $var; ?>
{# After #}
{{ var }}
<ul>
<?php
if (count($users) > 0) {
foreach ($users as $aUser) {
echo "<li>".$aUser['name']." live in ".$aUser['city']."</li>";
}
} else {
echo "<li>no user found</li>";
}
?>
</ul>
{% for aUser in users %}
- {{ aUser.name }} live in {{ aUser.name }}
{% else %}
- no user found
{% endfor %}
{{ "now"|date("m/d/Y") }} // 2/11/2015
{{ 'abcd...'|reverse }} //...dcba
{{ '12345'|slice(1, 2) }} //23
{{ 101.51|round }} //102
{# Multiple filters#}
{{ 'abcd...'|slice(1, 2)|reverse }} //cb
//kernel.php
include_once('../Twig/lib/Twig/Autoloader.php');
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem(__DIR__."/../views"); // view folder
$twig = new Twig_Environment($loader, array(
'cache' => false
));
//script.php
include_once('kernel.php');
$template = $twig->loadTemplate('welcome.html.twig');
echo $template->render(array(
'user' => $user,
'whatever' => $whatever
));
//layout.html.twig
<!DOCTYPE html>
<html>
<head>...</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
//welcome.html.twig
{% extends "layout.html.twig" %}
{% block body %}
{% if user %}
Welcome {{ user }}!
{% endif %}
{% endblock %}
echo $_POST['role']; echo $_POST['name'];
http://www.mywonderfulapp.com/script.php?firstname=john&lastname=doe&age=42 echo $_GET['firstname'], " ", $_GET['lastname'], $_GET['age']; //john doe 42
echo $_GET["message"];
echo htmlspecialchars($_GET["message"]); //PHP {{ message }} //twig
include "include/".$_GET["page"].".php";
//http://www.vulnerable_host.com/preview.php?page=../../../../etc/passwd%00
PDO = PHP Data Object
Secure request
try {
$bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8',
'root', 'root');
$reponse = $bdd->query('SELECT * FROM beers');
$data = $reponse->fetchAll();
} catch(Exception $e) {
die("Cannot connect to database server");
}
Now $data is an array of beers
$request = $bdd->prepare("SELECT * FROM beers WHERE id = :id");
$request->execute(array(
':id' => $_GET['id']
));
$aBeer = $request->fetch();
Now $data contain one beer
Avoid SQL Injection
$request = $bdd->prepare("INSERT INTO beers (name, description)
VALUES (:name, :description)");
$request->bindParam(':name', $name);
$request->bindParam(':description', $description);
$name = "Mélusine Bière de Noël";
$description = "Merry christmas!";
$request->execute();
$name = "Kronembourg";
$description = "You get what you pay for...";
$request->execute();
//UPDATE
$sql = 'UPDATE beers set name=:name where id=:id';
$request = $bdd->prepare($sql);
$request->execute(array(
':name' => 'Grimbergen',
':id' => 1
));
//DELETE
$sql = 'DELETE from beers where id=:id';
$request = $bdd->prepare($sql);
$request->execute(array(':id' => 1));
Don't copy/past like an idiot
Try to stick PSR Standard Recommendations
http://www.php-fig.orgWhy ? Improve...
I recommend Symfony2 (most popular in France)
Keep moving: https://www.codecademy.com/tracks/php