TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-15-2009, 08:06 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default Increment, decrement....

Hey guys,

Got a newbie and embarrassing question....

I thought that the ++ would add 1 to a number? Like so:

Code:
$test = 1;

$test2 = $test++;

echo "OUTPUT: $test2";
The output is still 1. I thought that the output should be 2?

Is the only way to use the ++ is in loops?

I thought that the ++ was a quick way to add "1" without having to do this:

$test2 = ($test + 1);
allworknoplay is offline  
Reply With Quote
Old 04-15-2009, 09:02 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

You meant this:

Code:
$a = 1;

$b = ++$a;

echo $b; // 2

//----------------//
// ...or... 
//----------------//
$a = 1;

$a++;

$b = $a;

echo $b; // 2
$var++ first returns the value of the variable, and then increments it, whereas ++$var first increments the variable, and then returns its value.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 04-15-2009, 09:06 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

For that use, you would need to use a pre-increment rather than post-increment operator. With your original code, using a post-increment, the expression $test++ evaluates to 1 (this is used for the assignment) then the value is incremented. With a pre-increment, the expression ++$test evaluates to 2 which is used in the assignment to $test2

Full details: http://php.net/operators.increment

P.S. xenon beat me to it, both posts say the same thing.
Salathe is offline  
Reply With Quote
Old 04-15-2009, 10:10 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

darn!!! Yes you guys are right!!

I forgot about doing ++$test...

You know sometimes you are so used to doing: $i++ in your loops you forget
you can do it the other way too!!

Thanks guys!!
allworknoplay is offline  
Reply With Quote
Old 04-16-2009, 11:12 AM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Hehe. I know what you mean!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Never ending increment? Orc General 22 02-15-2008 10:30 AM


All times are GMT. The time now is 11:59 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design