Thursday, 28 December 2017

What are Operators in PHP Like Most Programming Languages

On this instructional exercises we will find out what are Operators is all about, we use operators for assignment, Arithmetic, Comparison, Logical statement.

Assignment Operators Includes
1. = += -= /= *= %= ++ -- : unlike other programming languages.
2. .= - joining words operator.

for exmple

$text="This is a text";
$increment+=2;// increment by 2;
$decre-=1; decreament by 1;
$num1/=5; // the value of num1 divide by 5
$num2*=10; //the value of num2 multiplyby 10
$num3++; // auto increment by 1 at every run
$num3--; // auto decrement by 1 at every run

2. .= : joining words operator.

$string1.=' php world!'; // the space there matters a lot


Arithmetic Operators Includes
1. + - * / % - unlike other programming languages.
simply use for for calculation purpose

$result=$val1 + $val2; // add the value of 1 and 2
$result=$val1 - $val2; // subtract the value of 1 from 2
$result=$val1 * $val2; // add the value of 1 and 2
$result=$val1 / $val2; // divide the value of 1 from 2
$result=$val1%; // get the percentage the value of 1

Comparison Operators Includes
Is use to compare and contrast between two values, also can be use for decission making process, like if, else if, switch etc
1. == != < > <= >= : Unlike other programming languages. Even less than < and greater than > is thesame as !=.

for exmaple
1. if($val==$val2)
{
    print 'result';
}

2.  if($val!=$val2)
{
    print 'result';
}
Note: the exclamation sign ! means is not equal to.

3.  if($val < $val2) // less than
{
    print 'result';
}

4.  if($val > $val2) // greater than
{
    print 'result';
}

5.  if($val <= $val2) // less than or equal to, two condition are pased here.
{
    print 'result';
}

6.  if($val >= $val2) // greater than or equal to, two condition are also pased here.
{
    print 'answer';
}



2. === : this type is used to valid datatype and value if datatypes are thesame.
for example
$val=10;
$val2=9;
if($val===$val2)
{
    print 'Condition Passed';
}
else
{
    print 'The value '.$val.' is not equal to '.$val2;
}

3. !== - this other is the case, is also use thesame way but this time is not thesame datatype because of ! sign.

if($val!==$val2) // like the above example using ! sign you know what that means, is not equal
{
        print 'The value '.$val.' is not equal to '.$val2;
}
else
{
        print 'Condition Passed';
}

for exmaple
if($val==$val2)

Logical Operators Includes
As the name says logic

1. && || ! : Unlike other programming languages (&& and || short-form)
2. and or : like && and || but this || have lower precedence than &&

For Example
1. if($a==3 && $b=6)
{
    print 'All the condition are passed!';
}
or
if($a==3 AND $b=6)
{
    print 'All the condition are passed!';
}

OR STATEMENT
if($a==3 || $b=6)
{
    print 'One of the condition is passed!';
}

if($a==3 OR $b=6)
{
    print 'One of the condition is passed!';
}

No comments:

Post a Comment