Home » PHP Tutorial » Learn about PHP Operator | Operator Examples – CodeMirch

Learn about PHP Operator | Operator Examples – CodeMirch

PHP Operator Example

We are sharing PHP Operator Example For Beginners to get easily understood

Learn : PHP Operator

Watch Our Php Operator Videos :

=========================

Watch This Link : https://youtu.be/kr8eXwfJJOg

==========================

What is an Operator in PHP ?

The Operator in PHP is used for operation some task on Variable and its value.

Here We are sharing basic PHP Operator Example in below-

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators

Arithmetic Operator Example :

These operators are mainly used for arithmetic Calculation on Variable and value.Lets See our Example –

Operator Sign :

+ = Addition

– = subtraction

*= Multiplication

/= Division

Example For + = Addition :

<?php

$value = 30 ;

$val  = 40;

$val3 = $value + $val;

Print $val3;

?>

Example For – = subtraction :

<?php

$value = 50 ;

$val  = 40;

$val3 = $value – $val;

Print $val3;

?>

Example For  * = Multiplication :

<?php

$value = 50 ;

$val  = 40;

$val3 = $value * $val;

Print $val3;

?>

Example For  /= Division :

<?php

$value = 50 ;

$val  = 2;

$val3 = $value / $val;

Print $val3;

?>

PHP Assignment operators Example :

This Operator is used for assigning variable’s value. Let’s Check Our Examples

= Assignment operators Example

<?php

$value = 50 ;

$val  = 2;

Echo $value;

?>

In this Section We assigned variable value respectively.

Increment/Decrement operators Example :

This operator is mainly used to increment or Decrement on a variable value.

Example of increment Variale Value in order to post and pre  increment :

Post Increment Operator Example :

<?php

 

$i = 2;

Echo $i++;

?>

Pre-Increment Operator Example:

<?php

$i= 4 ;

Echo ++$i;

?>

Example of Decrement Variale Value in order to post and pre Decrement :

Post Decrement Operator Example :

<?php

 

$i = 20;

Echo $i–;

?>

Pre-Decrement Operator Example:

<?php

$i= 40 ;

Echo –$i;

?>

PHP Comparison Operators Example :

This Operator is used for comparing the two or more than variable values. Lets Check Our Example

== Equal Sign 

Example Of Equal Sign :

<?php

$val = 20;

$val2 = 20;

If ($val == $val2)

Echo “The value is Equal “;

?>

===Identical Sign 

Example Of Identical Sign :

<?php

$val = 70;

$val2 = 70;

If ($val === $val2)

Echo “The values are Equal and they are same type”;

?>

!= Not Equal Sign 

Example of Not Equal Sign

<?php

$val = 70;

$val2 = 80;

If ($val != $val2)

Echo “The values are Not Equal”;

?>

!== Not identical Sign 

Example of Not identical Sign

<?php

$val = 80; // integer value

$val2 = “Test Value”; // String Value

If ($val !== $val2)

Echo “The values are Not Equal and they are not in same type”;

?>

(>) Greater than Sign 

Example of Greater than Sign

<?php

$val = 80;

$val2 = 70;

If ($val > $val2)

Echo “The val is greater than val2”;

?>

(<) Less than Sign 

Example of Less than Sign

<?php

$val = 80;

$val2 = 90;

If ($val < $val2)

Echo “The val is Less than val2”;

?>

(>=) Greater than or Equal to Sign 

Example of Greater than or Equal to Sign

<?php

$val = 120;

$val2 = 90;

If ($val >= $val2)

Echo “The val is greater than or Equal to val2”;

?>

(<=) Less than Or Equal to Sign 

Example of Less than Or Equal to Sign

<?php

$val = 60;

$val2 = 70;

If ($val <= $val2)

Echo “The val is Less than or Equal to val2”;

?>

PHP Logical operators Example 

This operator is used to logically testing variable value. There are SEVERAL  type of logical operator here

  1. AND OPERATOR &&
  2. OR OPERATOR . ||
  3. Xor OPERATOR
  4. NOT OPERATOR !

Example of AND (&&) Operator :

<?php

$user = “CodeMirch”;

$password = “1234”;

If ($user==”CodeMirch” && $password==”1234″) // compare two variable and Must be both true

Echo “User Registration Login Successful “;

Else

Echo “User Registration Login UnSuccessful because both are not true”;

?>

Example of OR (||) Operator :

<?php

$user = “CodeMirch”;

$password = “1234”;

If ($user==”CodeMirch” ||$password==”Test”) // compare two variable, EITHER one is must be true

Echo “User Registration Login Successful because one statement is true”;

Else

Echo “Both are not true”;

?>

Example of NOT (!) Operator :

<?php

 

$val =20;

If (! $val )

Echo “This is not the same value ,  Its OK”;

Else

Echo “Both are not true”;

?>

PHP String Operators Example :

This operator is used to Concatenation with two strings. Let’s See Example.

<?php

$test = “String One”;

$str = “String Two”;

Echo “$test.$str”;

?>

PHP Array Operator Example

This Operator is used to compare  array.

<?php

$arr = array (1,2,3,4);

If ($arr [2] ==3)

Echo “The indexing value is 3”;

?>

 

Watch Our Live Streaming, PHP Tutorial

PHP Operator Example :

https://youtu.be/kr8eXwfJJOg

 

, , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*