php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46148 ((strlen($someString) <= $someInteger) ? someStatement : someStatement) fails
Submitted: 2008-09-22 02:40 UTC Modified: 2008-09-22 09:53 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: Yuji1 at mail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.6 OS: Windows XP Pro SP2
Private report: No CVE-ID: None
 [2008-09-22 02:40 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-22 08:40 UTC] tf at oopsilon dot com
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)
 [2008-09-22 09:38 UTC] Yuji1 at mail dot com
Compare please, the following:

Your example: (strlen($foo)<=300)?$foo:substr($foo,0,300)
My example: ((strlen($foo) <= 300) ? substr($foo[1], 0, 300) : $foo[1])

...So like, tell me where I screwed up then?
 [2008-09-22 09:39 UTC] Yuji1 at mail dot com
Hell, even (strlen($foo) <= 300) ? substr($foo[1], 0, 300) : $foo[1] How is it wrong?
 [2008-09-22 09:51 UTC] mkoppanen@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

http://php.net/manual/en/language.operators.comparison.php

You are checking if the string length is less or equal to 300 characters and in case it is you take 300 first characters.
 [2008-09-22 09:53 UTC] Yuji1 at mail dot com
I just realized. Apologies, I haven't had my spot of tea today. Remove post? >///>;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 21:01:30 2024 UTC