PHP Exam 1: Variables, Strings, and Operators (10 mixed exercises)

PHP Exam 1: Language Fundamentals

Test your PHP basics, including variables, echo, and working with strings and numbers.

Exercise 1 Syntax & Echo

Write a simple PHP snippet:

  1. Open a PHP tag (`<?php`).
  2. Use `echo` to print "Hello World".
  3. Add a single-line comment explaining the code.
Solution
<?php
// Print a welcome message
echo "Hello World";
?>
Exercise 2 Variables

Define the following variables:

  1. `$name` with your name (string).
  2. `$age` with your age (integer).
  3. `$price` with a price (float 10.5).
  4. Print: "My name is [name] and I am [age]".
Solution
<?php
$name = "Ahmed";
$age = 25;
$price = 10.5;

echo "My name is $name and I am $age";
?>
Exercise 3 Arithmetic

Perform the following operations:

  1. Define `$x = 10` and `$y = 4`.
  2. Print the sum.
  3. Print the division result.
  4. Print the modulus (remainder).
Solution
<?php
$x = 10;
$y = 4;

echo $x + $y; // 14
echo "<br>";
echo $x / $y; // 2.5
echo "<br>";
echo $x % $y; // 2
?>
Exercise 4 String Functions

Work with the string `$text = "Hello PHP"`:

  1. Print the string length (strlen).
  2. Print the word count (str_word_count).
  3. Reverse the string (strrev).
  4. Replace "PHP" with "World" (str_replace).
Solution
<?php
$text = "Hello PHP";

echo strlen($text); // 9
echo str_word_count($text); // 2
echo strrev($text); // PHP olleH
echo str_replace("PHP", "World", $text); // Hello World
?>
Exercise 5 Constants

Use constants:

  1. Define `SITE_NAME` as "My Website" using `define`.
  2. Define `TAX_RATE` as 0.15 using `const`.
  3. Print both values.
Solution
<?php
define("SITE_NAME", "My Website");
const TAX_RATE = 0.15;

echo SITE_NAME;
echo TAX_RATE;
?>
Exercise 6 Data Types & var_dump

Check data types:

  1. Define `$isActive = true`.
  2. Define `$scores = [10, 20]`.
  3. Define `$nothing = null`.
  4. Use `var_dump()` to print the type and value of each.
Solution
<?php
$isActive = true;
$scores = [10, 20];
$nothing = null;

var_dump($isActive);
var_dump($scores);
var_dump($nothing);
?>
Exercise 7 Logical Operators

Evaluate the following conditions (print the result):

  1. Is 10 greater than 5 AND 5 less than 2?
  2. Is 10 equal to 10 OR 5 equal to 0?
  3. What is NOT true?
Solution
<?php
var_dump(10 > 5 && 5 < 2); // bool(false)
var_dump(10 == 10 || 5 == 0); // bool(true)
var_dump(!true); // bool(false)
?>
Exercise 8 Increment/Decrement

Manipulate a variable:

  1. Define `$count = 10`.
  2. Increment it by 1 (`++`).
  3. Decrement it by 1 (`--`).
  4. Add 5 using `+=`.
Solution
<?php
$count = 10;
$count++; // 11
$count--; // 10
$count += 5; // 15

echo $count;
?>
Exercise 9 Math Functions

Use built-in math functions:

  1. Absolute value of -5 (abs).
  2. Square root of 64 (sqrt).
  3. Round 3.7 (round).
  4. Generate a random number between 1 and 100 (rand).
Solution
<?php
echo abs(-5); // 5
echo sqrt(64); // 8
echo round(3.7); // 4
echo rand(1, 100);
?>
Exercise 10 Mixing HTML with PHP

Write code that mixes both:

  1. Define `$color = "red"`.
  2. Print an `<h1>` containing "Hello".
  3. Set the text color using the `$color` variable (inline style).
Solution
<?php
$color = "red";
?>

<h1 style="color: <?php echo $color; ?>">Hello</h1>
Smart Editor

Write code and see the result instantly

Try it free