"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Friday, September 20, 2024

Solutions Of Test1

TEST 1(PHP)

From the Beginning Concept Upto Looping

Solutions

Assignment 1: Basic PHP Syntax and Output

Q1: Write a PHP script that outputs "Hello, World!".

Code:

<?php

echo "Hello, World!";

?>
Q2: Write a PHP script to print your name and today's date.

Code:

<?php

echo "Name: John Doe<br>";

echo "Today's Date: " . date("Y-m-d");

?>
Q3: Create a PHP script that stores your first name, last name, and age in variables and then prints them using a single echo statement.

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

Q1: Create a PHP script that demonstrates the following data types: String, Integer, Float, Boolean, and Array. Store appropriate values in variables and display them.

Code:

<?php

$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);

?>

Q2: Write a PHP script that concatenates two strings (first name and last name) and prints the full name.

Code:

<?php

$firstName = "John";

$lastName = "Doe";

echo "Full Name: " . $firstName . " " . $lastName;

?>
Q3: Create a script that performs arithmetic operations (addition, subtraction, multiplication, and division) using two integer variables and displays the result.

Code:

<?php

$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)

Q1 :Write a PHP script that checks if a given number is positive, negative, or zero using an if-else structure.

Code:

<?php

$num = -5;

if ($num > 0) {

    echo "The number is positive.";

} elseif ($num < 0) {

    echo "The number is negative.";

} else {

    echo "The number is zero.";

}

?>
Q2 :Create a script that checks if a person is eligible to vote. If the person’s age is 18 or above, print "Eligible to vote," otherwise print "Not eligible to vote."

Code:

<?php

$age = 17;

 if ($age >= 18) {

    echo "Eligible to vote.";

} else {

    echo "Not eligible to vote.";

}

?>

Q3 :Write a script that checks whether a number is odd or even and prints the result.

Code:

<?php

$num = 4;

if ($num % 2 == 0) {

    echo "The number is even.";

} else {

    echo "The number is odd.";

}

?>

Assignment 4: Switch Statement

Q1 :Create a PHP script that takes a variable representing the day of the week (1-7), and using a switch statement, prints the corresponding day (e.g., 1 = Monday, 2 = Tuesday, etc.).

Code:

<?php

$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";

}

?>
Q3: Write a PHP script to check whether a given grade (A, B, C, D, F) is valid or not using a switch statement, and print a corresponding message.

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

Q1: Write a PHP script that prints the numbers from 1 to 10 using:
I) for loop

Code:

<?php

for ($i = 1; $i <= 10; $i++) {

    echo $i . " ";

}

?>
II) while loop

Code:

<?php

$i = 1;

while ($i <= 10) {

    echo $i . " ";

    $i++;

}

?>
III)do-while loop

Code:

<?php

$i = 1;

do {

    echo $i . " ";

    $i++;

} while ($i <= 10);

?>

Q2 :Create a script that prints the multiplication table of a number (for example, 5) using a for loop.

Code:

<?php

$num = 5;

for ($i = 1; $i <= 10; $i++) {

    echo "$num x $i = " . ($num * $i) . "<br>";

}

?>
Q 3: Write a script that calculates the sum of numbers from 1 to 100 using a while loop.

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

Q1 : Write a PHP script to display the following pattern using nested for loops
                                

Code:

<?php

for ($i = 1; $i <= 5; $i++) {

    for ($j = 1; $j <= $i; $j++) {

        echo "*";

    }

    echo "<br>";

}

?>

Q 2: Create a script that prints a multiplication table from 1 to 5 using nested loops.

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

Q1 :Write a PHP script that prints all prime numbers between 1 and 50 using a for loop and if-else statements.

Code:

<?php

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 . " ";

    }

}

?>
Q 2: Create a script that calculates the factorial of a given number using a for loop.

Code:

<?php

$num = 5;

$factorial = 1;

 for ($i = $num; $i >= 1; $i--) {

    $factorial *= $i;

}

echo "Factorial of $num is $factorial";

?>
Q2: Write a script that counts how many numbers between 1 and 100 are divisible by both 3 and 5.

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";

?


*******************************

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template