0% found this document useful (0 votes)
5 views13 pages

PHP Practice Worksheet With Answers

The document contains a series of PHP programming exercises and their corresponding solutions. It includes scripts for defining variables, conditional statements, loops, array manipulation, and function creation. Each question is followed by a sample PHP code that demonstrates the required functionality.

Uploaded by

ariraghad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

PHP Practice Worksheet With Answers

The document contains a series of PHP programming exercises and their corresponding solutions. It includes scripts for defining variables, conditional statements, loops, array manipulation, and function creation. Each question is followed by a sample PHP code that demonstrates the required functionality.

Uploaded by

ariraghad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Web Systems - 502315-3

PHP-Part01-Basics
PHP Practice Worksheet
Dr. Afnan Bukhari
Department of Information Technology

1
Question 1

1. Create a PHP script that:


- Defines two variables: your name
and your favorite color.
- Prints a sentence like:
> Hello, my name is Ali and my
favorite color is blue.

2
Answer to Question 1

<?php
$name = "Ali";
$color = "Blue";
echo "Hello, my name is $name and my favorite color is
$color.";
?>

3
Question 2

2. Write a PHP script that:


- Defines a variable called $grade.
- If $grade is greater than or equal to 90,
print "Excellent".
- If $grade is between 70 and 89, print
"Good".
- Otherwise, print "Needs Improvement".

4
Answer to Question 2

<?php
$grade = 85;
if ($grade >= 90) {
echo "Excellent";
} elseif ($grade >= 70) {
echo "Good";
} else {
echo "Needs Improvement";
}
?>

5
Question 3

3. Create a PHP script that:


- Prints the numbers from 1 to 10
using a for loop.

6
Answer to Question 3

<?php
for ($i = 1; $i <= 10; $i++) {
echo $i . "<br>";
}
?>

7
Question 4

4. Create a PHP script that:


- Prints each element of an array
Example: $fruits = array("Apple", "Banana",
"Cherry");
- Use a foreach loop.

8
Answer to Question 4

<?php
$fruits = array("Apple", "Banana", "Cherry");
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>

9
Question 5

5. Write a function named greet that:


- Takes one argument, a name.
- Returns "Hello, [name]!".
Example output if you call greet("Sara"):
> Hello, Sara!

10
Answer to Question 5

<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("Sara");
?>

11
Question 6

6. Write a PHP script that:


- Checks if a number is even or odd.
Example: 4 → Even, 7 → Odd.

12
Answer to Question 6

<?php
$number = 7;
if ($number % 2 == 0) {
echo "$number is Even";
} else {
echo "$number is Odd";
}
?>

13

You might also like