Php convert string to boolean
How can I convert string to
it returns,
but it should be
Neuron 4,5784 gold badges32 silver badges53 bronze badges asked Sep 7, 2011 at 15:54
2 This method was posted by @lauthiamkok in the comments. I'm posting it here as an answer to call more attention to it. Depending on your needs, you should consider using
answered Feb 25, 2013 at 20:13
BradBrad 154k48 gold badges341 silver badges510 bronze badges 4 Strings always evaluate to boolean true unless they have a value that's considered "empty" by
PHP (taken from the documentation for
If you need to set a boolean based on the text value of a string, then you'll need to check for the presence or otherwise of that value.
EDIT: the above code is intended for clarity of understanding. In actual use the following code may be more appropriate:
or maybe use of the
answered Sep 7, 2011 at 15:55
GordonMGordonM 30.5k15 gold badges86 silver badges129 bronze badges 10 The String
so if you do:
or
in both cases
Nhan 3,5486 gold badges31 silver badges37 bronze badges answered Sep 7, 2011 at 16:04
wosiswosis 1,16910 silver badges14 bronze badges 1 When working with JSON, I had to send a Boolean value via
In the code above, my Boolean was converted into a JSON string. To overcome this, you can decode the string using
(This should normally work with Boolean values converted to string and sent to the server also by other means, i.e., other than using JSON.) answered Nov 2, 2014 at 19:27
0 you can use json_decode to decode that boolean
answered Jul 27, 2016 at 14:44
isnvi23h4isnvi23h4 1,8081 gold badge25 silver badges43 bronze badges 2
It
handles all possible variants of
answered Jun 20, 2017 at 10:31
mrdedmrded 4,2442 gold badges32 silver badges34 bronze badges 2 If your "boolean" variable comes from a global array such as $_POST and $_GET, you can use Example for POST:
If your "boolean" variable comes from other source you can use Example:
answered Nov 23, 2017 at 9:03
SandroMarquesSandroMarques 5,2571 gold badge39 silver badges40 bronze badges the easiest thing to do is this:
Doing it this way, you can loop through a series of 'true', 'TRUE', 'false' or 'FALSE' and get the string value to a boolean. answered Apr 19, 2016 at 3:07
1
You must specify FILTER_NULL_ON_FAILURE otherwise you'll get always false even if $string contains something else. answered Jan 15, 2020 at 22:58
ybenhssaienybenhssaien 2,7171 gold badge8 silver badges12 bronze badges Other answers are over complicating things. This question is simply logic question. Just get your statement right.
Now your answer will be either
I have to note that answered Sep 3, 2013 at 16:49
kaiserkaiser 21.1k16 gold badges87 silver badges106 bronze badges 0
or
answered Aug 20, 2014 at 14:24
DmitryDmitry 1,0752 gold badges11 silver badges19 bronze badges I do it in a way that will cast any case insensitive version of the string "false" to the boolean FALSE, but will behave using the normal php casting rules for all other strings. I think this is the best way to prevent unexpected behavior.
Or as a function:
answered Mar 24, 2015 at 18:12
Syntax ErrorSyntax Error 4,3752 gold badges21 silver badges33 bronze badges The answer by @GordonM is good. But it would fail if the Extending his answer, I'd use:
answered Feb 10, 2015 at 9:41
Ema4rlEma4rl 5771 gold badge6 silver badges17 bronze badges 0 I was getting confused with wordpress shortcode attributes, I decided to write a custom function to handle all possibilities. maybe it's useful for someone:
answered Mar 13, 2016 at 5:30
2
You have to do it manually answered Sep 15, 2021 at 13:47
pennypenny 133 bronze badges You can use the settype method too!
answered May 11, 2014 at 9:56
NaiNai 4003 silver badges15 bronze badges 1 A simple way is to check against an array of values that you consider true.
answered Feb 16, 2017 at 14:20
Edited to show a working solution using
answered Jul 29, 2015 at 22:12
JSGJSG 3601 gold badge4 silver badges12 bronze badges 2 You should be able to cast to a boolean using (bool) but I'm not sure without checking whether this works on the strings "true" and "false". This might be worth a pop though
It is worth knowing that the following will evaluate to the boolean False when put inside
Everytyhing else will evaluate to true. As descried here: http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting answered Sep 7, 2011 at 16:05
dougajmcdonalddougajmcdonald 18.5k11 gold badges53 silver badges89 bronze badges 3 |