Re: [PHP] My truth comes out [1]

2010-10-21 Thread Russell Dias
preg_match(/false/i, $string) ? false : true;



On Thu, Oct 21, 2010 at 7:39 PM, Gary php-gene...@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



Re: [PHP] My truth comes out [1]

2010-10-21 Thread a...@ashleysheridan.co.uk
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 rus...@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-gene...@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



Re: [PHP] My truth comes out [1]

2010-10-21 Thread Russell Dias
I'm curious to know why 'thats as bad as an if' ?

It's simple, concise, understandble and does the job. So, I'm curious
to know why exactly its bad?

On Thu, Oct 21, 2010 at 8:01 PM, a...@ashleysheridan.co.uk
a...@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 rus...@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-gene...@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



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



Re: [PHP] My truth comes out [1]

2010-10-21 Thread a...@ashleysheridan.co.uk
Because it is an if statement, just in a different form, and preg_match is more 
computational expensive than a direct string comparison.

Thanks,
Ash
http://www.ashleysheridan.co.uk

- Reply message -
From: Russell Dias rus...@gmail.com
Date: Thu, Oct 21, 2010 11:03
Subject: [PHP] My truth comes out [1]
To: a...@ashleysheridan.co.uk a...@ashleysheridan.co.uk
Cc: php-general@lists.php.net


I'm curious to know why 'thats as bad as an if' ?

It's simple, concise, understandble and does the job. So, I'm curious
to know why exactly its bad?

On Thu, Oct 21, 2010 at 8:01 PM, a...@ashleysheridan.co.uk
a...@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 rus...@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-gene...@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




Re: [PHP] My truth comes out [1]

2010-10-21 Thread Russell Dias
Whats wrong with an if *construct* ? I understand that the ternary
operator is another way of doing it. Using it in such a small scenario
is perfectly justifiable. Its ridiculous to use in more complicated
scenarious because of readability issues.

Direct string comparison? You suggested yet another function call,
settype I believe. Please do not make benchmark claims without any
data.


On Thu, Oct 21, 2010 at 8:11 PM, a...@ashleysheridan.co.uk
a...@ashleysheridan.co.uk wrote:
 Because it is an if statement, just in a different form, and preg_match is 
 more computational expensive than a direct string comparison.

 Thanks,
 Ash
 http://www.ashleysheridan.co.uk

 - Reply message -
 From: Russell Dias rus...@gmail.com
 Date: Thu, Oct 21, 2010 11:03
 Subject: [PHP] My truth comes out [1]
 To: a...@ashleysheridan.co.uk a...@ashleysheridan.co.uk
 Cc: php-general@lists.php.net


 I'm curious to know why 'thats as bad as an if' ?

 It's simple, concise, understandble and does the job. So, I'm curious
 to know why exactly its bad?

 On Thu, Oct 21, 2010 at 8:01 PM, a...@ashleysheridan.co.uk
 a...@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 rus...@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-gene...@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




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



Re: [PHP] My truth comes out [1]

2010-10-21 Thread Alexander Schrijver
On Thu, Oct 21, 2010 at 08:15:40PM +1000, Russell Dias wrote:
 Whats wrong with an if *construct* ? I understand that the ternary
 operator is another way of doing it. Using it in such a small scenario
 is perfectly justifiable. Its ridiculous to use in more complicated
 scenarious because of readability issues.

It depends on what the output is. If it actually is either TRUE or FALSE
then it would be fine.

 
 Direct string comparison? You suggested yet another function call,
 settype I believe. Please do not make benchmark claims without any
 data.

You use preg_match which loads the PCRE library which obviously is overkill 
here.

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



Re: [PHP] My truth comes out [1]

2010-10-21 Thread Richard Quadling
On 21 October 2010 10:39, Gary php-gene...@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

[1] Ugh! Ugly Houses, Chumbawamba.



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

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



Re: [PHP] My truth comes out [1]

2010-10-21 Thread Richard Quadling
On 21 October 2010 10:39, Gary php-gene...@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]

What is the response when the string is NOT TRUE or FALSE?



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

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



Re: [PHP] My truth comes out [1]

2010-10-21 Thread Richard Quadling
On 21 October 2010 12:42, Richard Quadling rquadl...@gmail.com wrote:
 On 21 October 2010 10:39, Gary php-gene...@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]

 What is the response when the string is NOT TRUE or FALSE?

?php
$s_OriginalValue = 'FALSE';
$b_Test = in_array($s_Test = strtolower($s_OriginalValue),
array('true', 'false')) ? !!strcmp($s_Test, 'false') : null;
var_dump($b_Test);

$s_OriginalValue = 'TRUE';
$b_Test = in_array($s_Test = strtolower($s_OriginalValue),
array('true', 'false')) ? !!strcmp($s_Test, 'false') : null;
var_dump($b_Test);


$s_OriginalValue = 'GARBAGE';
$b_Test = in_array($s_Test = strtolower($s_OriginalValue),
array('true', 'false')) ? !!strcmp($s_Test, 'false') : null;
var_dump($b_Test);
?

outputs ...

bool(false)
bool(true)
NULL

But, really? What's wrong with if()?



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

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



Re: [PHP] My truth comes out [1]

2010-10-21 Thread Midhun Girish
Really interesting thread :D . Why go for other methods when you can
do it easily using if Is this an exercise or something?


Midhun Girish
Development Lead
MobAlive Technologies
Trivandrum


Re: [PHP] My truth comes out [1]

2010-10-21 Thread chris h
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, a...@ashleysheridan.co.uk 
a...@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 rus...@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-gene...@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




Re: [PHP] My truth comes out [1]

2010-10-21 Thread Adam Richardson
On Thu, Oct 21, 2010 at 5:39 AM, Gary php-gene...@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


You don't need an 'if', and you don't need to add a function call to the
stack:

$value_to_convert = 'TRUE'; // tested with true, but works with false, too
$bool = ('TRUE' == $value_to_convert);

This does require that you've sanity checked the value before hand, as your
either TRUE or FALSE statement implies.

Adam

-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com