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

Friday, August 23, 2024

Lecture Notes 3: Operators in PHP

 

Operations in PHP

1. Arithmetic Operations

Arithmetic operations are used to perform mathematical calculations on variables.

Basic Arithmetic Operators

Addition (+): Adds two numbers.

$a = 10;
$b = 20;
$result = $a + $b;  // Result: 30
Subtraction (-): Subtracts one number from another.

$result = $b - $a;  // Result: 10
Multiplication (*): Multiplies two numbers.

$result = $a * $b;  // Result: 200
Division (/): Divides one number by another.

$result = $b / $a;  // Result: 2
Modulus (%): Returns the remainder of the division of one number by another.

$result = $b % $a;  // Result: 0 (since 20 divided by 10 leaves no remainder)
Exponentiation (**): Raises one number to the power of another.

$result = $a ** 2;  // Result: 100 (10 raised to the power of 2)
Example:
 Arithmetic Operations

<?php

    $x = 15;

    $y = 4;

    

    echo "Addition: " . ($x + $y);         // Output: 19

    echo " Subtraction: " . ($x - $y);     // Output: 11

    echo " Multiplication: " . ($x * $y);  // Output: 60

    echo " Division: " . ($x / $y);        // Output: 3.75

    echo " Modulus: " . ($x % $y);         // Output: 3

?>

 


Output:

2. Assignment Operations
Assignment operations assign values to variables.

Basic Assignment Operators

Assignment (=): 
Assigns the value on the right to the variable on the left.
$x = 10;  // $x now holds the value 10
Compound Assignment Operators
Addition Assignment (+=): 
Adds the value on the right to the current value of the variable and assigns the result to the variable.

$x += 5;  // $x = $x + 5; 
Result: 15
Subtraction Assignment (-=): 
Subtracts the value on the right from the current value of the variable and assigns the result to the variable.

$x -= 3;  // $x = $x - 3;
Result: 7
Multiplication Assignment (*=): 
Multiplies the variable by the value on the right and assigns the result to the variable.

$x *= 2;  // $x = $x * 2; 
Result: 20
Division Assignment (/=): Divides the variable by the value on the right and assigns the result to the variable.

$x /= 5;  // $x = $x / 5; 
Result: 2
Modulus Assignment (%=): 
Takes the modulus of the variable by the value on the right and assigns the result to the variable.

$x %= 3;  // $x = $x % 3; 
Result: 1
Example: Assignment Operations

<?php

    $x = 10;

    $x += 5;   // Now $x is 15

    echo "New Value: " . $x;

?>

 

Output:


3. Comparison Operations

Comparison operations compare two variables and return either true or false.

Comparison Operators
Equal (==): 
Checks if two values are equal.

$x == $y;
Identical (===): 
Checks if two values are equal and of the same type.

$x === $y;
Not Equal (!= or <>): 
Checks if two values are not equal.

$x != $y;
Not Identical (!==): 
Checks if two values are not equal or not of the same type.

$x !== $y;
Greater Than (>): 
Checks if the value on the left is greater than the value on the right.

$x > $y;
Less Than (<): 
Checks if the value on the left is less than the value on the right.

$x < $y;
Greater Than or Equal To (>=): 
Checks if the value on the left is greater than or equal to the value on the right.

$x >= $y;
Less Than or Equal To (<=): 
Checks if the value on the left is less than or equal to the value on the right.

$x <= $y;
Example: 
Comparison Operations

 <?php

    $a = 5;

    $b = 10;

    

    var_dump($a == $b);   // Output: bool(false)

    var_dump($a != $b);   // Output: bool(true)

    var_dump($a < $b);    // Output: bool(true)

?>

 

Output:



4. Logical Operations

Logical operations are used to combine comparison operations and control program flow.

Logical Operators
AND (&&): 
Returns true if both conditions are true.

$x && $y;
OR (||): 
Returns true if at least one of the conditions is true.

$x || $y;
NOT (!): 
Reverses the value of a condition. If true, returns false; if false, returns true.

!$x;

Example: Logical Operations

<?php

    $a = 5;

    $b = 10;

    

    if ($a < $b && $b > 0) {

        echo "Both conditions are true";

    }

?>

 



Output:

5. Increment/Decrement Operations
These operations are used to increase or decrease the value of a variable by 1.

Increment Operators
Post-increment ($x++): 
Increases the variable's value by 1 after the operation.

$x++;
Pre-increment (++$x): 
Increases the variable's value by 1 before the operation.


++$x;
Decrement Operators
Post-decrement ($x--):
 Decreases the variable's value by 1 after the operation.

$x--;
Pre-decrement (--$x):
 Decreases the variable's value by 1 before the operation.

--$x;
Example: 
Increment/Decrement Operations

<?<?php

    $x = 10;

    echo $x++;   // Output: 10 (then $x becomes 11)

    echo ++$x;   // Output: 12

?>

 


Output:

6. String Operations
String operations allow you to manipulate and work with strings in PHP.

Concatenation (.)
Concatenates (joins) two strings together.

$first_name = "John";
$last_name = "Doe";
$full_name = $first_name . " " . $last_name;  // Result: "John Doe"
Concatenation Assignment (.=)
Appends the right-hand string to the left-hand string and assigns the result to the left-hand variable.

$text = "Hello";
$text .= " World";  // Result: "Hello World"
Example: String Operations

<?php

    $greeting = "Hello";

    $greeting .= ", World!";  // Append to string

    echo $greeting;  // Output: Hello, World!

?>

 



Output:


Conclusion
By understanding and practicing these variable operations in PHP, you will gain the ability to manipulate data effectively in your PHP scripts.

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template