Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Operator Concept in PHP:-
..............................................................................



It is used to perform operations using operand, Operand can be variable, constant, and literals.

literal means value of variable or constant. for example, 1 is literals, "hello" is String literals.


Type of Operator:-

1) Unary:-    This operator will work using a single operand.

1.1 Increment:-      $a++    increase by one

1.1.1  Post increment:-   first complete other operation then increase value.
          $a++;

         Example of Post Increment:-
         <?php
             $a=2;
            $b=$a++;  //b=2,a=3
            echo $a,"<br>",$b;
          ?>
   
1.1.2  Pre Increment:-     first increase then perform other operations.

         ++$a;

1.2  Decrement:-   $a--      decrease by one
   
Example of Increment, Decrement:-

<?php

$a=5;
$a++;
echo $a,"<br>";
$a--;
echo $a;

?>


Another Example of Increment, Decrement:-

<?php

$a=5;
$b=5;
$a++;  
++$b;
echo $a++ ," ", ++$b,"<br>";

?>


2) Binary:-   It will use minimum two operand to perform operation .

2.1) Arithmetic :-  +,-,*,/, %

2.2)  Conditional or Relational :-   <, >,  <=,  >=    (it will use in if--else or loop)

2.3)  Comparison  :-

         ==      it will compare only data, datatype can be different

         if(2=="2")   //true
    
        ===   it will compare data and datatype both
       if(2==="2")   //false

2.4)  Logical Operator:-

       &&       it will return when all condition will be true

       ||            it will return when the particular condition will be true

      !             it is the opposite of true condition and returns a false value.

2.5) Assignment operator:-

2.5.1)   Simple Assignment    =    ($a=2 )
2.5.2)   Complex Assignment      $a=$a+5  ,  $a+=5
                                                      $a=$a*3,   $a*=3

                                                     $a=$a/4       $a/=4

<?php
$a=5;      //5
$a=$a+2;   //7
echo $a;
$a=$a*2;   //14
echo $a;
$a=$a%2;  
echo $a;  //0

?> 

3) Ternary:-

It is used to solve the condition-based problems using a ternary statement. ternary provide a single-line statement to implement code.

var = (condition)?true:false;

WAP to check even numbers or odd numbers?

<?php
$num=4;
echo $num%2==0?"even":"odd";



?>


<?php
$num=4;
$x=$num%2==0?"even":"odd";
echo $x;


?>


Assignment:-

WAP to calculate electricity bill where unit price and total consumption will be assigned by the user?

WAP to reverse the five-digit number?

12345   res  54321

<?php
$num = 78912;

$a = $num%10;  //5
$num = (int)($num/10); //1234.5
$b = $num%10;  //4
$num = (int)($num/10);  //123
$c = $num%10; //3
$num = (int)($num/10);  //12
$d = $num%10;  //2
$e = (int)($num/10);  //1
$rev = $a*10000+$b*1000+$c*100+$d*10+$e*1;
print($rev);

?>





 WAP to perform the addition of complex numbers?

2+3i
4+5i
.................
6+8i

<?php

$r1=2;
$i1=4;
$r2=7;
$i2=5;
$r = $r1+$r2;
$i = $i1+$i2;

echo $r1."+".$i1."i"."<br>";
echo $r2."+".$i2."i"."<br>";
echo $r."+".$i."i";

?>


Another Solution:-

<?php

$r1=2;
$i1=4;
$r2=7;
$i2=5;
$r = $r1+$r2;
$i = $i1+$i2;

echo "$r1 + $i1 i <br>";
echo "$r2 + $i2 i <br>";
echo "$r + $i i <br>";

?>



WAP to calculate the salary of the employee where basic, ta, da, comm, pf,hra, and leave will be assigned by the users?







Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post