PHP Operators

Loading concept...

๐ŸŽญ PHP Operators: The Magic Tools in Your Coding Toolbox

Imagine you have a magic toolbox. Inside are special tools that help you do math, compare things, and combine stuff together. In PHP, these tools are called operators. Letโ€™s open the toolbox and discover each one!


๐Ÿงฎ Arithmetic Operators: Your Math Friends

Think of arithmetic operators like the buttons on a calculator. They help you do math!

Operator What It Does Example
+ Adds numbers together 5 + 3 = 8
- Subtracts one number from another 10 - 4 = 6
* Multiplies numbers 4 * 3 = 12
/ Divides numbers 20 / 5 = 4
% Gives the remainder (modulus) 7 % 3 = 1
** Power (exponentiation) 2 ** 3 = 8

๐Ÿ• Pizza Example

$slices = 8;
$friends = 3;
$each = $slices / $friends;
$leftover = $slices % $friends;
// Each friend gets 2 slices
// 2 slices are left over!

Simple Story: You have 8 pizza slices and 3 friends. Division / tells you each friend gets 2 slices. The modulus % tells you 2 slices are left over. Magic!


๐Ÿ“ฆ Assignment Operators: Putting Things in Boxes

Assignment operators are like putting things into boxes. The = sign doesnโ€™t mean โ€œequalsโ€ like in math class. It means โ€œput this value into this box.โ€

Operator What It Does Same As
= Put value in box $x = 5
+= Add and put back $x = $x + 3
-= Subtract and put back $x = $x - 2
*= Multiply and put back $x = $x * 4
/= Divide and put back $x = $x / 2
%= Remainder and put back $x = $x % 3

๐ŸŽฎ Video Game Score Example

$score = 100;    // Start with 100
$score += 50;    // Got bonus! Now 150
$score -= 20;    // Hit obstacle! Now 130
$score *= 2;     // Double points! Now 260

Think of it this way: These shortcuts save you from writing the same variable twice. Instead of $score = $score + 50, just write $score += 50. Less typing, same result!


๐Ÿ”ผ๐Ÿ”ฝ Increment & Decrement Operators: Going Up and Down

These are the simplest operators! They add 1 or subtract 1 from a number.

Operator What It Does Example
++$x Add 1 first, then use it Pre-increment
$x++ Use it first, then add 1 Post-increment
--$x Subtract 1 first, then use it Pre-decrement
$x-- Use it first, then subtract 1 Post-decrement

๐ŸŽ‚ Birthday Candles Example

$candles = 5;
echo $candles++;  // Shows 5, then becomes 6
echo ++$candles;  // Becomes 7, then shows 7

The Secret:

  • When ++ is before the variable (++$x): Add first, use second
  • When ++ is after the variable ($x++): Use first, add second

โš–๏ธ Comparison Operators: The Judges

Comparison operators are like judges at a contest. They compare two things and answer with true or false.

Operator What It Asks Example
== Are they equal? 5 == "5" โ†’ true
=== Same value AND type? 5 === "5" โ†’ false
!= Are they different? 5 != 3 โ†’ true
!== Different value OR type? 5 !== "5" โ†’ true
> Is left bigger? 10 > 5 โ†’ true
< Is left smaller? 3 < 8 โ†’ true
>= Bigger or equal? 5 >= 5 โ†’ true
<= Smaller or equal? 4 <= 6 โ†’ true
<=> Spaceship! Compare all Returns -1, 0, or 1

๐ŸŽ๐ŸŠ Fruit Comparison Example

$apples = 5;
$oranges = "5";

$apples == $oranges;   // true (same value)
$apples === $oranges;  // false (different types!)

The Spaceship Operator <=> is special:

  • Returns -1 if left is smaller
  • Returns 0 if theyโ€™re equal
  • Returns 1 if left is bigger
1 <=> 2;  // Returns -1
2 <=> 2;  // Returns 0
3 <=> 2;  // Returns 1

๐Ÿง  Logical Operators: The Decision Makers

Logical operators help you make decisions with AND, OR, and NOT. Theyโ€™re like asking questions!

Operator What It Means Example
&& or and Both must be true true && true โ†’ true
|| or or At least one true true || false โ†’ true
! Flip it! (NOT) !true โ†’ false
xor Only one can be true true xor false โ†’ true

๐ŸŽข Theme Park Example

$tall_enough = true;
$old_enough = true;
$has_ticket = false;

// Can ride the coaster?
$can_ride = $tall_enough && $old_enough;
// true - passed both checks!

// Can enter park?
$can_enter = $has_ticket || $is_member;
// Need at least one to be true

Remember:

  • && (AND) = Both must be true
  • || (OR) = At least one must be true
  • ! (NOT) = Flips true to false, false to true

๐ŸŽฏ Operator Precedence: Who Goes First?

Just like in math class, some operations happen before others. This is called precedence.

graph TD A["๐Ÿฅ‡ Highest Priority"] --> B["++ -- #40;increment/decrement#41;"] B --> C["! #40;not#41;"] C --> D["* / % #40;multiply/divide#41;"] D --> E["+ - #40;add/subtract#41;"] E --> F["< > <= >= #40;comparison#41;"] F --> G["== != === !== #40;equality#41;"] G --> H["&& #40;and#41;"] H --> I["|| #40;or#41;"] I --> J["= += -= #40;assignment#41;"] J --> K["๐Ÿฅ‰ Lowest Priority"]

๐Ÿงฎ The Math Problem

$result = 2 + 3 * 4;
// Answer: 14 (not 20!)
// Multiplication happens first: 3 * 4 = 12
// Then addition: 2 + 12 = 14

๐Ÿ’ก Pro Tip: Use Parentheses!

When in doubt, use () to make your intentions clear:

$result = (2 + 3) * 4;  // Now it's 20!

๐Ÿ”ค String Operators: Gluing Words Together

String operators help you combine text (called concatenation).

Operator What It Does Example
. Joins strings together "Hello" . " World"
.= Add to existing string $text .= "more"

๐ŸŽจ Name Badge Example

$first = "John";
$last = "Doe";
$full = $first . " " . $last;
// Result: "John Doe"

$greeting = "Hello, ";
$greeting .= $full;
// Result: "Hello, John Doe"

Think of . as glue that sticks words together!


๐Ÿ“š Array Operators: Working with Lists

Arrays are like lists of items. Array operators help you combine and compare these lists.

Operator What It Does Example
+ Combine arrays (union) $a + $b
== Same values? $a == $b
=== Same values, order, types? $a === $b
!= Different values? $a != $b
!== Different in any way? $a !== $b

๐ŸŽ’ Backpack Example

$backpack1 = ["book", "pencil"];
$backpack2 = ["apple", "water"];

$combined = $backpack1 + $backpack2;
// Result: ["book", "pencil", "apple", "water"]

โš ๏ธ Important Note About +

The + operator for arrays keeps the first arrayโ€™s values if keys match:

$a = [0 => "red", 1 => "green"];
$b = [0 => "blue", 2 => "yellow"];

$c = $a + $b;
// Result: [0 => "red", 1 => "green", 2 => "yellow"]
// "blue" is ignored because key 0 already exists!

๐ŸŽฎ Quick Reference Card

Category Operators Remember As
Arithmetic + - * / % ** Calculator buttons
Assignment = += -= *= /= %= Putting in boxes
Increment ++ -- Up and down arrows
Comparison == === != !== > < >= <= <=> The judges
Logical && || ! and or xor Decision makers
String . .= Word glue
Array + == === != !== List managers

๐Ÿš€ Youโ€™re Now an Operator Expert!

Youโ€™ve discovered all the magic tools in your PHP toolbox:

  1. โœ… Arithmetic - Do math like a calculator
  2. โœ… Assignment - Store values in variables
  3. โœ… Increment/Decrement - Quick +1 or -1
  4. โœ… Comparison - Judge and compare values
  5. โœ… Logical - Make smart decisions
  6. โœ… Precedence - Know who goes first
  7. โœ… String - Glue text together
  8. โœ… Array - Manage your lists

Now go build something amazing! ๐ŸŽ‰

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.