Activity 3. PHP Operators


1. Arithmetic Operators

Problem 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 = 5;
$width = 9;
$area = $length * $width;
echo "A rectangle with length $length and width $width has an area of $area";

Output

A rectangle with length 5 and width 9 has an area of 45

Problem 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_rate;)

Solution

$php = 100;
$conversionRate = 0.028;
$converted = $php * $conversionRate;
echo "$php PHP is equivalent to $converted AUD";

Output

100 PHP is equivalent to 2.8 AUD

2. Assignment Operators

Problem 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 = 0;
$new = 69;
$total += $new;
echo "Total Donation: $total";

Output

Total Donation: 69

Problem 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

$c1Votes = 34;
$c2Votes = 25;
$p1 = $p2 = $p3 = $p4 = 1;
$c1Votes += $p1 + $p2 + $p4;
$c2Votes += $p3;
echo "
Candidate 1 Votes: $c1Votes
Candidate 2 Votes: $c2Votes
";

Output

Candidate 1 Votes: 37
Candidate 2 Votes: 26

3. Comparison Operators

Problem 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_t = 12;
$athlete2_t = 11;
$winner = ($athlete1_t < $athlete2_t) ? "Athlete 1" : "Athlete 2";
echo $winner . " is the winner";

Output

Athlete 2 is the winner

Problem 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

$alg1 = 1.31;
$alg2 = 1.55;
$faster = ($alg1 < $alg2) ? "Algorithm 1" : "Algorithm 2";
echo "The faster algorithm is $faster";

Output

The faster algorithm is Algorithm 1

4. Increment/Decrement Operators

Problem A

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

Solution

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

Output

5! = 120

Problem 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

$startPos = 0;
$travel = 99;
$startPos += $travel;
echo "Vehicle at final position $startPos";

Output

Final position of vehicle at 99

5. Logical Operators

Problem 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 = 301;
$loyalCustomer = true;
$eligible = ($amount > 300 && $loyalCustomer) ? "Eligible" : "Not eligible";
echo $eligible . " for discount";

Output

Eligible for discount

Problem 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

$userQuery = "moon phase";
$botResponse = ($userQuery == "moon phase") ? "Tonight is a full moon" : "I don't understand your question";
echo "ChatBot Response: $botResponse"

Output

ChatBot Response: Tonight is a full moon