Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Basics Unit Converter Creating a Unit Conversion Tool

5 Answers

Benjamin Verspeak
Benjamin Verspeak
10,940 Points

From what I understand, When you run ($a++) it returns $a first and if you were to then var_dump($a) afterwards, it would now be 6.

So: <?php $a = 5; var_dump($a++); var_dump($a); ?>

will return: int(5) int(6)

Raphaël Seguin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 Points

Let's look at sequentially (the way the computer sees it) :

  • first : $a = 5;
    You assign the value 5 to the variable $a.
  • then : var_dump($a++); the var_dump() function displays the values of the arguments you pass it to. In our case, the argument is $a++. The expression $a++ does two things, in this order (the order is important) :
  • it returns the variable $a
  • then it increments the value of $a by one. So, since $a = 5, $a++ returns 5 and then var_dump($a++) returns 5. After that, the value of $a is incremented. You could easily check that if you wrote : var_dump($a++); var_dump($a); You'd see it doesn't show the same value the second time, because $a has been incremented after the first var_dump function was called but before the second one was called.

Does it help you to understand what's happening ?

cheers,

Raphaël

Raphaël Seguin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 Points

Hi, a++ returns the value of a, and then adds one to the value of a. ++a adds one to the value of a, and then returns the value of a.

cheers,

raphaël

Micah Doulos
Micah Doulos
17,663 Points

$a = 5; var_dump($a++);

=

$a = 5; Display the value of $a and afterwards add 1 (new value not displayed).

with var_dump($a++) you first dump the $a variable and after showing that variable it adds 1 to $a. with var_dump(++$a) it will first add 1 to $a and show it.