Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
C0D4669025yPhp: the one language you have a magnitude of ways of doing the same thing - because reasons.
-
and in which language exactly is the boolean value of a non empty string not true?
-
C0D4669025y@myss because 0 is falsey.
@Jilano the kind that's seen way to many things to not get surprised anymore. -
@myss welcome to dynamically typed languages where puking out programs is faster but debugging becomes harder.
In your case, if you expect the value to be (string) "true" or "false", then the best practise would be to use
$dataValue = $dataValue === "true";
If you accept all kinds of value types for $dataValue beforehand, then I'd say it's a mess.
Chances are you use PHP 7+ which comes with type-hinting. If $dataValue is a function parameter, you can decorate it with a type so PHP will throw an exeption on invalid function call.
When you get $dataValue from some user input, then you should do validation there. -
arekxv10525y@myss You can always use json_decode as a one liner. Sure it's not fast but it parses all of the cases for truthy / falsy result 😁
-
@erroronline1 don't apologise. Many have fallen in this trap hole.
Btw. don't use boolval / intval and so on.
(int)
(bool)
...
are ur friend.
Related Rants
PHP are you freaking kidding me right now? Why are you forcing me to write ugly and meaningless code like this?
Today I just learned that boolval("false") will return true.
I'd deffo expect this from casting operators, but not from a function which even has val inside of its name.
What purpose is to have functions like these in language if they just serve as plain wrapper for casting operators
rant
casting
php
bool