Intro to PHP – Open the deck – You will learn



Intro to PHP – Open the deck – You will learn

0 0


php-pres


On Github aeud / php-pres

Intro to PHP

<h1>Intro to <?php echo "PHP"; ?></h1>

Open the deck

adrien.eudes.co/php-pres

You will learn

  • What PHP is
  • How a browser is communicating with a server
  • How to create a PHP environment
  • What is an algorithm
  • How to build sripts and functions in PHP
  • How to serve a web page via PHP

Hi PHP

What does PHP do?

  • Server-side scripting
  • Command-line scripting
  • Client-side GUI applications

A brief history of PHP

Rasmus Lerdorf first conceived of PHP in 1994

PHP 1.0 annoncement

  • Logging accesses to your pages in your own log files
  • Providing a nice interface to this log information
  • Displaying last access information right on your pages
  • Full daily and total access counters
  • Password protecting pages based on users' domains
  • Tracking accesses based on users' e-mail addresses
  • Easily create and display forms
  • Ability to use form information in following documents

A popular language

w3techs.com

Few steps back...

What is a web page?

Behind the scene

How do you request this page?

A HTTP request

URL, protocol

URL, hostname

URL, path

Back on the HTTP request

Where does this request go?

HTTP response

HTTP response

Now, let's see what a server looks like!

A server

From your laptop!

Or in the cloud

Cloud9

Connect

c9.io

Create a workspace

Welcome!

First scripts in PHP

Few rules

<?php PHP code is inside tags ?> A row ends with a ; $ prepends the variables /* Comment your code !! */

Variables I

<?php
$integer = 1;
$float = 1.5;

echo $integer + $float;
// 2.5 <- float

$string = 'foo';

echo $string . $integer;
// 'foo1' <- concatenation
?>

Variables II

<?php
$list = array('foo', 'bar', 'bar2');
echo $list[1];
// 'bar'

$list[] = 'foo2';
echo $list[3];
// 'foo2'

$array = array('f' => 'foo', 'b' => 'bar');
echo $array['f'];
// 'foo'
?>

Cheat sheets

How to script in PHP

The ECHO function

Create a file in your root folder script.php Write an easy code: <?php echo "Hello World"; ?> Save your file! From the console php script.php

Exercice 1

Write your first script

Write a script to caclculate 13! = factorial(13)

php factor.php
6227020800

(Small) intro to algorithmic

Question

What is an algorithm?

Always on paper first

X <- "READ FROM INPUT"

IF X < 10
    THEN echo X
ELSE IF X < 20
    THEN echo X * X
ELSE
    echo factor(X)

IF

<?php
// $i = 10;
if (/* $i == 0 */) {
    // echo 'Zero';
} elseif (/* $i <= 10 */) {
    // echo 'Small';
} else {
    // echo 'Big';
}
// TIP: "if + TAB"
?>

WHILE

<?php
// $i = 0;
while (/* $i < 10 */) {
    // echo $i;
    // $i = $i + 1;
}
// TIP: "while + TAB"
?>

FOR

<?php
for (/* $i=0; $i < 10; $i++ */) {
    // echo $i;
}
// TIP: "for + TAB"
?>

FOREACH

<?php
// $array = array('bob', 'jules', 'mark');
foreach (/* $array as $element */) {
    // echo $element;
}
// TIP: "foreach + TAB"
?>

Exercice 2

Factorial, again, on paper

Take a sheet of paper! Write a script to caclculate 13!, with a loop!

Exercice 2b

Factorial, again, in PHP

Create a new file loop.php Write a script to caclculate 13!, with a loop!
php factor.php
6227020800

Functions in PHP

FUNCTION

<?php
function FunctionName($var1, $var2)
{
    // code...
    return $var1 + $var2;
}
echo FunctionName(1, 2);
// 3
// TIP: "fun + TAB"
?>

Exercice 3

Factorial functions

Create a new file

php factor_function.php
6227020800

Should look like that

<?php
function factor($n)
{
    // code...
    return $something;
}
echo factor(13);
// 6227020800
?>

Recursive functions

<?php
function factorRec($n)
{
    if ($n == 1) {
        return 1;
    } else {
        return $n * factorRec($n - 1);
    }
    // return $n > 1 ? $n * factorRec($n - 1) : $n;
}
echo factorRec(13);
// 6227020800
?>

Exercice 3b

Fibonacci function

Create a new file

php fibo.php
233

How to link the HTTP request to our PHP script?

Tutorial

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Homepage</title>
</head>
<body>
    <h1>Awesome homepage</h1>
    <p>First paragraph</p>
    <a href="/factor.php">Surprise!</a>
</body>
</html>
Create a file index.html at the root of your workspace Preview it

Input

<input name="mynumber" />

simplehtmlguide.com

Form = plural of input

Value: Submit

<form action="/post.php" method="POST">;
    <label>Value:</label>
    <input name="mynumber" />
    <button type="submit">Submit</button>
</form>

Request sent with a POST form

Good news: this data is fetchable with PHP!

Tutorial 2

$_POST is the key

<?php
echo $_POST["mynumber"];
?>
Create a file post.php at the root of your workspace Refresh index.html in the preview Fill the form, and submit it...

Tutorial 3

Mix PHP and HTML

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Result</title>
</head>
<body>
    <h2>Your result is <b><?php echo $_POST["mynumber"]; ?></b></h2>
</body>
</html>
Updatepost.php Refresh index.html in the preview Fill the form, and submit it...

Exercice 4

Update your post.php file to apply the factorial function on the input, and display it

Q&A

adrien.eudes@sephoradigital.com