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:
- Open a PHP tag (`<?php`).
- Use `echo` to print "Hello World".
- Add a single-line comment explaining the code.
Solution
<?php
// Print a welcome message
echo "Hello World";
?>
Exercise 2
Variables
Define the following variables:
- `$name` with your name (string).
- `$age` with your age (integer).
- `$price` with a price (float 10.5).
- 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:
- Define `$x = 10` and `$y = 4`.
- Print the sum.
- Print the division result.
- 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"`:
- Print the string length (strlen).
- Print the word count (str_word_count).
- Reverse the string (strrev).
- 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:
- Define `SITE_NAME` as "My Website" using `define`.
- Define `TAX_RATE` as 0.15 using `const`.
- 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:
- Define `$isActive = true`.
- Define `$scores = [10, 20]`.
- Define `$nothing = null`.
- 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):
- Is 10 greater than 5 AND 5 less than 2?
- Is 10 equal to 10 OR 5 equal to 0?
- 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:
- Define `$count = 10`.
- Increment it by 1 (`++`).
- Decrement it by 1 (`--`).
- 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:
- Absolute value of -5 (abs).
- Square root of 64 (sqrt).
- Round 3.7 (round).
- 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:
- Define `$color = "red"`.
- Print an `<h1>` containing "Hello".
- Set the text color using the `$color` variable (inline style).
Solution
<?php
$color = "red";
?>
<h1 style="color: <?php echo $color; ?>">Hello</h1>