# 23 nothing is so as it seems, but why

- Thread: https://externals.io/message/21578
- Original archive: https://news-web.php.net/php.internals/21578
- Started by Marco Kaiser on 2006-01-19T16:41:17+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #21578 — Marco Kaiser — 2006-01-19T16:41:17+00:00

URL: https://externals.io/message/21578#21578

Today during a session i had a strange "magic" feature found in php.

<?php
$a = 10;
echo ++$a + $a++;
?>

this works perfect as expected. it returns 22.
but if i assign $a as a ref. to another variable it will be return 23.

<?
$a = 10;
$b = &$a;
echo ++$a + $a++;
?>

this will gave me to 23
(used the $b to the pre post stuff)

<?
$a = 10;
$b = &$a;
echo ++$b + $b++;
?>

this just happens if i have a ref. count to my var. WHY? :)

http://en.wikipedia.org/wiki/23_%28film%29

--
Marco Kaiser

---

## #21579 — Hartmut Holzgraefe — 2006-01-19T16:48:11+00:00

URL: https://externals.io/message/21578#21579 · In reply to: #21578

Marco Kaiser wrote:
> Today during a session i had a strange "magic" feature found in php.
> 
> <?php
> $a = 10;
> echo ++$a + $a++;
> ?>

there is a very simple rule that PHP inherited from C:

   never use pre- and postfix increment operators in the same expression

as the results are undefined

-- 
Hartmut Holzgraefe, Senior Support Engineer                            .
MySQL AB, www.mysql.com

---

## #21581 — Christian Schneider — 2006-01-19T17:04:03+00:00

URL: https://externals.io/message/21578#21581

Etienne Kneuss wrote:
> Notice that this behavior can be reproduced using only prefix increment
> operators.
> 
> Is that also expected to give undefined results ?

Yes, see
<http://en.wikipedia.org/wiki/C_syntax#Undefined_behavior>

- Chris

---

## #21582 — Marcus Boerger — 2006-01-19T19:53:58+00:00

URL: https://externals.io/message/21578#21582 · In reply to: #21578

Hello Marco,

  though Hartmut is perfectly correct in his statement here's what happens:

$a = 10; // 10
++$a     // 11
$a + $a  // 22
$a++     // 23

Thursday, January 19, 2006, 5:41:17 PM, you wrote:

> Today during a session i had a strange "magic" feature found in php.

> <?php
> $a = 10;
> echo ++$a + $a++;
?>>

just to verfiy echo $a after your echo line, it will show 12

> this works perfect as expected. it returns 22.
> but if i assign $a as a ref. to another variable it will be return 23.

> <?
> $a = 10;
> $b = &$a;
> echo ++$a + $a++;
?>>

> this will gave me to 23
> (used the $b to the pre post stuff)

> <?
> $a = 10;
> $b = &$a;
> echo ++$b + $b++;
?>>

> this just happens if i have a ref. count to my var. WHY? :)

> http://en.wikipedia.org/wiki/23_%28film%29

> --
> Marco Kaiser



Best regards,
 Marcus

---

## #21583 — Marco Kaiser — 2006-01-19T20:09:45+00:00

URL: https://externals.io/message/21578#21583 · In reply to: #21582

Hmm yes i know this,
but its very interesting for me to see how php internaly handles ++$a with a
pointer.
Now i understand it :)

-- Marco

2006/1/19, Marcus Boerger <helly@php.net>:
>
> Hello Marco,
>
>   though Hartmut is perfectly correct in his statement here's what
> happens:
>
> $a = 10; // 10
> ++$a     // 11
> $a + $a  // 22
> $a++     // 23
>
> Thursday, January 19, 2006, 5:41:17 PM, you wrote:
>
> > […]
>
> > […]
> ?>>
>
> just to verfiy echo $a after your echo line, it will show 12
>
> > […]
>
> > […]
> ?>>
>
> > […]
>
> > […]
> ?>>
>
> > […]
>
> > […]
>
> > […]
>
>
>
> Best regards,
> Marcus
>
>


--
Marco Kaiser

---

## #21584 — Jani Taskinen — 2006-01-19T20:42:54+00:00

URL: https://externals.io/message/21578#21584 · In reply to: #21583

For the 2nd time: references ARE NOT POINTERS! :-p

     --Jani




> Hmm yes i know this,
> but its very interesting for me to see how php internaly handles ++$a with a
> pointer.
> Now i understand it :)
>
> -- Marco
>
> 2006/1/19, Marcus Boerger <helly@php.net>:
> > […]
>
>
> --
> Marco Kaiser
>

-- 
Give me your money at @ <http://pecl.php.net/wishlist.php/sniper>
Donating money may make me happier and friendlier for a limited period!
Death to all 4 letter abbreviations starting with P!

---

## #21585 — Marco Kaiser — 2006-01-19T20:45:26+00:00

URL: https://externals.io/message/21578#21585 · In reply to: #21584

Sorry Jani,

i mean a reference (alias) :)

-- Marco

2006/1/19, Jani Taskinen <sniper@iki.fi>:
>
>
>      For the 2nd time: references ARE NOT POINTERS! :-p
>
>      --Jani
>
>
> 
>
> > […]
> with a
> > […]
>
> --
> Give me your money at @ <http://pecl.php.net/wishlist.php/sniper>
> Donating money may make me happier and friendlier for a limited period!
> Death to all 4 letter abbreviations starting with P!
>



--
Marco Kaiser

---

## #21587 — Jon Dowland — 2006-01-20T11:36:01+00:00

URL: https://externals.io/message/21578#21587 · In reply to: #21578

> Today during a session i had a strange "magic" feature found in php.
> 
> <?php
> $a = 10;
> echo ++$a + $a++;
> ?>
> 
> this works perfect as expected. it returns 22.

Odd, I expected 21:
	echo (11 + 10)
	a = a + 1

-- 
Jon Dowland
http://alcopop.org/

---

## #21588 — Carl P. Corliss — 2006-01-20T15:14:16+00:00

URL: https://externals.io/message/21578#21588 · In reply to: #21587

Jon Dowland wrote:
> 
> 
> > […]
> 
> 
> Odd, I expected 21:
> 	echo (11 + 10)
> 	a = a + 1
> 

nope - discounting the 'undefined behavior' of using pre/post increment 
operators, 22 would be the correct assumption - remember, broken into 
seperate statements, it's effectively:

$a = 10;
$a = $a + 1; // $a == 11
$a + $a;     // $a == 22

echo $a;

$a = $a + 1; // $a == 23


Cheers,

--
Carl

---

## #21590 — Andi Gutmans — 2006-01-20T16:47:28+00:00

URL: https://externals.io/message/21578#21590 · In reply to: #21588

Guys, please take this offlist. Undefined means undefined. There's no 
assumed way of it working.

Andi

At 07:14 AM 1/20/2006, Carl P. Corliss wrote:
>Jon Dowland wrote:
> > […]
>
>nope - discounting the 'undefined behavior' of using pre/post 
>increment operators, 22 would be the correct assumption - remember, 
>broken into seperate statements, it's effectively:
>
>$a = 10;
>$a = $a + 1; // $a == 11
>$a + $a;     // $a == 22
>
>echo $a;
>
>$a = $a + 1; // $a == 23
>
>
>Cheers,
>
>--
>Carl
