#46148 [Com]: ((strlen($someString) <= $someInteger) ? someStatement : someStatement) fails
ID: 46148
Comment by: tf at oopsilon dot com
Reported By: Yuji1 at mail dot com
Status: Open
Bug Type: Scripting Engine problem
Operating System: Windows XP Pro SP2
PHP Version: 5.2.6
New Comment:
Firstly, your ternary's the wrong way up. You're asking whether a
string is less than 300 chars long, and if it is getting the first 300
chars.
Secondly, the ternary has a higher precedence than comparison: ($foo <
300 ? 1 : 2), for example, is ($foo < (300?1:2)).
Your statement should probably be:
(strlen($foo)<=300)?$foo:substr($foo,0,300)
Previous Comments:
------------------------------------------------------------------------
[2008-09-22 02:40:33] Yuji1 at mail dot com
Description:
------------
(somefunction($someObject) someOperator $someObject) ? someStatement :
someStatement
and
((somefunction($someObject) someOperator $someObject) ? someStatement :
someStatement)
fail
but
((somefunction($someObject someOperator $someObject) ? someStatement :
someStatement)
works. :/
Reproduce code:
---------------
$queryResults was an array of strings, [1] being a string. It had
length, yes.
I used:
$queryResults[1] = (strlen($queryResults[1]) <= 300) ?
substr($queryResults[1], 0, 300) : $queryResults[1]);
It however fails.
This fails too:
$queryResults[1] = strlen($queryResults[1]) <= 300 ?
substr($queryResults[1], 0, 300) : $queryResults[1];
This works:
$queryResults[1] = (strlen($queryResults[1] <= 300) ?
substr($queryResults[1], 0, 300) : $queryResults[1]);
Expected result:
----------------
I expected it to well, shorten the length to 300.
Actual result:
--------------
Completely fails. In fact, without the (strlen($queryResults[1] <= 300)
being that way, it does this (example is of a database query, and PER
ROW IT PRINTS OUT THE FOLLOWING STRING, THE POST STRING FOR NEWS):
First post:
StringStringStringString
Second post:
StringStringStringStringStringStringString
Third post:
StringStringStringStringStringStringStringStringStringString
Etc.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46148&edit=1
Thread (6 messages)