Document

1. Arithmetic Operations

A. Write a PHP script to calculate the area of a rectangle using arithmetic operators.

Solution

$length = 10;
$width = 5;
$area = $length * $width;
echo "Area of rectangle: $area\n";

Output

Area of rectangle: 50

B. Implement a script that converts currency from one denomination to another using arithmetic operators.

Solution

$usd = 100;
$conversion_rate = 0.88;
$eur = $usd * $conversion_rate;
echo "Equivalent in EUR: $eur\n";

Output

Equivalent in EUR: 88

2. Assignment Operators

A. Track the progress of a fundraising campaign by updating the donation total dynamically using combined assignment operators.

Solution

$total_donation = 500;
$new_donation = 50;
$total_donation += $new_donation;
echo "Total donation: $total_donation\n";

Output

Total donation: 550

B. Implement a voting system where candidate votes are incremented based on user input.

Solution

// B. Voting system
$candidate1_votes = 100;
$candidate2_votes = 80;
$user_vote = 1;
$candidate1_votes += $user_vote;
echo "Candidate 1 votes: $candidate1_votes\n";

Output

Candidate 1 votes: 101

3. Comparison Operators

A. Compare the performance of two athletes in a race and determine the winner using comparison operators.

Solution

$athlete1_time = 9.81; $athlete2_time = 9.79; $winner = ($athlete1_time < $athlete2_time) ? "Athlete 1" : "Athlete 2";
echo "Winner: $winner\n";

Output

Winner: Athlete 2

B. Evaluate the efficiency of two algorithms based on their execution times using comparison operators.

Solution

$algorithm1_time = 20; $algorithm2_time = 15; $faster_algorithm = ($algorithm1_time < $algorithm2_time) ? "Algorithm 1" : "Algorithm 2";
echo "Faster algorithm: $faster_algorithm\n";

Output

Faster algorithm: Algorithm 2

4. Increment/Decrement Operations

A. Calculate the factorial of a number using postfix increment/decrement operations.

Solution

$n = 5;
$factorial = 1;
for ($i = 1; $i <= $n; $i++) {
$factorial *= $i;
}
echo "Factorial of $n: $factorial\n";

Output

Factorial of 5: 120

B. Simulate the movement of a vehicle along a track using prefix increment/decrement operations.

Solution

$position = 0;
$distance = 10;
$position += $distance;
echo "Final position of vehicle: $position\n";

Output

Final position of vehicle: 10

5. Logical Operators

A. Determine eligibility for a discount based on purchase amount and customer loyalty using logical operators.

Solution

// A. Determine discount eligibility
$purchase_amount = 150;
$loyalty_member = true;
$eligible_discount = ($purchase_amount > 100 && $loyalty_member) ? "Eligible" : "Not eligible";
echo "Discount eligibility: $eligible_discount\n";

Output

Discount eligibility: Eligible

B. Design a decision-making system for a chatbot to respond to user queries using logical operators.

Solution

$user_query = "weather";
$response = ($user_query == "weather") ? "Today's weather is sunny." : "I don't understand your
query.";
echo "Chatbot response: $response\n";

Output

Chatbot response: Today's weather is sunny.