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.
$
.
'a $b \n'
"a $b \n"
$n = 3; // number
$b = true; // boolean
$s = 'abc'; // string
$s = "Value: $n"; // string with interpolation, like Python f-string
function doWork($name, $age) {
return 42;
}
$result = doWork('Ailish', 23);
print($result);
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 = [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"]));
$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).
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.
Employ PHP to load JSON from URL and render it to standard output. Utilize following functions:
$contentAsString = file_get_contents("path-url-here");
// https://www.php.net/manual/en/function.json-decode.php
// When is second argument true, we get an array.
$content = json_decode($content, true);
foreach ($content as $key => $value) {
$sanitizedKey = htmlspecialchars($key);
$sanitizedValue = htmlspecialchars($value);
print("$sanitizedKey : $sanitizedValue \n");
}
Paste code from next slide into a new PHP script file. Implement missing method to generate valid HTML form.
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.
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.
Utilize result from previous exercise and
Leave an anonymous feedback.