Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Operators in PHP

Operators in PHP

Operators in PHP

Operators in PHP are symbols or keywords used to perform operations on variables and values. PHP supports a wide range of operators, which can be classified into several types:


1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.

OperatorDescriptionExample
+Addition$a + $b
-Subtraction$a - $b
*Multiplication$a * $b
/Division$a / $b
%Modulo (Remainder)$a % $b
**Exponentiation$a ** $b (PHP 5.6+)

Examples:

<?php$a = 10;$b = 5;echo $a + $b;  // Output: 15echo $a - $b;  // Output: 5echo $a * $b;  // Output: 50echo $a / $b;  // Output: 2echo $a % $b;  // Output: 0 (remainder of division)echo $a ** $b; // Output: 100000 (10 raised to the power of 5)?>

2. Assignment Operators

Assignment operators are used to assign values to variables. PHP supports several assignment operators:

OperatorDescriptionExample
=Assign value to variable$a = 10
+=Add and assign$a += 5
-=Subtract and assign$a -= 3
*=Multiply and assign$a *= 2
/=Divide and assign$a /= 4
%=Modulo and assign$a %= 2
.= Concatenate and assign string$a .= "b"

Examples:

<?php$a = 10;$a += 5;   // $a = $a + 5;  $a is now 15$a -= 3;   // $a = $a - 3;  $a is now 12$a *= 2;   // $a = $a * 2;  $a is now 24$a /= 4;   // $a = $a / 4;  $a is now 6$a .= "b"; // $a = "6b";     Concatenates "b" to $aecho $a;   // Output: 6b?>

3. Comparison Operators

Comparison operators are used to compare two values. The result of a comparison is either true or false.

OperatorDescriptionExample
==Equal to$a == $b
!=Not equal to$a != $b
===Identical (equal and same type)$a === $b
!==Not identical (not equal or different type)$a !== $b
>Greater than$a > $b
<Less than$a < $b
>=Greater than or equal to$a >= $b
<=Less than or equal to$a <= $b

Examples:

<?php$a = 5;$b = 10;echo ($a == $b);   // Output: false (because 5 is not equal to 10)echo ($a != $b);   // Output: true (because 5 is not equal to 10)echo ($a === 5);   // Output: true (because both value and type are the same)echo ($a < $b);    // Output: true (because 5 is less than 10)?>

4. Increment/Decrement Operators

Increment and decrement operators are used to increase or decrease the value of a variable by 1.

OperatorDescriptionExample
++Increment$a++ or ++$a
--Decrement$a-- or --$a

Examples:

<?php$a = 5;echo ++$a;  // Output: 6 (pre-increment: increments before use)echo $a++;  // Output: 6 (post-increment: increments after use)echo $a;    // Output: 7 (since $a was incremented after echo)?>

5. Logical Operators

Logical operators are used to combine multiple conditions in if statements or loops.

OperatorDescriptionExample
&&And (both conditions true)$a && $b
``
!Not (negation)!$a

Examples:

<?php$a = true;$b = false;var_dump($a && $b); // Output: false (both must be true)var_dump($a || $b); // Output: true (only one must be true)var_dump(!$a);      // Output: false (negation of true is false)?>

6. String Operators

String operators are used to perform operations on string variables.

OperatorDescriptionExample
.Concatenate (combine) strings$a . $b
.=Concatenate and assign$a .= $b

Examples:

<?php$a = "Hello";$b = " World";echo $a . $b;   // Output: Hello World$a .= $b;       // Concatenate $b to $aecho $a;        // Output: Hello World?>

7. Array Operators

Array operators are used to compare arrays or combine them.

OperatorDescriptionExample
+Union of arrays (no duplicate values)$a + $b
==Equal (arrays have same key/value pairs)$a == $b
===Identical (same key/value pairs and order)$a === $b
!=Not equal (arrays are not equal)$a != $b
!==Not identical (arrays differ in values or order)$a !== $b

Examples:

<?php$a = array("a" => "apple", "b" => "banana");$b = array("b" => "banana", "c" => "cherry");print_r($a + $b);  // Output: Array ( [a] => apple [b] => banana [c] => cherry )var_dump($a == $b); // Output: false (arrays are not equal)var_dump($a === $b); // Output: false (arrays are not identical)?>

8. Conditional (Ternary) Operator

The ternary operator is a shorthand for if-else statements.

OperatorDescriptionExample
?:Ternary (if-else shorthand)condition ? expr1 : expr2

Example:

<?php$a = 5;$b = 10;echo ($a > $b) ? "a is greater" : "b is greater";  // Output: b is greater?>

9. Null Coalescing Operator

The null coalescing operator ?? is used to check if a variable is set and is not null. If it is null, it returns the second value.

OperatorDescriptionExample
??Null coalescing$a ?? $b

Example:

<?php$a = null;$b = "Hello";echo $a ?? $b;  // Output: Hello (since $a is null, it returns $b)?>

Summary

PHP offers a wide range of operators for working with numbers, strings, arrays, and logical conditions. These operators allow you to perform arithmetic calculations, compare values, combine strings, manipulate arrays, and more.

Let me know if you'd like more detailed examples or if you have any specific questions about operators!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql