PHP Exam 3: Functions, Forms, and Validation (10 mixed exercises)
PHP Exam 3: Functions and Forms
Test your skills in organizing code with functions and handling user input from forms.
Exercise 1
Basic Function
Create a greeting function:
- Define a function named `writeMsg`.
- The function prints "Hello world!".
- Call the function.
Solution
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg();
?>
Exercise 2
Function Arguments
Create a function to add two numbers:
- Define `addNumbers` with parameters `$a` and `$b`.
- Return the sum.
- Print the result of calling it with 5 and 10.
Solution
<?php
function addNumbers($a, $b) {
return $a + $b;
}
echo addNumbers(5, 10); // 15
?>
Exercise 3
Default Arguments
Create a function with a default value:
- Define `setHeight` with `$minheight` defaulting to 50.
- Print "The height is : $minheight".
- Call it with 350 and without arguments.
Solution
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // uses 50
?>
Exercise 4
Global Variables
Access a variable outside a function:
- Define `$x = 5` outside any function.
- Inside `myTest`, print `$x`.
- Use the `global` keyword to access it.
Solution
<?php
$x = 5;
function myTest() {
global $x;
echo $x;
}
myTest();
?>
Exercise 5
Form Handling (POST)
Receive form data:
- Assume a form sends `name` and `email` via POST.
- Write PHP to receive and display them.
- Use `$_POST['key']`.
Solution
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
echo "Welcome $name<br>";
echo "Your email is: $email";
}
?>
Exercise 6
Form Handling (GET)
Read data from the URL:
- Assume: `test.php?subject=PHP&web=W3schools.com`.
- Use `$_GET` to read and display `subject` and `web`.
Solution
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
Exercise 7
Form Validation
Sanitize inputs:
- Create `test_input($data)`.
- Trim whitespace (`trim`).
- Remove slashes (`stripslashes`).
- Escape special chars (`htmlspecialchars`) to prevent XSS.
Solution
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Exercise 8
Required Fields
Check that a field is not empty:
- Check if POST `name` is empty (`empty`).
- If empty, set an error message in `$nameErr`.
- Otherwise, store the cleaned value in `$name`.
Solution
<?php
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
?>
Exercise 9
Email Validation
Validate an email:
- Use `filter_var` to check if `$email` is valid.
- Use `FILTER_VALIDATE_EMAIL`.
Solution
<?php
$email = "test@example.com";
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format";
} else {
echo "Valid email";
}
?>
Exercise 10
URL Validation
Validate a URL:
- Use a regex to check that `$website` is a valid URL.
- Use `preg_match` with the pattern `/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i`.
Solution
<?php
$website = "https://www.google.com";
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $website)) {
echo "Invalid URL";
} else {
echo "Valid URL";
}
?>