Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3518

Re: simple session question

From Richard Damon <news.x.richarddamon@xoxy.net>
Newsgroups comp.lang.php
Subject Re: simple session question
Date 2011-10-22 19:29 -0400
Organization A noiseless patient Spider
Message-ID <j7vjlc$bbh$1@dont-email.me> (permalink)
References (5 earlier) <j7st09$al6$1@news.albasani.net> <j7str1$4k0$1@dont-email.me> <j7uh5i$rvt$2@news.albasani.net> <j7vc0i$pnr$1@dont-email.me> <j7ve6a$svk$1@news.albasani.net>

Show all headers | View raw


On 10/22/11 5:56 PM, The Natural Philosopher wrote:
> Richard Damon wrote:
>> On 10/22/11 9:40 AM, Thomas Mlynarczyk wrote:
>>> Jerry Stuckle schrieb:
>>> [$foo = $foo++;]
>>>>> Is that mentioned somewhere in the manual?
>>>> Yes, see how operator precedence works. It is well defined.
>>>
>>> It is the internal order of execution that confuses me a bit. First, the
>>> expression on the right is evaluated, yielding the "not-yet-incremented"
>>> value. Then, two things must happen: Incrementing $foo and assigning
>>> $foo the value from the first step. The result depends on the order of
>>> these two steps. Clearly, the increment should happen before the next
>>> read access to $foo. But whether or not it happens before the next write
>>> access (assigning $foo the value from the first step) is neither
>>> intuitively clear (and optimizers might handle this one way or the
>>> other) nor explicitly stated in
>>> <http://de3.php.net/manual/en/language.operators.increment.php>. There
>>> is only a user comment saying: "The exact moment when post-increment and
>>> post-decrement happen is _just immediately after the variable is
>>> evaluated_ (not "after the line is processed" or something like that)".
>>> If this is meant to be documented behaviour, they should mention it as
>>> such in the manual.
>>>
>>> Greetings,
>>> Thomas
>>>
>>
>> I suspect that there is a difference between the execution model of
>> C/C++ and PHP here, do in part to the fact that C/C++ is (normally) a
>> compiled language with the goal of allowing the compiler to generate
>> as efficient of code as possible, while PHP is designed as a
>> interpreted language.
>>
>> in C, x = x++; is undefined behavior, as the timing of when the =
>> operator is executed and the writing back of the value of x++ from the
>> ++ operator is not specified, depending on when the compiler can most
>> efficiently implement it is ok. The code could be converted into the
>> equivalent of either.
>>
>
> I dont think it is. The value of x is assigned before the increment
> operator is applied: that's defined.
>
>

Where do you see the definition of assignment first? In fact, from the 
earlier examples, it is clear that at least those version of PHP did the 
increment first.

The wording of the $a++ postfix operator is that it "Returns $a, then 
increments $a by one." which if you want to be a stickler, requires the 
interpreter to fork, as after the execution "returns" it can't continue 
to do something. I think what they meant (as shown by the tests 
upthread) that $a++ increments $a, but returns the value of saved value 
of $a prior to the increment. To get an assignment comes first you would 
need something like: returns the current value of $a and then at [what 
here?] increments the value of $a. For [what here] you would need to add 
something like Cs sequence points, for example in

$b = f($a++);

would $a be incremented before calling f, or after the assignment to $b?

>> temp = x; /* save original value of x */
>> x = x+1 /* perform increment */
>> x = temp; /* perform the = */
>>
>> or
>> x = x; /* perform the = */
>> x = x+1; /* perform the ++ */
>>
>> PHP doesn't seem to reserve for itself this ability, and there seems
>> to be some comments (which you refer to) asserting that x++ will ALWAY
>> be the equivalent of
>>
>> temp = x;
>> x = x+1;
>> ... do what ever with temp
>>
>
> BUT that doesnt cover the $bar=($foo++);
>
> That should be
> INC [foo]
> MOV [bar].[foo];
>
> NOT the other way around.
>
> But in php it is.

Putting $foo++ inside parens SHOULD change its meaning to ++$foo !!! The 
value of the $foo++ operator is *always* the value of $foo before the 
increment.

Parens force operators inside them to bind to operands before an 
operator outside can bind to them. Let us say that * bound tighter than 
++ (thankfully it doesn't, not much does)

then $a*$b++ would ordinarily be parsed the same as ($a*$b)++, (which 
generates an error) but $a*($b++) generates what we normally want.


in the same way, fa()*(fb()+fc()), the () mean that we add the value of 
fb() to fc() before we multiply by fa(). but does NOT imply that fa() 
itself will be evaluated afterwards. To my knowledge, PHP does not 
define the order that operands are evaluated in an expression, only the 
precedence of the operators for which one binds tighter to the operand, 
except for the logical operators which will always evaluate the left 
operand first, and only if it then needs the value of the right operand, 
evaluate that.

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

simple session question cerr <ron.eggler@gmail.com> - 2011-10-21 10:44 -0700
  Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-21 20:19 +0200
    Re: simple session question Jonathan Stein <jstein@image.dk> - 2011-10-21 20:51 +0200
      Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-21 22:00 +0200
        Re: simple session question Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-21 16:14 -0400
          Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-22 00:50 +0200
            Re: simple session question Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-21 19:05 -0400
              Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-22 15:40 +0200
                Re: simple session question Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-22 17:19 -0400
                Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-22 22:56 +0100
                Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 00:35 +0200
                Re: simple session question Tim Streater <timstreater@greenbee.net> - 2011-10-22 23:43 +0100
                Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 01:56 +0200
                Re: simple session question Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-22 19:29 -0400
                Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 01:54 +0200
                Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 02:12 +0200
                Re: simple session question Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-22 21:13 -0400
                Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-23 08:44 +0100
                Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 17:31 +0200
                Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-23 17:18 +0100
                Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 19:14 +0200
                Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-23 18:16 +0100
                Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 19:00 +0200
                Re: simple session question Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-23 15:28 -0400
                Re: simple session question Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-22 23:03 -0400
                Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 19:06 +0200
                Re: simple session question Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-23 14:35 -0400
            Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-22 00:32 +0100
              Re: simple session question Luuk <Luuk@invalid.lan> - 2011-10-22 12:01 +0200
                Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-22 15:27 +0100
                Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-22 15:37 +0100
                Re: simple session question Luuk <Luuk@invalid.lan> - 2011-10-22 17:08 +0200
                OT: and even in Dart .........Re: simple session question Luuk <Luuk@invalid.lan> - 2011-10-22 17:17 +0200
                Re: OT: and even in Dart .........Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-22 16:54 +0100
                Re: OT: and even in Dart .........Re: simple session question Luuk <Luuk@invalid.lan> - 2011-10-22 18:47 +0200
                Re: OT: and even in Dart .........Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-22 19:34 +0200
                Re: OT: and even in Dart .........Re: simple session question Norman Peelman <npeelmandog@cfl.rr.com> - 2011-10-22 16:08 -0400
                Re: OT: and even in Dart .........Re: simple session question Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-22 16:28 -0400
                Re: OT: and even in Dart .........Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 01:39 +0200
                Re: OT: and even in Dart .........Re: simple session question Norman Peelman <npeelmandog@cfl.rr.com> - 2011-10-22 22:40 -0400
                Re: OT: and even in Dart .........Re: simple session question Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-22 23:05 -0400
                Re: OT: and even in Dart .........Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-22 22:52 +0100
                Re: OT: and even in Dart .........Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 01:25 +0200
                Re: OT: and even in Dart .........Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-23 08:41 +0100
                Re: OT: and even in Dart .........Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 00:56 +0200
                Re: OT: and even in Dart .........Re: simple session question Norman Peelman <npeelmandog@cfl.rr.com> - 2011-10-22 22:52 -0400
                Re: OT: and even in Dart .........Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 17:12 +0200
                Re: OT: and even in Dart .........Re: simple session question Tim Streater <timstreater@greenbee.net> - 2011-10-23 09:39 +0100
                Re: OT: and even in Dart .........Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-23 09:40 +0100
                Re: OT: and even in Dart .........Re: simple session question Tim Streater <timstreater@greenbee.net> - 2011-10-23 09:43 +0100
                Re: OT: and even in Dart .........Re: simple session question "Peter H. Coffin" <hellsop@ninehells.com> - 2011-11-05 15:04 -0500
                Re: OT: and even in Dart .........Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-22 22:47 +0100
                Re: OT: and even in Dart .........Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 01:22 +0200
                Re: OT: and even in Dart .........Re: simple session question The Natural Philosopher <tnp@invalid.invalid> - 2011-10-23 08:40 +0100
                Re: OT: and even in Dart .........Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-23 17:04 +0200
                Re: OT: and even in Dart .........Re: simple session question "Peter H. Coffin" <hellsop@ninehells.com> - 2011-11-05 15:00 -0500
              Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-22 15:39 +0200
    Re: simple session question cerr <ron.eggler@gmail.com> - 2011-10-21 14:43 -0700
      Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-22 00:47 +0200
      Re: simple session question Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-21 19:06 -0400
    Re: simple session question cerr <ron.eggler@gmail.com> - 2011-10-21 14:17 -0700
      Re: simple session question Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-10-22 15:50 +0200
    Re: simple session question Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-23 15:47 +0000

csiph-web