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:

  1. Define a function named `writeMsg`.
  2. The function prints "Hello world!".
  3. Call the function.
Solution
<?php
function writeMsg() {
    echo "Hello world!";
}

writeMsg();
?>
Exercise 2 Function Arguments

Create a function to add two numbers:

  1. Define `addNumbers` with parameters `$a` and `$b`.
  2. Return the sum.
  3. 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:

  1. Define `setHeight` with `$minheight` defaulting to 50.
  2. Print "The height is : $minheight".
  3. 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:

  1. Define `$x = 5` outside any function.
  2. Inside `myTest`, print `$x`.
  3. 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:

  1. Assume a form sends `name` and `email` via POST.
  2. Write PHP to receive and display them.
  3. 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:

  1. Assume: `test.php?subject=PHP&web=W3schools.com`.
  2. Use `$_GET` to read and display `subject` and `web`.
Solution
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
Exercise 7 Form Validation

Sanitize inputs:

  1. Create `test_input($data)`.
  2. Trim whitespace (`trim`).
  3. Remove slashes (`stripslashes`).
  4. 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:

  1. Check if POST `name` is empty (`empty`).
  2. If empty, set an error message in `$nameErr`.
  3. 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:

  1. Use `filter_var` to check if `$email` is valid.
  2. 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:

  1. Use a regex to check that `$website` is a valid URL.
  2. 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";
}
?>
Smart Editor

Write code and see the result instantly

Try it free