Skip to content

Instantly share code, notes, and snippets.

@ahmed-sheta
Last active November 6, 2019 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmed-sheta/f6af7b8338fa0b048b6d8ddaddec95d8 to your computer and use it in GitHub Desktop.
Save ahmed-sheta/f6af7b8338fa0b048b6d8ddaddec95d8 to your computer and use it in GitHub Desktop.
PHP Operators
<?php
/*
Arthimetic Operators
Addition [ + ]
Subtraction [ - ]
Maltuiplication [ * ]
Division [ / ]
Modulus [ % ] The rest of Division (Baqi alqesma()
Exponentiation [ ** ] Exponent (in Arabic Al'os) needs php 5.6 or higher...... Ex: 2 ** 2 means in Arabic 2 Os 2 and the result will be 4 and 3 ** 3 means in Arabic 3 Os 3 and the result will be 27.
*/
/* $var1 = 1000;
$var2 = 2000;
echo $var1 + $var2; //3000
*/
echo "Arthimetic Operators" . "<br>";
echo "<hr />";
$var1 = 4500;
$var2 = 1000;
$var3 = $var1 - $var2;
$var4 = $var2 - $var1;
$var5 = 4;
$var6 = 5;
$var7 = 90;
$var8 = 3;
$var9 = 120;
$var10 = 7;
echo $var1 + $var2 . "<br/>"; //5500
echo $var3 . "<br/>"; //3500
echo $var4 . "<br/>"; // -3500
echo $var5 * $var6 . "<br/>"; //20
echo $var7 / $var8 . "<br/>"; //30
echo $var9 % $var10 . "<br/>"; //
// phpinfo(); //To show all info about the current PHP version
/*
Assignment Operators
(=) $var1 = 5; (=) is the Assignment operator
(+=) is Assignment Operator also.
(.=) is Assignment Operator also.
(-=) is Assignment Operator also.
(*=) is Assignment Operator also.
(/=) is Assignment Operator also.
*/
echo "<hr />";
echo "Assignment Operators" . "<br>";
echo "<hr />";
$var11 = 100;
$var11 += 200; // Result will be 300 .... This Ex is similar to $var11 = $var11 + 200;
$myName = "Ahmed"; // This EX is similar to $myName = "Ahmed" . " Sheta" . " VIROX"
$myName .= " Sheta";
$myName .= " VIROX";
$var12 = 200;
$var13 = 300;
$var12 += $var13;
$var14 = 500;
$var14 -= 300;
$var15 = 4;
$var15 *= 3;
$var15 .= " is the result.";
$var16 = 20;
$var16 /= 4;
echo $var11 . "<br>";
echo $myName . "<br>";
echo $myNamee = "Ahmed" . " Sheta" . " VIROX" . "<br>";
echo $var12 . "<br>";
echo $var14 . "<br>";
echo $var15 . "<br>";
echo $var16 . "<br>";
/*
Comparison Operators
[ == ] Equals ----> it will use to compare. It will check if the values are the same even if its data type are different. >>> $var1 = 100; $var2 = "100" so $var1 == $var2 becuase its values are the same.
[ != ] NOT Equals ----> it's Reverse of the [ == ] Equal Operator.
[ <> ] NOT Equals ----> the Same uses for [ != ] it's Reverse of the [ == ] Equal Operator.
[ === ] Identical ----> it will use to make sure the variables have the same Data Type and the Same Value.
[ !== ] NOT Identical ----> it's Reverse of the [ === ] Identical Operator.
[ < ] Less Than.
[ <= ] Less Than or Equal.
[ > ] Greather Than.
[ >= ] Greater Than or Equal.
*/
echo "<hr />";
echo "Comparison Operator";
echo "<hr />";
$a = 100;
$b = 500;
echo gettype($a) . "<br>" . gettype($b) . "<br>";
// echo $a == $b; // No Result because $a doesn't equal to $b, so it will be useful with if statement or loops.
if ($a == $b) { // if we uses (=) one equal only, it will mean Assignment, so $a = $b will return True every time.
echo "True" . "<br>";
} else {
echo "False" . "<br>";
}
if ($a === $b) { //a & b have the same data type (integer), but have different value, so a & b are NOT identical
echo "a & b have the Same Value, and Data type" . "<br>";
} else {
echo "a & b have NOT the Same Value, and Data type" . "<br>";
}
// another EX about [ === ] Identical Operator
$c = 500;
$d = "500";
echo "The Data Type for c is " . gettype($c) . "<br>" . "The Data Type for d is " . gettype($d) . "<br>";
if ($c === $d) {
echo "c & d are Identical and have The same Data Type and the Same Value" . "<br>";
} else {
echo "c & d are NOT Identical, because the Data Type and the Value for both are NOT the same, or at least one of them" . "<br>";
}
// Ex about [ != ] NOT Equals
$e = 100;
$f = 300;
if ($e != $f) {
echo "Yes, " . $e . " is NOT Equal to " . $f . "<br>";
} else {
echo "No, " . $e . " is Equal to " . $f . "<br>";
}
// Ex about [ !== ] NOT Identical
$g = 123;
$h = 147;
echo "The Data Type for g is " . gettype($c) . "<br>" . "The Data Type for h is " . gettype($d) . "<br>";
echo "Now we know the Data Type for \$g & \$h" . "<br>"; // Use (\) before Var Name to print it's name Not its Value.
if ($g !== $h) {
echo "Yes, " . $g . " is NOT Identical with " . $h . "<br>";
} else {
echo "No, " . $g . " is Identical with " . $h . "<br>";
}
// Ex about [ <> ] NOT Equals
$hi = 150;
$bye = 750;
if ($hi <> $bye) {
echo "True " . $hi . " is NOT Equal to " . $bye . "<br>";
} else {
echo "False " . $hi . " is Equal to " . $bye . "<br>";
}
// Ex about [ < ] Less Than Operator
$x = 550;
$y = 750;
if ($x < $y) {
echo "Yes " . $x . " is Less Than " . $y . "<br>";
} else {
echo "No " . $x . " is Greater Than " . $y . "<br>";
}
// Ex about [ <= ] Less Than or Equal Operator
$z = 513;
$k = 512;
if ($z <= $k) {
echo "Ooooh Yeah " . $z . " is Less Than or Equal to " . $k . "<br>";
} else {
echo "Ooops! No, " . $z . " is Greater Than " . $k . " and NOT Equal to it also." ."<br>";
}
// Ex about [ <= ] Less Than or Equal Operator
$t = 2048;
$r = 1024;
if ($t > $r) {
echo "Yes " . $t . " is Greater Than " . $r . "<br>";
} else {
echo "No " . $t . " is Less Than " . $r . "<br>";
}
// Ex about [ > ] Less Than or Equal Operator
$win = 2020;
$lose = 2019;
if ($win > $lose) {
echo "Yep, " . $win . " is Greater Than " . $lose . "<br>";
} else {
echo "Nope, " . $win . " is Less Than " . $lose . "<br>";
}
// Ex about [ >= ] Less Than or Equal Operator
$q = 2.25;
$w = 1.5;
echo "The Data Type for \$q is " . gettype($q) . ", and the Data Type for \$w is " . gettype($w) . "<br>";
if ($q >= $w) {
echo "WoW! Yes, " . $q . " is Greater than or Equal to " . $w . "<br>";
} else {
echo "OooOoOps! No, " . $q . " is NOT Greater than or Equal to " . $w . "<br>";
}
/*
Increment / Decrement Operators (Used with Num and String Only)
[ ++$var ] Pre Increment, It will increase the Value by 1 first, then Print it.
[ $var++ ] Post Increment, It will type the Value first, then increase it by 1 and Print it.
[ --$var ] Pre Decrement, It will decrease the Value by 1 first, then Print it.
[ $var-- ] Post Decrement, It will type the Value first, then decrease it by 1 and Print it.
*/
echo "<hr />";
echo "Increment / Decrement Operator";
echo "<hr />";
$incvar = 10;
echo ++$incvar . "<br>"; // 11 [ ++$var ] Pre Increment
echo ++$incvar . "<br>"; // 12 [ ++$var ] Pre Increment
echo ++$incvar . "<br>"; // 13 [ ++$var ] Pre Increment
echo ++$incvar . "<br>"; // 14 [ ++$var ] Pre Increment
$incvar2 = 20;
echo ".................................." . "<br>";
echo $incvar2++ . "<br>"; // 20 [ $var++ ] Post Increment
echo $incvar2++ . "<br>"; // 21 [ $var++ ] Post Increment
echo $incvar2++ . "<br>"; // 22 [ $var++ ] Post Increment
echo $incvar2++ . "<br>"; // 23 [ $var++ ] Post Increment
echo ".................................." . "<br>";
$decvar = 30;
echo --$decvar . "<br>"; // 29
echo --$decvar . "<br>"; // 28
echo --$decvar . "<br>"; // 27
echo --$decvar . "<br>"; // 26
echo --$decvar . "<br>"; // 25
$decvar2 = 50;
echo ".................................." . "<br>";
echo $decvar2-- . "<br>"; // 50
echo $decvar2-- . "<br>"; // 49
echo $decvar2-- . "<br>"; // 48
echo $decvar2-- . "<br>"; // 47
echo $decvar2-- . "<br>"; // 46
/*
Logica Operators
[ and ] All conditions must be TRUE.
[ && ] We must use it instead of (and). All Conditions must be TRUE.
[ xor ] One condition only must be TRUE and the others must be FALSE.
[ or ] At least one condition must be TRUE.
[ || ] Means (or) and we must use it instead of (or). At least one condition must be TRUE.
[ ! ] Means not. EX: if !(var=10)
*/
echo "<hr />";
echo "Logical Operators";
echo "<hr />";
$firname = "Ahmed";
$midname = "Sheta";
$lasname = "VIROX";
if ($firname == "Ahmed" && $midname == "Sheta" && $lasname == "VIROX") {
echo $firname . " " . $midname . " " . $lasname . "<br>";
}
$game1 = "Unreal";
$game2 = "Stronghold";
if ($game1 == "Red Alert" xor $game2 == "Stronghold") {
echo "Best PC Games for Ahmed Sheta" . "<br>";
} else {
echo "Ooooh, I need my best games!!!" . "<br>";
}
$game3 = "Red Alert";
$game4 = "Medal of Honor";
$game5 = "Angry Birds";
if ($game3 == "Red Alert" || $game4 =="Pocket Tanks" || $game5 == "Angry Birds") {
echo "Yes, Ahmed Sheta Love Those Games" . "<br>";
}
$game6 = "Call of Duty";
$game7 = "Mario";
$game8 = "Lego Racer";
if (!($game7 === $game8)) {
echo "Where is Mafia Game ??" . "<br>";
} else {
echo "Yes, I Love Mafia Game" . "<br>";
}
/*
Error Operator
[ @ ] This will ignore the Error message and will not display it, and also we can add a Custom Erro message.
*/
// $myfile = fopen("ahmed.txt" , "r"); // file is exist
// $myfile2 = @fopen("virox.txt", "r") or die("404! File Not Found. Help yourself and try again.");
/*
String Operators
[ . ] Concatenate 2 or more strings. For String Only
[ .= ]
*/
$conct = "Conct My friend";
$conct .= " - Good Morning";
echo $conct; //same as echo "Conct My friend" . " - Good Morning";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment