Observed this week...
-
return $count; // @todo cast to int
Hmm.
-
Sounds like a lot of work. I'd leave that for later, too.
-
Sounds like a lot of work. I'd leave that for later, too.
Perhaps I didn't make it clear enough.
The correct construction at this point:
return (int) $count;
Yes, it literally took more effort to write the comment than it would have done to actually do what the comment is.
-
Perhaps I didn't make it clear enough.
No, no...plenty clear. I'm exhausted just reading about what you did there.
-
Tell you what, so am I.
-
// @todo use a better language
-
Yes, it literally took more effort to write the comment than it would have done to actually do what the comment is.
Maybe
$count
is sometimesfalse
, and some code relies on it by using===
…
-
Haha yeah if it comes from the JSON parser or something it could be.
-
Maybe
$count
is sometimesfalse
, and some code relies on it by using===
…No, it's retrieved from a database query as the result of a SELECT COUNT(*) query but between MySQL and PHP's type farting, it can be a string containing a number and thus needing a typecast.
-
My guess, having seen PHP written by a shitty developer before, is that wherever the returned value is used has possibly previously relied on the ambiguous type, and casting it directly to int will break that code. So, the "todo" in this case isn't just typing six characters, but actually checking the rest of the places the value is passed for consistency.
Don't you just love how you don't have to worry about type anymore?
-
In this particular case, the code has never relied on ambiguous typing.
It was always string-as-int and type-juggled by PHP to be int but was tagged by a former dev as 'this should actually be an int' only never actually made it so.
-
Being right at the expense of being correct? Sounds like a developer to me.
-
And I do it in PHP for the win.
But in this case it was just about being 'thorough' but seriously... it took so much more effort to write the comment than it would have taken to just do it.
-
so much more effort
While it is about 200% more typing, it's not as if
Sorry, got worn out from all the extra effort.
-
```php
return $count; // @todo cast to intHmm.</blockquote> That's actually a really good WTF. Short, too the point, not hard to identify. It's almost a piece of art in its simplicity.
-
Maybe…
return $count; // @todo my left parenthesis key just stopped working, will cast to int later
-
That's what alt+numpad combinations are for.
Hey, you know what PHP needs? Trigraphs, for just such an occasion.
PHP being PHP, they'd probably be quadgraphs. Left paren keys' busted? that's OK, <?{[ does the same thing.
-
Coding challenge: cast a PHP variable as an integer without a left parenthesis.
-
-
You sir, win an internet.
Any int operation would work, of course.
It is also possible to cast to an int without using the (int) cast, by way of intval() but that requires a left parenthesis.
-
I should not win any internet with code that shitty.
Filed Under: Just saying!
Any int operation would work, of course.
You are correct. I shall change my approach:
[code]
<?php return $var / 2 * 2 + (15 / 5 - 3); // optimized Code due to Comment 3814 / 20 ?>[/code]
-
The point of it was to be bad. There should have been two prizes, one for best code given the constraints, one for worst code.
Filed under: sorry, there are no prizes
-
And then you fail because of the requirements
-
Wow.... I feel stupid now! I felt stupid for "optimizing" but ... yeah, I failed hard.
Filed Under: You get a like for spotting that mistake!
-
Have a Whoosh.
-
// @todo my left parenthesis key just stopped working, will cast to int later
Not buying it.
-
My boss frequently does type casting in sql as
1e0 * 'some stringified number'
I'm still on the fence about brilliance or 'OMG' (ignoring the fact of the original 'OMG' for storing a number as a string - another group forces it.)
-
Boo,
SELECT N1
doesn't case the number 1 as Unicode.
-
Have a Whoosh.
How's that a whoosh?! The rules said 'no left parenthesis' and that solution had some even if it was epic optimiwtfing otherwise.
-
I'm still on the fence about brilliance or 'OMG' (ignoring the fact of the original 'OMG' for storing a number as a string - another group forces it.)
Sounds like "quirk that was funny 1-3 times, 15 years ago, but nobody's told him it's not funny any more because he's the boss."
-
No, it's a legitimate thing that he does.
-
intval() returns a double (or at least it does in C). In PHP, $string * 1 may also return a double, if the string parses to a floating-point value.
$ php -r 'echo ("1.5" * 1) . "\n";' 1.5
-
<?php return $var * 1; ?>
Filed Under: Do I win anything?
I don't PHP, but what if $var was, say, 1.5?
EDIT: Sheesh...now that looks dumb showing up right after @PleegWat's post. Maybe Jeff had a point<hyeah, right>?
-
EDIT: Sheesh...now that looks dumb showing up right after @PleegWat's post. Maybe Jeff had a point?
Just pretend that Discourse didn't load that post.
-
Just pretend that Discourse didn't load that post.
At least reading his post proves my suspicion[spoiler] that I'm smarter than PHP guys[/spoiler].
-
Can't you convert the decimal number string to a numeric type, substract the number modulo 1 and convert that to string and then to int?
I don't know anything about PHP, though ...
-
Given the provided variable, $var:
$varIsZeroFix = $var > -1 && $var < 1 ? 1 : 0; // Fix for division by zero error $varIsNegative = $var < 0; $varMagnitude = $varIsNegative ? -$var : $var; $varMagnitudeFixed = $varMagnitude + $varIsZeroFix; $varMagnitudeFixedMinusOne = $varMagnitudeFixed - 1; $varIntegerFixed = $varMagnitudeFixedMinusOne % $varMagnitudeFixed + 1; $varInteger = $varIntegerFixed - $varIsZeroFix; $result = $varIsNegative ? -$varInteger : $varInteger;
-
+1, though “like” is really the wrong word here…
-
+1, though “like” is really the wrong word here…
That's probably how (int) is implemented in the PHP code base.
-
Just for reference, officially intval() returns an int in PHP (even though it probably does just call C's intval, can't be arsed to troll through the source code) but using (int) to cast truncates the number, as (int) -1.5 becomes -1.
-
So did we ever find an exact replacement? I'm too lazy to look back.
-
If you're referring to the OP... yes, and it was as implied very very early on simply just using
(int)
in the first place because that was all that was needed.
-
I meant without the parenthesis.
-
Answer is no.
@Kuro's (post 21) uses parentheses though the WTF is appreciated.
@Keith's (post 37) handles /0 and various WTFery longhand... but because%
has a higher precedence than+
does, meaningx % x + 1
nets you(x % x) + 1
= 1 which isn't right. But nice longhand WTFery.But if you want a case that does actually work, assuming truncating floats is acceptable, I only got one thing.
$var = $var << 1 >> 1;
Strings get coerced to ints, even strings that contain floats. Test code:
<?php echo 5.75 << 1 >> 1, '<br>'; echo 5.5 << 1 >> 1, '<br>'; echo 5 << 1 >> 1, '<br>'; echo '5' << 1 >> 1, '<br>'; echo '5.5' << 1 >> 1, '<br>';
-
I meant without the parenthesis.
Entertainingly (to me, since I made the opposite correction earlier) you probably meant "parentheses."
-
Entertainingly (to me, since I made the opposite correction earlier) you probably meant "parentheses."
Actually... no, see:
Coding challenge: cast a PHP variable as an integer without a left parenthesis.
-
@Arantor, thank you, it was singular on purpose
-
I know it was. I even flagged myself for pendantry in pointing this fact out with sources
Next you'll be daring me to do it without a left parenthesis or angle brackets and I'll be forced to try to think of something even more cleverer.
-
do it without punctuation
-