ID: 46148 Updated by: [EMAIL PROTECTED] Reported By: Yuji1 at mail dot com -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: Windows XP Pro SP2 PHP Version: 5.2.6 New Comment:
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. Previous Comments: ------------------------------------------------------------------------ [2008-09-22 09:39:04] 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:38:00] 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 08:40:14] 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 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