Document

Arithmetic Operations

Program A. Write a PHP script to calculate the area of a rectangle using arithmetic operators. Display the result.(area of a rectangle: $area = $length * $width;)

Solution

$length = 10;
$width = 5;
$area = $length * $width;
echo "$area"


Output

Area of rectangle: 50





Program B. Implement a script that converts currency from one denomination to another using arithmetic operators. Display the converted amount. (Convert currency: $converted_amount = $amount * $conversion)

Solution

$amount = 10;
$conversion_rate = 56.21;
$converted_amount = $amount * $conversion_rate;
echo "Equivalent in dollar: $converted_amount";


Output

Equivalent in dollar: 562.1




Assignment Operators

Program A. Track the progress of a fundraising campaign by updating the donation total dynamically using combined assignment operators. Display the updated total.
(Track fundraising: $total_donation += $new_donation)

Solution

$total_donation = 0;
$new_donation = 500;
$total_donation += $new_donation;
echo "Track fundraising: $total_donation"


Output

Track fundraising: 500





Program B.
Implement a voting system where candidate votes are incremented based on user input. Display the final vote count for each candidate. (Voting System: $candidate_votes +=$user_vote;)

Solution

$candidate_votes = 5;
$user_vote = 1;
$candidate_votes += $user_vote;
echo "$candidate_votes";


Output

Voting system: 6




Comparison Operators

Program A. Compare the performance of two athletes in a race and determine the winner using comparison operators. Display the result. (Compare athlete times: Use '<' to compare times.)

Solution

$athlete1 = 6.70;
$athlete2 = 9.10;
$winner = ($athlete1>$athlete2)?"Athlete 1" : "Athlete 2";
echo "Winner: $winner";


Output:

Winner: Athlete 2





Program B.
Evaluate the efficiency of two algorithms based on their execution times using comparison operators. Display the more efficient algorithm. (Evaluate algorithm efficiency: Use '<' to compare times.)

Solution

$algorithm1 = 40;
$algorithm2 = 20;
$fastAlgorithm = ($algorithm1 < $algorithm2) ? "Algorithm 1 is faster" : "Algorithm 2 is faster";
echo "$fastAlgorithm";


Output:
Algorithm 2 is faster




Increment/Decrement Operations

Program A. Calculate the factorial of a number using postfix increment/decrement operations. Display the factorial. (Calculate factorial: Use a loop to multiply numbers.)

Solution

$x = 4;
$factorial = 1;
for ($i = 1; $i <=$x; $i++){
$factorial *=$i;
} echo "$x! = $factorial";


Output:
4! = 24





Program B. Simulate the movement of a vehicle along a track using prefix increment/decrement operations. Display the final position of the vehicle. (Simulate vehicle movement: Use += for forward movement.)

Solution

$position = 0;
$end = 50;
$position += $end;
echo"Final position of the vehicle is: $position";


Output:
Final position of the vehicle is: 50




Logical Operations

Program A. Determine eligibility for a discount based on purchase amount and customer loyalty using logical operators. Display whether the customer is eligible for a discount. (Determine discount eligibility: Use && to check conditions.)

Solution

$amount = 290;
$membership = true;
$discount = ($amount > 200 && $membership) ? "Eligible for discount" : "Not eligible for discount";
echo "$discount";


Output:
Eligible for discount





Program B. Design a decision-making system for a chatbot to respond to user queries using logical operators. Display the appropriate response based on the query. (Chatbot decision-making: Use ? : for simple conditions.)

Solution

$query = "Good Morning";
$response = ($query == "Good Morning") ? "Good Morning" : "It's morning";
echo "$response";


Output:
Good Morning