TEST 1(PHP)
Solutions
Assignment 1: Basic PHP Syntax and Output
Code:
<?php
echo "Hello,
World!";
Code:
<?php
echo "Name: John
Doe<br>";
echo "Today's
Date: " . date("Y-m-d");
Code:
<?php
$firstName = "John";
$lastName = "Doe";
$age = 25;
echo "My name is $firstName $lastName and I am $age years old.";
?>
Assignment 2: Variables and Data Types
Code:
$stringVar = "Hello,
World!";
$intVar = 100;
$floatVar = 10.5;
$boolVar = true;
$arrayVar = array("Apple",
"Banana", "Orange");
echo "String: $stringVar<br>";
echo "Integer: $intVar<br>";
echo "Float: $floatVar<br>";
echo "Boolean:
" . ($boolVar ? "true" : "false") . "<br>";
echo "Array:
" . implode(", ", $arrayVar);
Code:
$firstName = "John";
$lastName = "Doe";
echo "Full Name: " . $firstName . " " . $lastName;
Code:
$num1 = 10;
$num2 = 5;
echo "Addition: " . ($num1 + $num2) . "<br>";
echo "Subtraction:
" . ($num1 - $num2) . "<br>";
echo "Multiplication:
" . ($num1 * $num2) . "<br>";
echo "Division:
" . ($num1 / $num2);
?>
Assignment 3: Control Structures (if-else)
Code:
$num = -5;
if ($num > 0) {
echo "The number is positive.";
} elseif ($num < 0)
{
echo "The number is negative.";
} else {
echo "The number is zero.";
}
Code:
<?php
$age = 17;
echo "Eligible to vote.";
} else {
echo "Not eligible to vote.";
}
?>
Code:
<?php
$num = 4;
if ($num % 2 == 0) {
echo "The number is even.";
} else {
echo "The number is odd.";
}
?>
Assignment 4: Switch Statement
Code:
$day = 3;
switch ($day) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
case 4:
echo "Thursday";
break;
case 5:
echo "Friday";
break;
case 6:
echo "Saturday";
break;
case 7:
echo "Sunday";
break;
default:
echo "Invalid day";
}
Code:
<?php
$grade = 'B';
switch ($grade) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'F':
echo "Valid grade: $grade";
break;
default:
echo "Invalid grade.";
}
?>
Assignment 5: Loops
I) for loop
Code:
<?php
for ($i = 1; $i <= 10;
$i++) {
echo $i . " ";
}
Code:
$i = 1;
while ($i <= 10) {
echo $i . " ";
$i++;
}
Code:
<?php
$i = 1;
do {
echo $i . " ";
$i++;
} while ($i <= 10);
?>
Code:
$num = 5;
for ($i = 1; $i <= 10; $i++) {
echo "$num x $i = " . ($num * $i)
. "<br>";
}
Code:
<?php
$sum = 0;
$i = 1;
while ($i <= 100) {
$sum += $i;
$i++;
}
echo "The sum of numbers from 1 to 100 is: $sum";
?>
Assignment 6: Nested Loops
Code:
<?php
for ($i = 1; $i <= 5;
$i++) {
for ($j = 1; $j <= $i; $j++) {
echo "*";
}
echo "<br>";
}
?>
Code:
<?php
for ($i = 1; $i <= 5;
$i++) {
for ($j = 1; $j <= 5; $j++) {
echo "$i x $j = " . ($i * $j)
. "<br>";
}
echo "<br>";
}
Assignment 7: Combining Conditionals and Loops
Code:
for ($num = 2; $num
<= 50; $num++) {
$isPrime = true;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
$isPrime = false;
break;
}
}
if ($isPrime) {
echo $num . " ";
}
}
Code:
<?php
$num = 5;
$factorial = 1;
$factorial *= $i;
}
echo "Factorial of $num is $factorial";
Code:
<?php
$count = 0;
for ($i = 1; $i <= 100;
$i++) {
if ($i % 3 == 0 && $i % 5 == 0) {
$count++;
}
}
echo "Numbers
divisible by both 3 and 5: $count";