Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Problem incrementing / decrementing
Old 07-10-2008, 12:09 PM Problem incrementing / decrementing
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
Is this code:
PHP Code:
        for ($i 0$i $num$i ++)
            {
                if (
$portIdNav[$i] == $portId)
                    {
                        echo 
$portId ' is in index ' $i '<br>';
                        
                        
$portIdNext $portIdNav[$i 1];
                        
$portIdPrev $portIdNav[$i 1];
                        
                        echo 
'Next is ' $portIdNext ' and Prev is ' $portIdPrev;
                    }
            } 
equivalent to this?
PHP Code:
        for ($i 0$i $num$i ++)
            {
                if (
$portIdNav[$i] == $portId)
                    {
                        echo 
$portId ' is in index ' $i '<br>';
                        
                        
$portIdNext $portIdNav[$i ++];
                        
$portIdPrev $portIdNav[$i --];
                        
                        echo 
'Next is ' $portIdNext ' and Prev is ' $portIdPrev;
                    }
            } 
Ive always thought it was, but it gives me different results!

In case you cant spot the difference, its where the ++ and -- lie on the second code sample
__________________

Please login or register to view this content. Registration is FREE

Last edited by pealo86; 07-10-2008 at 12:10 PM..
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 07-10-2008, 12:18 PM Re: Problem incrementing / decrementing
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
http://uk3.php.net/manual/en/languag....increment.php - This should help you. I think your code should be
PHP Code:
        for ($i 0$i $num$i ++) 
            {
                if (
$portIdNav[$i] == $portId)
                    {
                        echo 
$portId ' is in index ' $i '<br>';
                        
                        
$portIdNext $portIdNav[++$i]; // We increase $i by 1 - so if $i was 4 it would be 5.
                        
$portIdPrev $portIdNav[--$i]; // We decrease $i by 1 - so if $i was 4 it would be 3.
                        
                        
echo 'Next is ' $portIdNext ' and Prev is ' $portIdPrev;
                    }
            } 

*Copies PHP.net's example*
PHP Code:
<?php
echo "<h3>Postincrement</h3>";
$a 5;
echo 
"Should be 5: " $a++ . "<br />\n";
echo 
"Should be 6: " $a "<br />\n";

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

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

echo 
"<h3>Predecrement</h3>";
$a 5;
echo 
"Should be 4: " . --$a "<br />\n";
echo 
"Should be 4: " $a "<br />\n";
?>
Would return (Browser side)

Code:
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
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 07-10-2008 at 12:26 PM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 07-10-2008, 12:25 PM Re: Problem incrementing / decrementing
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
Ohhhh yeah I totally forgot you could do that! Thanks.
__________________

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 07-10-2008, 12:30 PM Re: Problem incrementing / decrementing
Skilled Talker

Posts: 71
Trades: 0
PHP Code:
        for ($i 0$i $num$i ++)
            {
                if (
$portIdNav[$i] == $portId)
                    {
                        echo 
$portId ' is in index ' $i '<br>';
                        
                        
$portIdNext $portIdNav[$i];
                        
$portIdPrev $portIdNav[$i];
      
                        
$portIdNext $portIdNext++;
                        
$portIdPrev $portIdPrev--;
                        
                        echo 
'Next is ' $portIdNext ' and Prev is ' $portIdPrev;
                    }
            } 
This should work.
CrazeDizzleD is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Problem incrementing / decrementing
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.62161 seconds with 12 queries