Discussion:
Why PHP sucks - farce or is there a bit of truth?
Tim-Hinnerk Heuer
2013-10-18 00:46:04 UTC
Permalink
Hi,

I've been a PHP programmer for several years now and have a bit of a
love-hate relationship with it. It's great for doing something quickly,
especially web stuff, but recently I have heard people moaning about PHP a
lot and did some research and found this:

http://webonastick.com/php.html

One thing I had to get my head around is this:
The ternary operator
<?php
$foo = 1;
print(($foo == 1) ? "uno" : ($foo === 2) ? "dos" : "tres");
print("\n");

outputs
dos
because the operator is left-to-right associative instead of right-to-left
as in other languages. I was thinking there must be a reason for this.
Speed? Is it faster to evaluate/implement all operators as left-to-right?

I noticed that the above could easily be fixed by saying:

<?php
$foo = 1;
print(($foo == 1) ? "uno" : (($foo === 2) ? "dos" : "tres"));
print("\n");

outputs
uno
Was this a deliberate design decision or is it a flaky implementation of
the ternary operator?

Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Daniel
2013-10-18 00:49:34 UTC
Permalink
Post by Tim-Hinnerk Heuer
Hi,
I've been a PHP programmer for several years now and have a bit of a
love-hate relationship with it. It's great for doing something quickly,
especially web stuff, but recently I have heard people moaning about PHP a
http://webonastick.com/php.html
The ternary operator
<?php
$foo = 1;
print(($foo == 1) ? "uno" : ($foo === 2) ? "dos" : "tres");
print("\n");
outputs
dos
because the operator is left-to-right associative instead of right-to-left
as in other languages. I was thinking there must be a reason for this.
Speed? Is it faster to evaluate/implement all operators as left-to-right?
<?php
$foo = 1;
print(($foo == 1) ? "uno" : (($foo === 2) ? "dos" : "tres"));
print("\n");
outputs
uno
Was this a deliberate design decision or is it a flaky implementation of
the ternary operator?
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Maybe it just me but I look at it the same as math, you don't add
something up from right to left do you?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tim-Hinnerk Heuer
2013-10-18 00:51:54 UTC
Permalink
No, but when you add stuff it gets the same result from left to right as
from right to left.
So, the ternary operator is more related to logic, even though logic
belongs to math.

Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Post by Tim-Hinnerk Heuer
Post by Tim-Hinnerk Heuer
Hi,
I've been a PHP programmer for several years now and have a bit of a
love-hate relationship with it. It's great for doing something quickly,
especially web stuff, but recently I have heard people moaning about PHP
a
Post by Tim-Hinnerk Heuer
http://webonastick.com/php.html
The ternary operator
<?php
$foo = 1;
print(($foo == 1) ? "uno" : ($foo === 2) ? "dos" : "tres");
print("\n");
outputs
dos
because the operator is left-to-right associative instead of
right-to-left
Post by Tim-Hinnerk Heuer
as in other languages. I was thinking there must be a reason for this.
Speed? Is it faster to evaluate/implement all operators as left-to-right?
<?php
$foo = 1;
print(($foo == 1) ? "uno" : (($foo === 2) ? "dos" : "tres"));
print("\n");
outputs
uno
Was this a deliberate design decision or is it a flaky implementation of
the ternary operator?
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Maybe it just me but I look at it the same as math, you don't add
something up from right to left do you?
Tamara Temple
2013-10-18 00:59:17 UTC
Permalink
Ternary operator works the same in every language I've encountered… you can expand every such statement to an if-then-else if it makes you feel better. Not every operator in math is transitive, either. a - b is not the same as b - a, for example.
Post by Tim-Hinnerk Heuer
No, but when you add stuff it gets the same result from left to right as
from right to left.
So, the ternary operator is more related to logic, even though logic
belongs to math.
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Post by Tim-Hinnerk Heuer
Post by Tim-Hinnerk Heuer
Hi,
I've been a PHP programmer for several years now and have a bit of a
love-hate relationship with it. It's great for doing something quickly,
especially web stuff, but recently I have heard people moaning about PHP
a
Post by Tim-Hinnerk Heuer
http://webonastick.com/php.html
The ternary operator
<?php
$foo = 1;
print(($foo == 1) ? "uno" : ($foo === 2) ? "dos" : "tres");
print("\n");
outputs
dos
because the operator is left-to-right associative instead of
right-to-left
Post by Tim-Hinnerk Heuer
as in other languages. I was thinking there must be a reason for this.
Speed? Is it faster to evaluate/implement all operators as left-to-right?
<?php
$foo = 1;
print(($foo == 1) ? "uno" : (($foo === 2) ? "dos" : "tres"));
print("\n");
outputs
uno
Was this a deliberate design decision or is it a flaky implementation of
the ternary operator?
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Maybe it just me but I look at it the same as math, you don't add
something up from right to left do you?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tim-Hinnerk Heuer
2013-10-18 01:05:06 UTC
Permalink
a - b = (+a) + (-b) = (-b) + (+a)

The argument is about the ternary operator though:
#include<stdio.h>

main()
{
int a = 1;
printf("%s", (a == 1) ? "one" : (a == 2) ? "two" : "three");

}
Post by Tim-Hinnerk Heuer
one
So, why is PHP different?

Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Ternary operator works the same in every language I've encountered
 you
can expand every such statement to an if-then-else if it makes you feel
better. Not every operator in math is transitive, either. a - b is not the
same as b - a, for example.
Post by Tim-Hinnerk Heuer
No, but when you add stuff it gets the same result from left to right as
from right to left.
So, the ternary operator is more related to logic, even though logic
belongs to math.
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Post by Tim-Hinnerk Heuer
Post by Tim-Hinnerk Heuer
Hi,
I've been a PHP programmer for several years now and have a bit of a
love-hate relationship with it. It's great for doing something quickly,
especially web stuff, but recently I have heard people moaning about
PHP
Post by Tim-Hinnerk Heuer
Post by Tim-Hinnerk Heuer
a
Post by Tim-Hinnerk Heuer
http://webonastick.com/php.html
The ternary operator
<?php
$foo = 1;
print(($foo == 1) ? "uno" : ($foo === 2) ? "dos" : "tres");
print("\n");
outputs
dos
because the operator is left-to-right associative instead of
right-to-left
Post by Tim-Hinnerk Heuer
as in other languages. I was thinking there must be a reason for this.
Speed? Is it faster to evaluate/implement all operators as
left-to-right?
Post by Tim-Hinnerk Heuer
Post by Tim-Hinnerk Heuer
Post by Tim-Hinnerk Heuer
<?php
$foo = 1;
print(($foo == 1) ? "uno" : (($foo === 2) ? "dos" : "tres"));
print("\n");
outputs
uno
Was this a deliberate design decision or is it a flaky implementation
of
Post by Tim-Hinnerk Heuer
Post by Tim-Hinnerk Heuer
Post by Tim-Hinnerk Heuer
the ternary operator?
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Maybe it just me but I look at it the same as math, you don't add
something up from right to left do you?
Tim-Hinnerk Heuer
2013-10-18 01:15:45 UTC
Permalink
Post by Tim-Hinnerk Heuer
(a == 1) ? "one" : (a == 2) ? "two" : "three"
Same in JS:
$ node
Post by Tim-Hinnerk Heuer
a = 1;
1
Post by Tim-Hinnerk Heuer
(a == 1) ? "one" : (a == 2) ? "two" : "three"
'one'


Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Robert Cummings
2013-10-18 03:45:11 UTC
Permalink
Post by Tim-Hinnerk Heuer
a - b = (+a) + (-b) = (-b) + (+a)
#include<stdio.h>
main()
{
int a = 1;
printf("%s", (a == 1) ? "one" : (a == 2) ? "two" : "three");
}
one
So, why is PHP different?
Why not? Both are valid interpretations. if you don't want ambiguity,
clarify the expression with parenthesis (as you've shown in a subsequent
post). One can't hinge the entire argument of whether PHP sucks on a
right-left/left-right interpretation of the ternary operator, yet this
is what you've chosen to focus on.

Let's also put things in perspective, when a language reaches an
audience in the millions SOMEONE will have something to whine about...
and it just so happens some people like to whine loudly like a baby with
a soiled nappie.

:)

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tim-Hinnerk Heuer
2013-10-18 08:06:34 UTC
Permalink
Clearly you haven't read the whole thread. The subject line asks the
question:
"Why PHP sucks - farce or is there a bit of truth?" - and there is a link
to the article of which I found many more btw.
I am not dissing PHP at all. In fact, as I said, I have been a PHP
programmer for many years...
It was just one thing that puzzled me and that I followed up on. I even
asked: "Is it faster to execute/implement if evaluated left-to-right?"
which would be an argument for this.

I've heard many people moan about lots of languages and there are
weaknesses in most if not all of them.

All I'm trying to do here is improving the language by raising issues (one
at a time) on this list to better understand the language and get people
thinking.

Another issue I found is:
Not so useful return values:

<?php
$b = false;
$c = "orange";
$a = $b || $c;
echo $a;

outputs:
1

JS seems more convenient here. As a scripting language it also has the
notion of truthy but returns the default value "orange".

:-)

Cheers,
Tim

Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Post by Robert Cummings
Post by Tim-Hinnerk Heuer
a - b = (+a) + (-b) = (-b) + (+a)
#include<stdio.h>
main()
{
int a = 1;
printf("%s", (a == 1) ? "one" : (a == 2) ? "two" : "three");
}
one
So, why is PHP different?
Why not? Both are valid interpretations. if you don't want ambiguity,
clarify the expression with parenthesis (as you've shown in a subsequent
post). One can't hinge the entire argument of whether PHP sucks on a
right-left/left-right interpretation of the ternary operator, yet this is
what you've chosen to focus on.
Let's also put things in perspective, when a language reaches an audience
in the millions SOMEONE will have something to whine about... and it just
so happens some people like to whine loudly like a baby with a soiled
nappie.
:)
Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
Tim-Hinnerk Heuer
2013-10-18 10:18:10 UTC
Permalink
http://stackoverflow.com/questions/1921422/php-nested-conditional-operator-bug
discusses
this as well.

On a slightly brighter note:

I've never used the ternary operator with two ternaries or more after each
other. I think it shouldn't be over-used anyway. Even just using one
ternary operator can be confusing for someone who
doesn't use it often. So, I avoid it and so should any sane programmer
unless its use is really the best thing to do. I use it with isset for
example but not when there are more than two choices which is the case
in the examples above. So, after all, I wouldn't be too bent out of shape
if it stays left-to-right.

Cheers,
Tim

Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Post by Tim-Hinnerk Heuer
Clearly you haven't read the whole thread. The subject line asks the
"Why PHP sucks - farce or is there a bit of truth?" - and there is a link
to the article of which I found many more btw.
I am not dissing PHP at all. In fact, as I said, I have been a PHP
programmer for many years...
It was just one thing that puzzled me and that I followed up on. I even
asked: "Is it faster to execute/implement if evaluated left-to-right?"
which would be an argument for this.
I've heard many people moan about lots of languages and there are
weaknesses in most if not all of them.
All I'm trying to do here is improving the language by raising issues (one
at a time) on this list to better understand the language and get people
thinking.
<?php
$b = false;
$c = "orange";
$a = $b || $c;
echo $a;
1
JS seems more convenient here. As a scripting language it also has the
notion of truthy but returns the default value "orange".
:-)
Cheers,
Tim
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Post by Robert Cummings
Post by Tim-Hinnerk Heuer
a - b = (+a) + (-b) = (-b) + (+a)
#include<stdio.h>
main()
{
int a = 1;
printf("%s", (a == 1) ? "one" : (a == 2) ? "two" : "three");
}
one
So, why is PHP different?
Why not? Both are valid interpretations. if you don't want ambiguity,
clarify the expression with parenthesis (as you've shown in a subsequent
post). One can't hinge the entire argument of whether PHP sucks on a
right-left/left-right interpretation of the ternary operator, yet this is
what you've chosen to focus on.
Let's also put things in perspective, when a language reaches an audience
in the millions SOMEONE will have something to whine about... and it just
so happens some people like to whine loudly like a baby with a soiled
nappie.
:)
Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
Robert Cummings
2013-10-18 13:46:50 UTC
Permalink
Post by Tim-Hinnerk Heuer
Clearly you haven't read the whole thread. The subject line asks the
"Why PHP sucks - farce or is there a bit of truth?" - and there is a
link to the article of which I found many more btw.
Actually, I did, but thanks for the huge font... I needed to step out of
the office to take it all in and so I grabbed a coffee too.
Post by Tim-Hinnerk Heuer
I am not dissing PHP at all. In fact, as I said, I have been a PHP
programmer for many years...
I didn't say you were.
Post by Tim-Hinnerk Heuer
It was just one thing that puzzled me and that I followed up on. I even
asked: "Is it faster to execute/implement if evaluated left-to-right?"
which would be an argument for this.
You're on the wrong list to ask this question... that's something more
appropriate for Internals where they discuss the PHP engine development
itself.
Post by Tim-Hinnerk Heuer
I've heard many people moan about lots of languages and there are
weaknesses in most if not all of them.
Indeed.
Post by Tim-Hinnerk Heuer
All I'm trying to do here is improving the language by raising issues
(one at a time) on this list to better understand the language and get
people thinking.
You're on the wrong list... internals is where you improve the language,
and the best way to do so is to write an RFC, submit it for review, and
depending on feedback you can write the code.
Post by Tim-Hinnerk Heuer
<?php
$b = false;
$c = "orange";
$a = $b || $c;
echo $a;
1
No, you'll find that $a contains the boolean value true. It just happens
to print as a 1. In this case it makes perfect sense... you are using a
boolean logic operator, it should return a boolean. Yes, yes, I know
some languages return the result of the first expression itself that
evaluates to true.
<?php

$b = false;
$c = 'orange';
$a = $b || $c;
var_dump( $a );

?>
Post by Tim-Hinnerk Heuer
JS seems more convenient here. As a scripting language it also has the
notion of truthy but returns the default value "orange".
Explain that notion of "default". Why is orange the default. What if the
order of evaluation were to be changed due to some optimization in the
future. Then the "default" would change. If you WANT the value then you
should make it explicit instead of obfuscating your code by relying on
the underlying implementation:
<?php

$b = false;
$c = 'orange';
($a = $b) || ($a = $c);
var_dump( $a );

?>
Post by Tim-Hinnerk Heuer
:-)
;)

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tim-Hinnerk Heuer
2013-10-18 00:50:26 UTC
Permalink
http://php.net/manual/en/language.operators.comparison.php#example-122

Found this in relation, so this behaviour is documented.

Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Post by Tim-Hinnerk Heuer
Hi,
I've been a PHP programmer for several years now and have a bit of a
love-hate relationship with it. It's great for doing something quickly,
especially web stuff, but recently I have heard people moaning about PHP a
http://webonastick.com/php.html
The ternary operator
<?php
$foo = 1;
print(($foo == 1) ? "uno" : ($foo === 2) ? "dos" : "tres");
print("\n");
outputs
dos
because the operator is left-to-right associative instead of right-to-left
as in other languages. I was thinking there must be a reason for this.
Speed? Is it faster to evaluate/implement all operators as left-to-right?
<?php
$foo = 1;
print(($foo == 1) ? "uno" : (($foo === 2) ? "dos" : "tres"));
print("\n");
outputs
uno
Was this a deliberate design decision or is it a flaky implementation of
the ternary operator?
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Jim Giner
2013-10-18 14:13:18 UTC
Permalink
Post by Tim-Hinnerk Heuer
http://php.net/manual/en/language.operators.comparison.php#example-122
Found this in relation, so this behaviour is documented.
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Post by Tim-Hinnerk Heuer
Hi,
I've been a PHP programmer for several years now and have a bit of a
love-hate relationship with it. It's great for doing something quickly,
especially web stuff, but recently I have heard people moaning about PHP a
http://webonastick.com/php.html
The ternary operator
<?php
$foo = 1;
print(($foo == 1) ? "uno" : ($foo === 2) ? "dos" : "tres");
print("\n");
outputs
dos
because the operator is left-to-right associative instead of right-to-left
as in other languages. I was thinking there must be a reason for this.
Speed? Is it faster to evaluate/implement all operators as left-to-right?
<?php
$foo = 1;
print(($foo == 1) ? "uno" : (($foo === 2) ? "dos" : "tres"));
print("\n");
outputs
uno
Was this a deliberate design decision or is it a flaky implementation of
the ternary operator?
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Having endured this entire thread (from T-to-B) I have to add my $.02.

I recently experienced the rather unexpected outcome of a ternary within
a ternary. Rather than consider the order of evaluation being L-to-R or
R-to-L, I merely diagnosed it as a simple bug in PHP - which BTW I love,
despite it's less than consistent syntax. Reading all the talk of order
of evaluation, I have to say in my 40+ years of programming in a variety
of languages, I can't recall ever having to consider the topic. When I
wrote code I always expected and relied upon the order being the way I
wrote it. And - I usually added parentheses to help clarify in my mind
what I was doing with the statement as I wrote it, although sometimes I
knew they were necessary to achieve the analysis I had in mind.

So - as expected, the ternary operator does work from L-to-R except when
joined with a second (or third?) ternary operator. Why that is I can't
figure out other than to assume it's an un-resolved bug, that occurs so
seldom it's probably never been issued. Oh, well.....
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2013-10-18 14:33:27 UTC
Permalink
Post by Jim Giner
Having endured this entire thread (from T-to-B) I have to add my $.02.
I recently experienced the rather unexpected outcome of a ternary within
a ternary. Rather than consider the order of evaluation being L-to-R or
R-to-L, I merely diagnosed it as a simple bug in PHP - which BTW I love,
despite it's less than consistent syntax. Reading all the talk of order
of evaluation, I have to say in my 40+ years of programming in a variety
of languages, I can't recall ever having to consider the topic. When I
wrote code I always expected and relied upon the order being the way I
wrote it. And - I usually added parentheses to help clarify in my mind
what I was doing with the statement as I wrote it, although sometimes I
knew they were necessary to achieve the analysis I had in mind.
So - as expected, the ternary operator does work from L-to-R except when
joined with a second (or third?) ternary operator. Why that is I can't
figure out other than to assume it's an un-resolved bug, that occurs so
seldom it's probably never been issued. Oh, well.....
Hi Jim,

I'm not sure it's a bug. There are two considerations... should the
first use of the ternary operator feed the first operand of the second,
or should the second use of the ternary operator feed the 3rd operand of
the first use. Visually... which is more correct:
<?php

$foo = ($a ? $b : $c) ? $d : $e; // PHP does this one.

$foo = $a ? $b : ($c ? $d : $e);

?>

They both seem perfectly reasonable and so I would say it's my job to
clarify if I don't want to leave it the the vagaries of the developer's
subjective opinion :) To me this isn't a flaw in PHP, but an oversight
by the programmer (because they don't see the ambiguity) or they have an
incorrect assumption that one way is the right way. Either way... do you
think the following makes for readable code?
<?php

$foo = $a ? $b : $c ? $d : $e;

?>

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-10-18 19:02:20 UTC
Permalink
Post by Robert Cummings
Hi Jim,
I'm not sure it's a bug. There are two considerations... should the
first use of the ternary operator feed the first operand of the second,
or should the second use of the ternary operator feed the 3rd operand of
<?php
$foo = ($a ? $b : $c) ? $d : $e; // PHP does this one.
$foo = $a ? $b : ($c ? $d : $e);
?>
They both seem perfectly reasonable and so I would say it's my job to
clarify if I don't want to leave it the the vagaries of the developer's
subjective opinion :) To me this isn't a flaw in PHP, but an oversight
by the programmer (because they don't see the ambiguity) or they have an
incorrect assumption that one way is the right way. Either way... do you
think the following makes for readable code?
<?php
$foo = $a ? $b : $c ? $d : $e;
?>
Cheers,
Rob.
When I write something like this, I expect it to happen from left to
right. Always have, always will. That said I see the 'proper'
expectation of this statement:

$foo = $a ? $b : $c ? $d : $e;

as:

$foo will be the result of "if $a then $b else if $c then $d else $e;"

Why php interprets it differently is just not logical to me. Of course
writing something like that with one set of parens solves the riddle as
well as making it more readable:

$foo = $a ? $b : ($c ? $d : $e);

As for ambiguity - I don't see it. Again the literal syntax of the
ternary if is (IMHO):
"if (cond) then (statement) else (other statment)"

In your example:
$foo = $a ? $b : $c ? $d : $e;
I see it exactly as this:
"if $a is true then
$b
else
if $c is true then
$d
else
$e;"

Why should php evaluate the last statement before deciding if it even
needs that statement?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-10-18 19:18:10 UTC
Permalink
Post by Jim Giner
Post by Robert Cummings
Hi Jim,
I'm not sure it's a bug. There are two considerations... should the
first use of the ternary operator feed the first operand of the second,
or should the second use of the ternary operator feed the 3rd operand of
<?php
$foo = ($a ? $b : $c) ? $d : $e; // PHP does this one.
$foo = $a ? $b : ($c ? $d : $e);
?>
They both seem perfectly reasonable and so I would say it's my job to
clarify if I don't want to leave it the the vagaries of the developer's
subjective opinion :) To me this isn't a flaw in PHP, but an oversight
by the programmer (because they don't see the ambiguity) or they have an
incorrect assumption that one way is the right way. Either way... do you
think the following makes for readable code?
<?php
$foo = $a ? $b : $c ? $d : $e;
?>
Cheers,
Rob.
When I write something like this, I expect it to happen from left to
right. Always have, always will. That said I see the 'proper'
$foo = $a ? $b : $c ? $d : $e;
$foo will be the result of "if $a then $b else if $c then $d else $e;"
Why php interprets it differently is just not logical to me. Of course
writing something like that with one set of parens solves the riddle as
$foo = $a ? $b : ($c ? $d : $e);
As for ambiguity - I don't see it. Again the literal syntax of the
"if (cond) then (statement) else (other statment)"
$foo = $a ? $b : $c ? $d : $e;
"if $a is true then
$b
else
if $c is true then
$d
else
$e;"
Why should php evaluate the last statement before deciding if it even
needs that statement?
And after posting the above and re-reading it several times I can now
see the 'other' interpretation of this puzzle.

Syntactically the ternary operator is recognized by the ? - no?
Therefore the compiler sees that and splits the statement into the three
parts: condition, true result & false result. Call this picture #1.

But in this situation the compiler runs into a second ? and has to
process it a bit differently.

The parts of the concatenation will be condition (picture #1), true
result, & false result.

I guess it's a case of that old expression "the emphasis is on the wrong
syl-lable".

So - while readers of English see it one way, that little old automaton
sees it differently and quite accurately it would seem. I guess the
writers of that compiler never thought about the future confusion it
would raise. Kind of like what I previously mentioned in my earlier
post about less than consistent syntax. This behavior surely is not the
same as the interpretation of a regular old if/then/else with
concatenated if/then/else pieces added in. Would you agree?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Harkness
2013-10-18 19:20:51 UTC
Permalink
Post by Robert Cummings
$foo = $a ? $b : $c ? $d : $e;
$foo will be the result of "if $a then $b else if $c then $d else $e;"
Why php interprets it differently is just not logical to me.
While I agree that PHP probably should have matched other languages like C
and Java, a) I have never needed to use this double construct, b) I
wouldn't even if it made sense due to the likely confusion, and c) PHP has
the Elvis operator ($x ?: $y) which is pure awesomeness so I can forgive
it's wonky ternary precedence.

Far more troublesome with PHP is the mixed parameter ordering in the
built-in functions. The quote on the top of that page says it all. I'm
constantly having to rely on code-completion for functions I've used for
years to make sure I'm getting the order right. Haystack before needle or
needle before haystack? Both! :(

Cheers,
David
German Geek
2013-10-19 03:32:58 UTC
Permalink
I agree, it's not a bug, just a different design or interpretation of the
statement. I further agree that the programmer should put parentheses
around statements if visually ambiguous. Also, from left to right seems
more logical. That it happens in this particular case that the result seems
counter-intuitive doesn't mean it is illogical. I just had trouble
understanding at first why it would output 'two' instead of 'one' when the
contents of the variable clearly was 1.

A different construct could be used better used in a case where a variable
can have more than 2 values. elseif or even a switch, though more verbose,
is probably much more readable.

Sorry, Robert, for the huge font. I was sure I had reduced it before
sending in gmail... Copy/paste from the website made it that big.

I raised the issue also to get a better understanding what other php
programmers think and I much better understand some things now.


Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Post by David Harkness
Post by Robert Cummings
$foo = $a ? $b : $c ? $d : $e;
$foo will be the result of "if $a then $b else if $c then $d else $e;"
Why php interprets it differently is just not logical to me.
While I agree that PHP probably should have matched other languages like C
and Java, a) I have never needed to use this double construct, b) I
wouldn't even if it made sense due to the likely confusion, and c) PHP has
the Elvis operator ($x ?: $y) which is pure awesomeness so I can forgive
it's wonky ternary precedence.
Far more troublesome with PHP is the mixed parameter ordering in the
built-in functions. The quote on the top of that page says it all. I'm
constantly having to rely on code-completion for functions I've used for
years to make sure I'm getting the order right. Haystack before needle or
needle before haystack? Both! :(
Cheers,
David
German Geek
2013-10-19 03:46:45 UTC
Permalink
German Geek is me btw. :-) I have that email as well and must've replied to
the email that got to that account... What happens when you over-automate.

I wouldn't use a 2-factor ternary operator, especially after this
discussion, lol. Saw that for the first time on that website. I personally
hate when people obfuscate just to make shorter lines or seem smarter. Code
is edited by different people if it is useful or good.

Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Post by German Geek
I agree, it's not a bug, just a different design or interpretation of the
statement. I further agree that the programmer should put parentheses
around statements if visually ambiguous. Also, from left to right seems
more logical. That it happens in this particular case that the result seems
counter-intuitive doesn't mean it is illogical. I just had trouble
understanding at first why it would output 'two' instead of 'one' when the
contents of the variable clearly was 1.
A different construct could be used better used in a case where a variable
can have more than 2 values. elseif or even a switch, though more verbose,
is probably much more readable.
Sorry, Robert, for the huge font. I was sure I had reduced it before
sending in gmail... Copy/paste from the website made it that big.
I raised the issue also to get a better understanding what other php
programmers think and I much better understand some things now.
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Post by David Harkness
Post by Robert Cummings
$foo = $a ? $b : $c ? $d : $e;
$foo will be the result of "if $a then $b else if $c then $d else $e;"
Why php interprets it differently is just not logical to me.
While I agree that PHP probably should have matched other languages like C
and Java, a) I have never needed to use this double construct, b) I
wouldn't even if it made sense due to the likely confusion, and c) PHP has
the Elvis operator ($x ?: $y) which is pure awesomeness so I can forgive
it's wonky ternary precedence.
Far more troublesome with PHP is the mixed parameter ordering in the
built-in functions. The quote on the top of that page says it all. I'm
constantly having to rely on code-completion for functions I've used for
years to make sure I'm getting the order right. Haystack before needle or
needle before haystack? Both! :(
Cheers,
David
Tim-Hinnerk Heuer
2013-10-19 03:47:48 UTC
Permalink
oops, saw my picture and assumed it would come from the correct account,
German Geek.

Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Post by German Geek
German Geek is me btw. :-) I have that email as well and must've replied
to the email that got to that account... What happens when you
over-automate.
I wouldn't use a 2-factor ternary operator, especially after this
discussion, lol. Saw that for the first time on that website. I personally
hate when people obfuscate just to make shorter lines or seem smarter. Code
is edited by different people if it is useful or good.
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
Post by German Geek
I agree, it's not a bug, just a different design or interpretation of the
statement. I further agree that the programmer should put parentheses
around statements if visually ambiguous. Also, from left to right seems
more logical. That it happens in this particular case that the result seems
counter-intuitive doesn't mean it is illogical. I just had trouble
understanding at first why it would output 'two' instead of 'one' when the
contents of the variable clearly was 1.
A different construct could be used better used in a case where a
variable can have more than 2 values. elseif or even a switch, though more
verbose, is probably much more readable.
Sorry, Robert, for the huge font. I was sure I had reduced it before
sending in gmail... Copy/paste from the website made it that big.
I raised the issue also to get a better understanding what other php
programmers think and I much better understand some things now.
Tim-Hinnerk Heuer
Blog: http://www.thheuer.com
On Fri, Oct 18, 2013 at 12:02 PM, Jim Giner <
Post by Robert Cummings
$foo = $a ? $b : $c ? $d : $e;
$foo will be the result of "if $a then $b else if $c then $d else $e;"
Why php interprets it differently is just not logical to me.
While I agree that PHP probably should have matched other languages like C
and Java, a) I have never needed to use this double construct, b) I
wouldn't even if it made sense due to the likely confusion, and c) PHP has
the Elvis operator ($x ?: $y) which is pure awesomeness so I can forgive
it's wonky ternary precedence.
Far more troublesome with PHP is the mixed parameter ordering in the
built-in functions. The quote on the top of that page says it all. I'm
constantly having to rely on code-completion for functions I've used for
years to make sure I'm getting the order right. Haystack before needle or
needle before haystack? Both! :(
Cheers,
David
TQ White II
2013-10-19 00:56:11 UTC
Permalink
If you wrote a nested ternary like that I'd fire your ass.

As one of the main programming philosophers said, the art of programming is making your intentions clear to the next programmer.


-----------------------------------

TQ White II
708-763-0100
Post by Robert Cummings
Post by Robert Cummings
Hi Jim,
I'm not sure it's a bug. There are two considerations... should the
first use of the ternary operator feed the first operand of the second,
or should the second use of the ternary operator feed the 3rd operand of
<?php
$foo = ($a ? $b : $c) ? $d : $e; // PHP does this one.
$foo = $a ? $b : ($c ? $d : $e);
?>
They both seem perfectly reasonable and so I would say it's my job to
clarify if I don't want to leave it the the vagaries of the developer's
subjective opinion :) To me this isn't a flaw in PHP, but an oversight
by the programmer (because they don't see the ambiguity) or they have an
incorrect assumption that one way is the right way. Either way... do you
think the following makes for readable code?
<?php
$foo = $a ? $b : $c ? $d : $e;
?>
Cheers,
Rob.
$foo = $a ? $b : $c ? $d : $e;
$foo will be the result of "if $a then $b else if $c then $d else $e;"
$foo = $a ? $b : ($c ? $d : $e);
"if (cond) then (statement) else (other statment)"
$foo = $a ? $b : $c ? $d : $e;
"if $a is true then
$b
else
if $c is true then
$d
else
$e;"
Why should php evaluate the last statement before deciding if it even needs that statement?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jonathan Sundquist
2013-10-19 01:43:41 UTC
Permalink
Always develop like the next person to work on the code is an axe murderer
who know where you live.
Post by TQ White II
If you wrote a nested ternary like that I'd fire your ass.
As one of the main programming philosophers said, the art of programming
is making your intentions clear to the next programmer.
-----------------------------------
TQ White II
708-763-0100
Post by Jim Giner
Post by Robert Cummings
Hi Jim,
I'm not sure it's a bug. There are two considerations... should the
first use of the ternary operator feed the first operand of the second,
or should the second use of the ternary operator feed the 3rd operand of
<?php
$foo = ($a ? $b : $c) ? $d : $e; // PHP does this one.
$foo = $a ? $b : ($c ? $d : $e);
?>
They both seem perfectly reasonable and so I would say it's my job to
clarify if I don't want to leave it the the vagaries of the developer's
subjective opinion :) To me this isn't a flaw in PHP, but an oversight
by the programmer (because they don't see the ambiguity) or they have an
incorrect assumption that one way is the right way. Either way... do you
think the following makes for readable code?
<?php
$foo = $a ? $b : $c ? $d : $e;
?>
Cheers,
Rob.
When I write something like this, I expect it to happen from left to
right. Always have, always will. That said I see the 'proper' expectation
Post by Jim Giner
$foo = $a ? $b : $c ? $d : $e;
$foo will be the result of "if $a then $b else if $c then $d else $e;"
Why php interprets it differently is just not logical to me. Of course
writing something like that with one set of parens solves the riddle as
Post by Jim Giner
$foo = $a ? $b : ($c ? $d : $e);
As for ambiguity - I don't see it. Again the literal syntax of the
"if (cond) then (statement) else (other statment)"
$foo = $a ? $b : $c ? $d : $e;
"if $a is true then
$b
else
if $c is true then
$d
else
$e;"
Why should php evaluate the last statement before deciding if it even
needs that statement?
Post by Jim Giner
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2013-10-19 02:46:07 UTC
Permalink
*lol*
Post by Jonathan Sundquist
Always develop like the next person to work on the code is an axe murderer
who know where you live.
Post by TQ White II
If you wrote a nested ternary like that I'd fire your ass.
As one of the main programming philosophers said, the art of programming
is making your intentions clear to the next programmer.
-----------------------------------
TQ White II
708-763-0100
Post by Jim Giner
Post by Robert Cummings
Hi Jim,
I'm not sure it's a bug. There are two considerations... should the
first use of the ternary operator feed the first operand of the second,
or should the second use of the ternary operator feed the 3rd operand of
<?php
$foo = ($a ? $b : $c) ? $d : $e; // PHP does this one.
$foo = $a ? $b : ($c ? $d : $e);
?>
They both seem perfectly reasonable and so I would say it's my job to
clarify if I don't want to leave it the the vagaries of the developer's
subjective opinion :) To me this isn't a flaw in PHP, but an oversight
by the programmer (because they don't see the ambiguity) or they have an
incorrect assumption that one way is the right way. Either way... do you
think the following makes for readable code?
<?php
$foo = $a ? $b : $c ? $d : $e;
?>
Cheers,
Rob.
When I write something like this, I expect it to happen from left to
right. Always have, always will. That said I see the 'proper' expectation
Post by Jim Giner
$foo = $a ? $b : $c ? $d : $e;
$foo will be the result of "if $a then $b else if $c then $d else $e;"
Why php interprets it differently is just not logical to me. Of course
writing something like that with one set of parens solves the riddle as
Post by Jim Giner
$foo = $a ? $b : ($c ? $d : $e);
As for ambiguity - I don't see it. Again the literal syntax of the
"if (cond) then (statement) else (other statment)"
$foo = $a ? $b : $c ? $d : $e;
"if $a is true then
$b
else
if $c is true then
$d
else
$e;"
Why should php evaluate the last statement before deciding if it even
needs that statement?
Post by Jim Giner
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Peter Ford
2013-10-21 07:54:22 UTC
Permalink
Post by Jonathan Sundquist
Always develop like the next person to work on the code is an axe murderer
who know where you live.
Oh.
I'd better start refactoring some code then...
I like messy code - it gives me an excuse when the boss says "Why
haven't you added that extra functionality yet?"
:)
--
Peter Ford phone: 01580 893333 fax: 01580 893399
Justcroft International Ltd. www.justcroft.com
Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom
Registered in England and Wales: 2297906
Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...