Web Applications

PHP Introduction

Objectives

  • PHP Introduction
  • Exercise: Rectangle
  • Demonstration: Reading JSON from a file
  • Exercise: Generating HTML form
  • Exercise: Dynamic HTML form generator

PHP Introduction

It is interpreted language, like Python. PHP files should utilize .php extension. You can run PHP script file using php ./file-to-run.php

PHP code section must start with <?php … ?>.

There is an online documentation with examples!

You can install locally or run using an online sandbox ( onlinephp.io, onecompiler.com, ... ).
There is PHP interpreter available at lab computers.

Variables

  • variables start with $
  • string concatenation with .
  • strings in single quotes uninterpreted: 'a $b \n'
  • strings in double quotes interpreted: "a $b \n"
  • usual operations +, -, * ...

$n = 3; // number

$b = true; // boolean

$s = 'abc'; // string

$s = "Value: $n"; // string with interpolation, like Python f-string
    

Functions


function doWork($name, $age) {
  return 42;
}

$result = doWork('Ailish', 23);
print($result);
     

Control structures


if (expression) stmt; elseif (expression) stmt2; else ...

while (expression) stmt;   

do stmt; while (expression);

for (init; expression; inc) stmt;

foreach (array as [ $key => ] $value) stmt;

switch (variable) { case ... default: }

...
     

Array


$array = [1, 2, 3];
$array[10] = 10;
var_dump($array);

$array["10"] = "ten"; // "10" and 10 are same keys due to auto-conversion.
var_dump($array);

$person = [ "name" => "Ailish", "age" => 12];
print($person["name"]."\n");
var_dump($person);

// Check if set and exists.
var_dump(isset($person));
var_dump(isset($person["name"]));
var_dump(isset($person["nickname"]));
     

Type Conversions


$number_as_string = "2";
$number = 40;

var_dump($number_as_string + $number); // 42
var_dump($number_as_string . $number); // 240
     

It is better to not rely on automatic type conversion (type juggling).

Exercise: Rectangle

Create a function that given size of a rectangle computes and print perimeter, and area. In addition this function renders the rectangle to output using ASCII art, see example bellow. Test the functions by calling it several times and properly handle different inputs.
Mind code decomposition!
Do NOT read from standard input, hard-code values in your code!

Example output for values: 5 3

  Perimeter: 16
  Area: 15

  *---*
  |...|
  *---*  
  

Make sure you properly handle edge-cases, like size 0, 1, and 2.

Bonus: upload the file to webik (public_html) and see what happens. Fix visual output to look like in the example above.

Demonstration: Reading JSON from a file

Employ PHP to load JSON from URL and render it to standard output. Utilize following functions:

Exercise: Generating HTML form

Paste code from next slide into a new PHP script file. Implement missing method to generate valid HTML form.

Input

This example of an input, you must support all items in the example.

$definition = [
  "action" => "/nswi142/practicals/script/02-handle-form.php",
  "fields" => [
    [
      "name" => "full_name",
      "label" => "Name",
      "type" => "text",
      "required" => true, // optional
    ],
    [
      "name" => "age",
      "label" => "Age",
      "type" => "number",
      "min" => 0, // optional
      "max" => 1000, // optional
      // we can have required here as well
    ],
    [
      "name" => "type",
      "type" => "hidden",
      "value" => "secret-code",
    ],
  ],
];
    

This example hosts all required functionality. Fields can be repeated multiple times. Ignore any unknown field type. Properties "name", "label", and "type" are always part of the data.

Output

Expected output for previous input, white spaces do not matter.

<!doctype html><meta charset=utf-8><title>Form</title>

<form action="/nswi142/practicals/script/02-handle-form.php" method="GET">

  <label>Name <input type="text" name="full_name" required> </label>

  <label>Age <input type="number" name="age" min="0" max="1000"> </label>

  <input type="hidden" name="type" value="secret-code">

  <input type="submit" value="Submit">

</form>
    

Form's name is fixed as well as submit button.

Exercise: Dynamic HTML form generator

Utilize result from previous exercise and

  • make sure you sanitize the output (htmlspecialchars),
  • make sure you properly handle type specific properties (for example do not render min and max for type=text even when provided),
  • load definition from https://webik.ms.mff.cuni.cz/nswi142/practicals/documents/04-form.json ,
  • upload your solution to public_html/04-form.php .

How are we doing?

  • PHP Introduction
  • Exercise: Rectangle
  • Demonstration: Reading JSON from a file
  • Exercise: Generating HTML form
  • Exercise: Dynamic HTML form generator

Leave an anonymous feedback.

Questions, ideas, or any other feedback?

Please feel free to use the anonymous feedback form.