On 10-10-21 06:39 AM, Gary wrote:
Russell Dias wrote:

preg_match("/false/i", $string) ? false : true;

| $foo = (strcmp('true', $string) == 0);
works as well :) I don't like those kinds of things for this purpose
because it takes longer for people new to the code to read and
understand it than, say
| (boolean) $string;
(which in my case does a pretty meaningless conversion)

No, I was just wondering whether I had missed some construct in the
language that might the conversion for me, and be obvious to
PHP-heads. No problem that there doesn't seem to be.

This is why in PHP you can create functions and libraries...


function string2bool( $string )
    return preg_match( '#^(1|true|enabled|on|active)$#i', $string );


I used a preg_match because I have my doubts 2.5 calls to strcasecmp() would be faster... but who knows... the above example also happens to be very succinct* if not optimal :)

* Yes, I know, nobody asked for the other options, but I like the flexibility and many ini files do too.

E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to