Let me give some context on why I'm doing this because the code isn't only stupid but the reason is also kinda stupid.
I work with a framework called Ext.js with PHP, basically, when I send the values to the PHP it becomes a String, so True -> "True". So I convert it to Boolean for a SQL Insert.
The whole situation is kinda stupid, but this workaround ain't all that big a deal. btw public sector with not much code quality required.
Most SQL servers accept a wide range of possibilities for a boolean field. If you're using placeholders in the query or a prepared statement, "true" and true should behave the same. If you're not....you should be, this is a major security vulnerability.
if it were me, I'd be representing your intentions in code a lot more and casting to Boolean in a less hand crafted way.
a simple thing to improve this would be to put all those values into an array - you can see that you kinda are already thinking this way by your variable names, you are calling them $foo_check/$bar_check. Imagine instead
If you were doing a full rewrite I'd have some way in code of mapping form responses to database tables, with e.g. an array of each column type so you could automatically validate/cast them.
Oh I've seen people do similarly dumb shit in production, so I'm not at all surprised. I once pair programmed with someone who started doing function calls with side effects inside the for loop conditions, and no code at all in the loop body. It did what it was supposed to do, but I was mind blown about the unreadability of it and couldn't even say something.
Something like
for(int i = 0; i <= doStuff(i); i++) { }
Some people just don't stop and wonder if anyone else is going to understand their code or if there are better ways to do things.
They should add functionality so you can automatically do that multiple times, like x++5 would increment x 5 times and 8=====D would check if 8 is 1 D times.
It sorta is, false == 0 is true but false === 0 is false, they're not the same type.
However the || 0 wouldn't work, that's a JavaScript-ism, in PHP boolean operators always return booleans, they can't be used in the short-circuit way they're used in JS. (So if $xx_check is false, doing $xx_check = $xx_check || 0 wouldn't change anything, since false || 0 returns false)
Ah I see, thanks for the clarification. So if they really wanted the value to be either true or 0, they'd have to go with ternary. But I doubt there is a good reason anyone would want that, so the equality check is much better.
Exactly why this sort of code is bad - it's hard to maintain and cockups like that will creep in. If you want to do the same thing 10 times, use the features of the language (loops, classes, functions) to do that in a clean way, don't copy the same line over and over.
True, but $it_check also has its own proper line right up in the first line. It's most likely that the first line was copied and pasted and edited when adding the other lines, and that one instance of $it_check = true on the fourth line was missed...and with the duplication of $dcss_check on the fourth and sixth lines there may well be another service that was missed out entirely.
I noticed this also and I just had a crazy problem last week where I had a table with a bunch of NULL values in it. My query simulator to replace NULl rows was returning zero matches on IS NULL (problem with the simulator, not my query lol), but in my attempts to fix the query, I tried entering WHERE col = 0, and I got a ton of matches... except, not for the NULl values, only all the values with strings. Luckily I didn't see the matches in the simulator and just run the query based on the number...
I have always found the 0 / false, 1 / true confusing in the way that the two can type cast between one another between languages and such as you move data around, or evaluate in a manner that indicates the variable exists in a super position of both "false" and 0 at any given time.
You should never use so much local variable names. This could be a class or an array. Then you can map the type conversion and make sure, it is the same for every member. OP wrote, that this data comes from javascript, so it is json and therefore an object, which is an array in php.
This is actually a pretty common problem, check out:
TERNARY statements but I am commenting this with the expectation that somebody else already tried to correct the typo like a real Prof. Thompson Actually.
I will 'uncooly' say php is most useful because you can get away with anything. Imagine php being your first language. And people complain about scripting languages indexing at one here and there...
He’s checking for string true, and setting to Boolean true, or 0 for some reason. It may be due to bars coming in with true false and strings or a case where he wants later to check exactly for true or 0
You meant 'ternary', but yeah. Guess who didn't understand DRY as well! Plus, true, false, 0, or what? I loathe PHP, stopped using it a decade ago and didn't miss it at all. It's a hideous douchebag pile of twisted everything and should be buried.
12
u/whoopsdang Aug 11 '22
Sometimes I think people make up bad code for karma. Who would write this? Are you a teacher?
72
u/anurat- Aug 11 '22
No need for ternary operator, just do
$xx_check = $xx_check === "true";
78
u/_PM_ME_PANGOLINS_ Aug 11 '22
Also got one of them wrong.
4
u/klapd0wn Aug 12 '22
Am I the only One noticing he assigned a bool on true, and an int on default?
25
u/itemluminouswadison Aug 11 '22
93
u/Knuffya Aug 11 '22
bitch you mean ternary?!
21
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 11 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
4
u/ItsMorbinTime69 Aug 12 '22
Line 1294 whyyyyyy
6
u/nissen_96 Aug 11 '22
r/BoneAppleTea
2
u/--var Aug 12 '22
An if statement is valid without a then condition? You learn something new every day. But also why?
I think some people are also missing that php is loosley typed.
"'true" (string) !== true (boolean) !== 1 (number that is loosely equivalent to true)
So if you wanted the string "true" to convert to the boolean true, this isn't that weird.
Also I noticed OP mention sql in another comment. Consider using
CAST(value AS bit), which should return either a 1 or a 0.2
u/Wafflelisk Aug 12 '22
Why did you have to go and make PHP rougher?
2
u/HerrMatthew Aug 12 '22
Or ternary statements in gameral
2
u/[deleted] Aug 12 '22
You should never use so much local variable names. This could be a class or an array. Then you can map the type conversion and make sure, it is the same for every member. OP wrote, that this data comes from javascript, so it is json and therefore an object, which is an array in php.
This is actually a pretty common problem, check out:
https://github.com/rutek/dataclass
https://github.com/rakit/validation
https://symfony.com/doc/current/components/validator.html
2
u/developer-ramen Sep 14 '22
TERNARY statements but I am commenting this with the expectation that somebody else already tried to correct the typo like a real Prof. Thompson Actually.
2
u/[deleted] Oct 04 '22
Or booleans for that matter
1
u/Double_Ad_2824 Aug 12 '22
Sometimes I just can't.
1
u/RyanNerd shameless Aug 12 '22
How many lines of this bad code are there? Just this small sample made my eyes water.
1
u/dhenfie Aug 12 '22
no need to use if to create ternary operator. $it_check = ($it_check === "true") ? true : 0;
1
u/real_psymansays Aug 12 '22
no me gusta
1
u/[deleted] Aug 12 '22
I will 'uncooly' say php is most useful because you can get away with anything. Imagine php being your first language. And people complain about scripting languages indexing at one here and there...
1
u/Valscher Aug 12 '22
most sane PHP developer be like
1
u/geilt Aug 12 '22
He’s checking for string true, and setting to Boolean true, or 0 for some reason. It may be due to bars coming in with true false and strings or a case where he wants later to check exactly for true or 0
1
u/mousepotatodoesstuff Aug 15 '22
is that a ternary operator lodged inside an useless if statement?
...why?
1
u/loganpt77 Aug 18 '22
First no one decorates, everyone learns from some shit from a frequent mistake in php
1
u/bravopapa99 Oct 30 '22
You meant 'ternary', but yeah. Guess who didn't understand DRY as well! Plus, true, false, 0, or what? I loathe PHP, stopped using it a decade ago and didn't miss it at all. It's a hideous douchebag pile of twisted everything and should be buried.
487
u/[deleted] Aug 11 '22
[removed] — view removed comment