Search
 
SCRIPT & CODE EXAMPLE
 

PHP

operators in php

<?php
  
  /* Operators are used to perform operations on variables and values
  PHP many types of operators:
  Arithmetic operators
  Assignment operators
  Comparison operators
  Increment/Decrement operators
  Logical operators
  String operators
  Array operators
  Conditional assignment operators
  */
  
  $x = 24;  
  $y = 3;
  
  #Arthimetic Operators
  
  #The PHP arithmetic operators are used with numeric values to perform common arithmetical operations.

  echo $x + $y . " this is addition <br>";
  echo $x - $y . " this is subtraction <br>";
  echo $x * $y . " this is multiplication <br>";
  echo $x / $y . " this is division <br>";
  echo $x % $y . " this is modulus <br>";
  echo $x ** $y . " this is exponentation <br>";

  #Assignment Operators

  echo $x = $y . " this is the same as x = y <br>";
  echo $x += $y . " this is the same as x = x + y <br>";
  echo $x -= $y . " this is the same as x = x - y <br>";
  echo $x *= $y . " this is the same as x = x * y <br>";
  echo $x /= $y . " this is the same as x = x / y <br>";
  echo $x %= $y . " this is the same as x = x % y <br>";
    
  #Comparison Operators
    
  echo $x == $y . " this returns true if x is equal to y <br>";
  echo $x === $y . " this returns true if x is equal to y and they are of the same type <br>";
  echo $x != $y . " this returns true if x is not equal to y <br>";
  echo $x <> $y . " this also returns true if x is not equal to y <br>";
  echo $x !== echo $x = $y . " this returns true if x is not equal to y, or they are not of the same type <br>";
  echo $x > $y . " this returns true if x is greater than y <br>";
  echo $x < $y . " this returns true if x is less than y <br>";
  echo $x >= $y . " this returns true if x is greater than or equal to y <br>";
  echo $x <= $y . " this returns true if x is less than or equal to y <br>";
  echo $x <=> . " this returns an integer less than, equal to, or greater than zero, depending on if x is less than, equal to, or greater than y. Introduced in PHP 7. <br>";
  
  #Increment/Decrement operators
    
  echo ++$x . " this increments x by 1 and then returns x <br>";
  echo $x++ . " this returns x and increments x by 1 <br>";
  echo --$x . " this decrements x by 1 and then returns x <br>";
  echo $x-- . " this returns x and decrements x by 1 <br>";
  
  #Logical Operators
    
  $a = $x >= $y
  $b = $x <= $y
  
  echo $a and $b . " this returns true if both, a and b are true <br>";
  echo $a or $b . " this returns true if either of them (or both) are true <br>";
  echo $a xor $b . " this returns true if either of them are true, but not both";
  echo $a && $b .  " this returns true if both, a and b are true <br>";
  echo $a || $b . " this returns true if either of them are true <br>";
  echo !$b . " this returns true if b is not true <br>";

  #String operators

  $text1 = "Sharp";
  $text2 = "Nails";
    
  echo $text1 . $text2 . " this adds text1 before text2 <br>";
  echo $text1 .= $text2 . " this appends text2 to text1"

  #Array operators
    
  $polygons = array("pentagon","hexagon","heptagon")
  $polygons1 = array("octagon","nonagon","decagon")
    
  print_r($polygons+$polygons1); #This appends polygons1 to polygons

  var_dump($polygons == $polygons1); #This returns true if polygons and polygons1 have the same key/value pairs in the same order and of the same types
  
  var_dump($polygons != $polygons1); #This returns true if polygons is not equal to polygons1
  var_dump($polygons <> $polygons1); #This returns true if polygons is not equal to polygons1

  var_dump($polygons !== $polygons1); #This returns true if polygons is not identical to polygons1
  
  #Conditional Assignment Operators

   echo $status = (empty($user)) ? "anonymous" : "logged in";
   echo(" when empty(user) is true, status is set to anonymous, when empty(user) is false, status is set to logged in<br>");

   $user = "Rick Astley";
   echo $status = (empty($user)) ? "anonymous" : "logged in";
   echo "<br>"
     
   echo $user = $_get["user"] ?? "anonymous";
   echo(" the value of user is _get['user'] if _get['user'] exists, and is not NULL. If _get['user'] does not exist, or is NULL, the value of user is anonymous.<br>");
  
?>
Comment

php != operator

$a == $b	Equal	TRUE if $a is equal to $b after type juggling.
$a === $b	Identical	TRUE if $a is equal to $b, and they are of the same type.
$a != $b	Not equal	TRUE if $a is not equal to $b after type juggling.
$a <> $b	Not equal	TRUE if $a is not equal to $b after type juggling.
$a !== $b	Not identical	TRUE if $a is not equal to $b, or they are not of the same type.
$a < $b	Less than	TRUE if $a is strictly less than $b.
$a > $b	Greater than	TRUE if $a is strictly greater than $b.
$a <= $b	Less than or equal to	TRUE if $a is less than or equal to $b.
$a >= $b	Greater than or equal to	TRUE if $a is greater than or equal to $b.
$a <=> $b	Spaceship	An integer less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. Available as of PHP 7.
Comment

php ?? operator

$x = expr1 ?? expr2	Null coalescing return expr1 if not null or available 
Comment

PREVIOUS NEXT
Code Example
Php :: php spreadsheet styles 
Php :: crypt password php 
Php :: Code to check Check whether a year is leap year or not 
Php :: if one condition 
Php :: optimize wordpress query 
Php :: Basic HTTP Authentication example 
Php :: create table laravel give name table 
Php :: php date time formatting 
Php :: laravel auth gurd for login user 
Php :: how to execute php in linux 
Php :: php integer variable 
Php :: laravel permission create role 
Php :: generate shortcode wordpress plugin 
Php :: route group laravel 8 
Php :: WooCommerce shop loop random array function not same values after each other 
Php :: In PackageManifest.php line 131: Undefined index: name 
Php :: php gd coordinate 
Php :: laravel remove public 
Php :: how to set default php version in ubuntu 
Php :: wordpress php 
Php :: Woofood Availability checker 
Php :: custom end-point request php-salesforce-rest-api 
Php :: What does this mean in PHP: - or = 
Php :: obtener tipo 
Php :: adding array not updating php 
Php :: shorthand to assign multiple variable to same value in php 
Php :: php executor 
Php :: php how to use namespaces 
Php :: laravel migration add contraint to other database 
Php :: phpmailer 5 string attachment 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =