Monday 19 January 2015

increment decrement in php

Hello

increment decrement in php

there 4 types

1)Pre-increment:-

++$my_information  means it can be Increments $my_information by one, then returns $my_information.

2)Post-increment

$my_information++ means it can be Returns $my_information, then increments $my_information by one.



3)Pre-decrement

--$my_information   means it can be Decrements $my_information by one, then returns $my_information.

4)Post-decrement

$my_information-- means it can be Returns $my_information, then decrements $my_information by one.

Try this code you can understand that.




<?php

echo "<h3>Postincrement</h3>";
$my_information = 5;
echo "Should be 5: " . $my_information++ . "<br />\n";
echo "Should be 6: " . $my_information . "<br />\n";

echo "<h3>Preincrement</h3>";
$my_information = 5;
echo "Should be 6: " . ++$my_information . "<br />\n";
echo "Should be 6: " . $my_information . "<br />\n";

echo "<h3>Postdecrement</h3>";
$my_information = 5;
echo "Should be 5: " . $my_information-- . "<br />\n";
echo "Should be 4: " . $my_information . "<br />\n";

echo "<h3>Predecrement</h3>";
$my_information = 5;
echo "Should be 4: " . --$my_information . "<br />\n";
echo "Should be 4: " . $my_information . "<br />\n";



?>

out put will be 


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



Try another example 


<?php
    $my_bat = 6;
    for ($i = 0; $i < 6; ++$i)
    {
        echo 'I have '.  $my_bat-- . " Bats. I just ate one.\n";}?
    
    >

?>

out out will be


I have 6 Bats.
I have 5 Bats.
I have 4 Bats.
I have 3 Bats.
I have 2 Bats.
I have 1 Bats.



No comments:

Post a Comment

Thank You For Comment