CopyPastehas never been so tasty!

Pre-increment, post-increment and negation operators in PHP

by anonymous

  • 0
  • 0
  • 0
85 views

Orignal Source:http://codingvilla.com/increment-negation-operators-php-single-article-587.aspx

 

Pre-increment

 

Pre-increment means that value is incremented first then assign to a variable and printed on the screen. As its name describe pre-increment.

 

Code:

 

 

<?php  

$var=2;

echo "Preincrement: ".(++$var);

echo "<BR>";

echo "Value after increment: ".$var;

echo "<BR>";

?>

 

Output:

 

Preincrement: 3
Value after increment: 3

 

Post-increment

 

Post-increment means that the value is assign to variable first and then incremented. As its name describe Post increment.

 

Code:

 

<?php

$var=2;

echo "Postincrement: ".($var++);

echo "<BR>";

echo "Value after drecrement: ".$var;

?>

 

Output:

 

Postincrement: 2
Value after drecrement: 3

 

Negation

 

Negation mean opposite. If -$var so its Negation is $var.

 

Code:

 

<?php

 

$var = false;

 

if ($var == false) {

    echo 'Not finisted till now';

}

 

if (!$var) {

   echo 'Not finisted till now';

}

?>

 

Output:

 

Not done yet!Not done yet!

 

$var is a variable having value false if the condition check $var is false then print false and if its negation is true so the negation of var is printed on!$var.

Add A Comment: