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

Wednesday, September 4, 2024

Assignments: Operators and Expressions

Class 3: Operators and Expressions


Assignment 1: Simple Arithmetic Operations

Code:

<?php

$a = 10;

$b = 5;

$sum = $a + $b;

$difference = $a - $b;

$product = $a * $b;

$quotient = $a / $b;

$remainder = $a % $b;

echo "Sum: $sum\n";

echo "Difference: $difference\n";

echo "Product: $product\n";

echo "Quotient: $quotient\n";

echo "Remainder: $remainder\n";

?>

 

 Explanation: This script demonstrates the use of basic arithmetic operators (+, -, *, /, %) to perform operations like addition, subtraction, multiplication, division, and modulus.


Assignment 2: Increment and Decrement Operators

Code:

<?php

$x = 10;

echo "Original Value: $x\n";

$x++;

echo "After Increment: $x\n";

$x--;

echo "After Decrement: $x\n";

?>

 

 Explanation: This script uses the increment (++) and decrement (--) operators to increase and decrease the value of a variable by 1.


Assignment 3: Compound Assignment Operators

Code:

<?php

$y = 15;

$y += 5;

echo "After Addition: $y\n";

 

$y -= 3;

echo "After Subtraction: $y\n";

 

$y *= 2;

echo "After Multiplication: $y\n";

 

$y /= 4;

echo "After Division: $y\n";

?>

 

 Explanation: This script demonstrates the use of compound assignment operators (+=, -=, *=, /=) to perform arithmetic operations and assign the result to the same variable.


Assignment 4: Comparison Operators

Code:

<?php


$a = 10;

$b = 20;

 

echo "Is a equal to b? " . var_export($a == $b, true) . "\n";

echo "Is a not equal to b? " . var_export($a != $b, true) . "\n";

echo "Is a greater than b? " . var_export($a > $b, true) . "\n";

echo "Is a less than b? " . var_export($a < $b, true) . "\n";

echo "Is a greater than or equal to b? " . var_export($a >= $b, true) . "\n";

echo "Is a less than or equal to b? " . var_export($a <= $b, true) . "\n";

?>

 

Step by step Explanation:

1. Variable Assignment:

$a = 10;

$b = 20;

Here, two variables are defined:

$a is assigned the value 10.

$b is assigned the value 20.

2. Comparison Operators:

The script then uses various comparison operators to compare the values of $a and $b.

a. Equality (==):

echo "Is a equal to b? " . var_export($a == $b, true) . "\n";

$a == $b: 

This checks if $a is equal to $b. Since $a (10) is not equal to $b (20), this will return false.

var_export($a == $b, true): 

var_export() is used to return the result (false) as a string ('false').

b. Not Equal (!=):

echo "Is a not equal to b? " . var_export($a != $b, true) . "\n";

$a != $b: 

This checks if $a is not equal to $b. Since 10 is not equal to 20, this will return true.

var_export($a != $b, true): 

The result (true) is output as 'true'.

c. Greater Than (>):

echo "Is a greater than b? " . var_export($a > $b, true) . "\n";

$a > $b:

 This checks if $a is greater than $b. Since 10 is not greater than 20, this will return false.

d. Less Than (<):

echo "Is a less than b? " . var_export($a < $b, true) . "\n";

$a < $b: This checks if $a is less than $b. Since 10 is less than 20, this will return true.

e. Greater Than or Equal To (>=):

echo "Is a greater than or equal to b? " . var_export($a >= $b, true) . "\n";

$a >= $b: This checks if $a is greater than or equal to $b. Since 10 is not greater than or equal to 20, this will return false.

f. Less Than or Equal To (<=):

echo "Is a less than or equal to b? " . var_export($a <= $b, true) . "\n";

$a <= $b: This checks if $a is less than or equal to $b. Since 10 is less than or equal to 20, this will return true.

Output:

The result of each comparison will be either true or false, and var_export() converts this result into a string for display.

Full Output:

Is a equal to b? false

Is a not equal to b? true

Is a greater than b? false

Is a less than b? true

Is a greater than or equal to b? false

Is a less than or equal to b? true

In summary, the script compares $a (10) and $b (20) using different operators and outputs whether each condition is true or false.


Assignment 5: Logical Operators (&&, ||, !)

Code:

<?php

$x = true;

$y = false;

echo "x AND y: " . var_export($x && $y, true) . "\n";

echo "x OR y: " . var_export($x || $y, true) . "\n";

echo "NOT x: " . var_export(!$x, true) . "\n";

?> 

Step by step Explanation:

1. Variable Assignment:

$x = true;

$y = false;

Here, two Boolean variables are defined:

$x is assigned the value true.

$y is assigned the value false.

2. Logical Operators:

a. Logical AND (&&):

echo "x AND y: " . var_export($x && $y, true) . "\n";

$x && $y: 

This checks if both $x and $y are true. The && operator (logical AND) returns true only if both operands are true. In this case, $x is true, but $y is false, so the result will be false.

var_export($x && $y, true): 

The result (false) is passed to var_export(), which converts it into a string representation ('false').

b. Logical OR (||):

echo "x OR y: " . var_export($x || $y, true) . "\n";

$x || $y: 

This checks if either $x or $y is true. The || operator (logical OR) returns true if at least one of the operands is true. In this case, since $x is true, the result will be true.

var_export($x || $y, true): 

The result (true) is passed to var_export(), which converts it into a string representation ('true').

c. Logical NOT (!):

echo "NOT x: " . var_export(!$x, true) . "\n";

!$x: 

This negates the value of $x. The ! (logical NOT) operator returns the opposite Boolean value. Since $x is true, !$x will return false.

var_export(!$x, true): 

The result (false) is passed to var_export(), which converts it into a string representation ('false').

Output:

The result of each logical operation will be either true or false, and var_export() converts these results into their string equivalents for display.

Full Output:

x AND y: false

x OR y: true

NOT x: false

Explanation of Each Operation:

x AND y ($x && $y): Both must be true for the result to be true. Since $x = true and $y = false, the result is false.

x OR y ($x || $y): At least one must be true for the result to be true. Since $x = true, the result is true.

NOT x (!$x): This negates the value of $x. Since $x = true, the result is false.

This script showcases basic logical operations in PHP with Boolean values.

Assignment 6: Checking Even or Odd Using the Modulus Operator

Code:

<?php

$number = 13;

if ($number % 2 == 0) {

    echo "$number is even.";

} else {

    echo "$number is odd.";

}

?>

 

 Explanation: This script checks whether a number is even or odd using the modulus (%) operator.


Assignment 7: Combining Comparison and Logical Operators

Code:

<?php

$age = 25;

$hasID = true;

 if ($age >= 18 && $hasID) {

    echo "You are allowed to enter.";

} else {

    echo "You are not allowed to enter.";

}

?>

 

Step by step Explanation:

1. Variable Assignment:

$age = 25;

$hasID = true;

Two variables are defined:

$age is assigned the value 25.

$hasID is assigned the Boolean value true, indicating that the person has an ID.

2. Conditional Statement (if-else):

The script checks whether the person meets certain conditions (being at least 18 years old and having an ID) to determine if they are allowed to enter.

a. The if Condition:

if ($age >= 18 && $hasID) {

    echo "You are allowed to enter.";

} else {

    echo "You are not allowed to enter.";

}

Condition: $age >= 18 && $hasID

$age >= 18: 

This checks if the person’s age is greater than or equal to 18. In this case, $age = 25, so the condition is true.

$hasID: 

This checks if the person has an ID. Since $hasID = true, this condition is also true.

Logical AND (&&): 

The && operator ensures that both conditions must be true for the whole expression to be true. Here, since both $age >= 18 and $hasID are true, the entire condition evaluates to true.

b. if Block:

Since the condition is true, the code inside the if block will be executed:

echo "You are allowed to enter.";

This will print the message: "You are allowed to enter."

c. else Block:

If the condition had been false, the code inside the else block would have executed:

echo "You are not allowed to enter.";

However, in this case, since the if condition is true, this part will not be executed.

3. Output:

Since both conditions (age >= 18 and hasID) are met, the script will output:

You are allowed to enter.

Summary:

The script checks if a person is at least 18 years old and has an ID.

If both conditions are true, the message "You are allowed to enter." is displayed.

If either condition is false, the message "You are not allowed to enter." would be shown instead.

Assignment 8: Using the Ternary Operator for Conditional Assignment

Code:

<?php

$age = 17;

$message = ($age >= 18) ? "Adult" : "Minor";

 echo $message;

?>

 

Step by step Explanation:

1. Variable Assignment:

$age = 17;

The variable $age is assigned the value 17, indicating the person's age.

2. Ternary Operator:

$message = ($age >= 18) ? "Adult" : "Minor";

Condition: $age >= 18

This checks if the person's age is greater than or equal to 18.

In this case, $age is 17, so the condition is false.

Ternary Syntax: (condition) ? value_if_true : value_if_false

If the condition ($age >= 18) is true, the expression returns "Adult".

If the condition is false, it returns "Minor".

Since $age = 17, the condition $age >= 18 evaluates to false, so the value "Minor" is assigned to $message.

3. Output:

echo $message;

The echo statement outputs the value of $message. Since the condition was false, $message contains "Minor".

The output will be:

Minor

Summary:

The script checks whether $age is 18 or older using the ternary operator.

If $age is 18 or more, it assigns "Adult" to $message.

If $age is less than 18, it assigns "Minor" to $message.

In this case, since $age = 17, the output is "Minor".

Assignment 9: Using Arithmetic Operations to Calculate Average

Code:

<?php

$marks = array(80, 70, 90, 85, 75);

$sum = 0;

 foreach ($marks as $mark) {

    $sum += $mark;

}

$average = $sum / count($marks);

echo "Average Marks: $average";

?>

 

 Explanation: This script calculates the average of an array of numbers using arithmetic operators.


Assignment 10: Using Assignment Operators with Strings

Code:

<?php

$text = "Hello";

$text .= " World!";

 echo $text;

?>

 

 Explanation: This script demonstrates the use of the concatenation assignment operator (.=) to append a string.


Assignment 11: Using && and || with Multiple Conditions

Code:

<?php

$temperature = 30;

$humidity = 60;

 if ($temperature > 25 && $humidity < 70) {

    echo "The weather is pleasant.";

} else {

    echo "The weather is uncomfortable.";

}

?>

 

 Explanation: This script evaluates multiple conditions using && and || logical operators.


Assignment 12: Determine Leap Year Using Comparison and Logical Operators

Code:

<?php

$year = 2024;

 if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {

    echo "$year is a leap year.";

} else {

    echo "$year is not a leap year.";

}

?>

 

 

Explanation: This script checks whether a year is a leap year using a combination of comparison and logical operators.


Assignment 13: Check if a Number is Between Two Values Using Comparison Operators

Code:

<?php

$number = 15;

if ($number >= 10 && $number <= 20) {

    echo "$number is between 10 and 20.";

} else {

    echo "$number is not between 10 and 20.";

}

?>

 

 Explanation: This script checks if a number is within a specified range using comparison operators.


Assignment 14: Logical NOT Operator to Invert a Condition

Code:

<?php

$isEnabled = true;

 if (!$isEnabled) {

    echo "The feature is disabled.";

} else {

    echo "The feature is enabled.";

}

?>

 

 Explanation: This script uses the logical NOT (!) operator to invert a boolean value.


Assignment 15: Nested Ternary Operator

Code:

<?php

$score = 85;

 $grade = ($score >= 90) ? "A" : (($score >= 80) ? "B" : "C");

 echo "Grade: $grade";

?>

 

 Explanation: This script uses a nested ternary operator to assign a grade based on a score.


Assignment 16: Check Multiple Conditions with Nested if Statements

Code:

<?php

$temperature = 20;

$rain = true;

 if ($temperature > 25) {

    echo "It's warm.";

} elseif ($temperature > 15 && !$rain) {

    echo "It's mild and dry.";

} else {

    echo "It's cool or rainy.";

}

?>

 

 Explanation: This script uses nested if statements to check multiple conditions and determine the output.


Assignment 17: Precedence of Operators

Code:

<?php

$result = 10 + 5 * 3;

echo "Result: $result";

?>

 

 Explanation: This script demonstrates the precedence of operators, where multiplication has a higher precedence than addition, so the multiplication is performed first.


Assignment 18: Logical Short-Circuiting

Code:

<?php

$a = false;

$b = true;

 if ($a && $b) {

    echo "Both are true.";

} else {

    echo "At least one is false.";

}

?>

 

 Explanation: This script demonstrates logical short-circuiting, where the second condition is not evaluated if the first condition in an && operation is false.


Assignment 19: Comparing Strings with == and ===

Code:

<?php

$str1 = "100";

$str2 = 100;

 echo "Using ==: " . var_export($str1 == $str2, true) . "\n";

echo "Using ===: " . var_export($str1 === $str2, true) . "\n";

?>

 

 Explanation: This script compares strings and integers using == (which checks value) and === (which checks both value and type).


Assignment 20: Using Bitwise Operators

Code:

<?php


$a = 5;  // 101 in binary

$b = 3;  // 011 in binary

$and = $a & $b;

?>

 

 

Assignment 20 (continued): Using Bitwise Operators

Code:

<?php


$a = 5;  // 101 in binary

$b = 3;  // 011 in binary

 $and = $a & $b;       // Bitwise AND

$or = $a | $b;        // Bitwise OR

$xor = $a ^ $b;       // Bitwise XOR

$not = ~$a;           // Bitwise NOT

 echo "Bitwise AND: $and\n"; // Result: 1 (001 in binary)

echo "Bitwise OR: $or\n";  // Result: 7 (111 in binary)

echo "Bitwise XOR: $xor\n"; // Result: 6 (110 in binary)

echo "Bitwise NOT: $not\n"; // Result: -6 (inverse of 101 is 010, negated to -6)

?>

 

 

Explanation: This script demonstrates the use of bitwise operators:


& (Bitwise AND): Performs a bitwise AND operation.

| (Bitwise OR): Performs a bitwise OR operation.

^ (Bitwise XOR): Performs a bitwise XOR operation.

~ (Bitwise NOT): Performs a bitwise NOT operation.


No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template