Re: "My truth comes out" [1]
| From: | chris h | Date: | Thu, 21 Oct 2010 14:01:44 +0000 |
| Subject: | Re: "My truth comes out" [1] | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-308933@lists.php.net to get a copy of this message | ||
settype looks like a no-go for this; per the php docs...
http://php.net/manual/en/function.settype.php
------
$bar = true; // boolean
settype($bar, "string"); // $bar is now "1" (string)
------
I think using a conditional here is the best (only?) way.
$bool = (strtolower($string)=='true')? true: false;
and if the default should be true...
$bool = (strtolower($string)=='false')? false: true;
If you you're going to use it a lot then wrap it in a function.
Also, (as was stated) preg_ is overkill here. php has a lot of nifty string
functions that should be taken advantage of before jumping onto regex.
Chris.
On Thu, Oct 21, 2010 at 6:01 AM, ash@ashleysheridan.co.uk <
ash@ashleysheridan.co.uk> wrote:
> That's as bad as an if!
>
> What about using settype() on the string? I've not tested it, but it looks
> like it should do the trick.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
> ----- Reply message -----
> From: "Russell Dias" <rus321@gmail.com>
> Date: Thu, Oct 21, 2010 10:51
> Subject: [PHP] "My truth comes out" [1]
> To: <php-general@lists.php.net>
>
> preg_match("/false/i", $string) ? false : true;
>
>
>
> On Thu, Oct 21, 2010 at 7:39 PM, Gary <php-general@garydjones.name> wrote:
> > Is there any nice way to convert a string containing either "TRUE" or
> > "FALSE" to a bool with the appropriate value? I know I can just "if
> > (strcmp..." but, as the song goes on to say "...ugly, so ugly, it's
> ugly"[1]
> >
> > Footnotes:
> > [1] "Mask", Henry Rollins
> >
> > --
> > Gary
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>