Sunday, 27 July 2014

Filled Under:

Php operators

Operators

                            Like c++ in php use different type of operators
  • Arithmetic operators in php
  • String operators in php
  • Assignment operators in php
  • Increment and decrements operators in php
  • Comparison operators in php
  • Logical operator in php

1-Airthmetic Operator:

                                                    Arithmetic operator use in php that is +(plus),-(subtract),*(multiply),/(divide),%(modulus)
Operator
Name
Example
+
Plus
$a+$c
-
Subtract
$a-$c
*
Multiply
$a*$c
/
Divide
$a/$c
%
Modulus
$a%$c

Here  $a%$c mean it will return reminder.

Program:

<?php
$a=5;
$c=2;
echo $a+$c;
echo “<br/>”;
echo $a-$c;
echo “<br/>”;
echo $a*$c;
echo “<br/>”;
echo $a/$c;
echo “<br/>”;
echo $a%$c;
?>

Output:

                      7
                      3
                      10
                      2.5
                      1

2-String operators:

Operator
Name
explanation
.
Concatenation
It concatenate two or more strings
.=
Concatenation assignment
It concatenate and assign the value of one string to other


Program:

<?php
$str="hello php";
$str2="programminglogic";
echo $str . $str2;
echo “<br/>”;
$str .=$str2;
echo $str;
?>

Output:

         hello php programminglogic
         hello php programminglogic

3-Assignment operator

                                          Assignment operator use in php that is "="
here i made a simple program in which i concatenate two string then its value will be assign to other new variable.

Program:

<?php
$str="hello php";
$str2="programminglogic";
$str3=$str . $str2;        //assign value
echo $str3;
?>

Output:

         hello php programminglogic

4-Increment and decrement operators:

                                                                   like c++ we use same increment and decrement operator in php 
Operator
Name
Explanation
$a++
Post increment
First display value then increment
$++a
Pre-increment
It first increment then display the output
$a--
Post-decrement
First display the output then decrement
$--a
Pre-decrement
It first decrement then display the output

Program:

<?php
$s=5;
echo ++$s."<br/>";//concatenate <br/>tag for next line 
$s=5;
echo $s++."<br/>";
$s=5;
echo $s--."<br/>";
$s=5;
echo --$s."<br/>";

?>

Output:

                     6
                     5
                     5
                     4

5-comparison operator:

                                              
Operator
Name
Explanation
==
Equal
Return true if two value will be equal
===
Identical
Return true if two value will be equal but same data type.
!=
Not equal
Return true if two value will be not equal
!==
Not identical
Return true if two value will be not equal but not same data type.
<> 
Not equal
It alternate of !=
> 
Greater than
Return true if first value will be greater than second
< 
Less than
Return true if first value will be less than second
>=
Greater than or equal
Return true if first value will be greater or equal to the second
<=
Less than or equal
Return true if first value will be less than or equal second

its example will be discuss in if else statement

6-logical operators:

Operator
Name
Explanation
and /&&
And
Return true if all value will true
or  / ||
Or
Return false if all value will be false
  !
Not
Return true if false




0 comments:

Post a Comment