Monthly Archives: November 2010 - Page 2

PHP post and pre increment

Increment/decrement Operators
Example Name Effect
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
–$a Pre-decrement Decrements $a by one, then returns $a.
$a– Post-decrement Returns $a, then decrements $a by one.

Try this example:

<?php
echo “<h3>Postincrement</h3>”;
$a = 5;
echo “Should be 5: ” . $a++ . “<br />
“;
echo “Should be 6: ” . $a . “<br />
“;

echo “<h3>Preincrement</h3>”;
$a = 5;
echo “Should be 6: ” . ++$a . “<br />
“;
echo “Should be 6: ” . $a . “<br />
“;

echo “<h3>Postdecrement</h3>”;
$a = 5;
echo “Should be 5: ” . $a– . “<br />
“;
echo “Should be 4: ” . $a . “<br />
“;

echo “<h3>Predecrement</h3>”;
$a = 5;
echo “Should be 4: ” . –$a . “<br />
“;
echo “Should be 4: ” . $a . “<br />
“;
?>
Output:


Postincrement
Should be 5: 5
Should be 6: 6
Preincrement
Should be 6: 6
Should be 6: 6
Postdecrement
Should be 5: 5
Should be 4: 4
Predecrement
Should be 4: 4
Should be 4: 4

PHP-Logical Operator

Logical Operators
Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.`

Check out this example

<?php
$a=TRUE; //1
$b=FALSE;//null 0
Echo “A has:”.(bool)$a;
echo”B has:”.(bool)$b;
echo “A AND B has: “;
echo $a && $b;
echo “A OR B has: “;
echo $a || $b;
?>

Increment/Decrement operator in php

This may seem a bit confusing, but there is even a shorter shorthand for the common task of adding 1 or subtracting 1 from a variable. To add one to a variable or “increment” use the “++” operator:

* $x++; Which is equivalent to $x += 1; or $x = $x + 1;

To subtract 1 from a variable, or “decrement” use the “–” operator:

* $x–; Which is equivalent to $x -= 1; or $x = $x – 1;

Study this example:

<?php
$a = 5;
echo “<h2>A initially is “.$a.”.</h2>”;
echo “<h3>Postincrement</h3>”;
echo “Should be 5: ” . $a++ . “<br />
“;
echo “Should be 6: ” . $a . “<br />
“;
echo “<h3>Preincrement</h3>”;
$a = 5;
echo “A is reset to: “.$a. “<br />
“;
echo “Should be 6: ” . ++$a . “<br />
“;
echo “Should be 6: ” . $a . “<br />
“;
echo “<h3>Postdecrement</h3>”;
$a = 5;
echo “A is reset to: “.$a. “<br />
“;
echo “Should be 5: ” . $a– . “<br />
“;
echo “Should be 4: ” . $a . “<br />
“;
echo “<h3>Predecrement</h3>”;
$a = 5;
echo “A is reset to: “.$a. “<br />
“;
echo “Should be 4: ” . –$a . “<br />
“;
echo “Should be 4: ” . $a . “<br />
“;
?>

php-comparision operator

Comparisons are used to check the relationship between variables and/or values. Comparison operators are used inside conditional statements and evaluate to either true or false. Here are the most important comparison operators of PHP.

Operator Desc Example Result
== Equal To $x == $y false
!= Not Equal To $x != $y true
< Less Than $x < $y true
> Greater Than $x > $y false
<= Less Than or Equal To $x <= $y true
>= Greater Than or Equal To $x >= $y false

Check out this example:

<?php
$a=10;
$b=17;
/*
$a=17;
$b=10;
*/
echo  “<br>A is : “.$a;
echo  “<br>B is : “.$b;
echo “<br><br>”;
if($a==$b)
{
echo”<br>A equals B.”;//it wont be true
}
if($a != $b){
echo  “<br>A is not equals to B.”;//it will be true
}
if($a < $b){
echo  “<br>A is less than B;”;
}
if($a > $b){
echo  “<br>A is greater than B;”;
}
if($a <= $b){
echo  “<br>A is less than or equals to B;”;
}
if($a >= $b){
echo “<br>A is greater than or equals to B;”;
}
?>

php – operators

An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value (so that the construction itself becomes an expression).Operators are used to manipulate or perform operations on variables and values.

There are many operators used in PHP.

1. Comparison Operator

2. Increment/Decrement Operator

3. Logical Operator

4. Arithmatic Operator