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

2010-10-22 Thread Ford, Mike
 -Original Message-
 From: Jason [mailto:networkad...@emarket2.com]
 Sent: 21 October 2010 11:45
 
 What about something simple and readable like:
 
 ($string==true) ? true : false;

... and wasteful. The above gives exactly the same result as

  ($string==true)


Cheers!

Mike

 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507 City Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.f...@leedsmet.ac.uk 
Tel: +44 113 812 4730




To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



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

2010-10-21 Thread Jason
-Original Message-
From: Gary [mailto:php-gene...@garydjones.name] 
Sent: 21 October 2010 11:39
To: php-general@lists.php.net
Subject: [PHP] Re: My truth comes out [1]

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.


What about something simple and readable like:

($string==true) ? true : false;


J


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



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

2010-10-21 Thread Russell Dias
I would stay clear of the ternary operator in nested coditions, for
obvious reasons. If it can fit in a single line and is essentially
clear in its message I dont see why not to use it.

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


 --
 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] Re: My truth comes out [1]

2010-10-21 Thread Jay Blanchard
[snip]
 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)
[/snip]

If you are worried that it takes longer for people new to the code to
read and understand then add comments! If it is about readability then
give them something to read. You could even create a function that is
more readable than the 'if' alone;

$foo = booleanString($string);

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



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

2010-10-21 Thread Robert Cummings

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...

?php

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.


Cheers,
Rob.
--
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



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

2010-10-21 Thread Jay Blanchard
[snip]
You removed the text in my post that you replied to which said I was
just wondering whether I had missed some construct in the language.
[/snip]

Yes, I read that and snipped it. I was just referring to the
readability statement. 

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



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

2010-10-21 Thread Robert Cummings

On 10-10-21 09:24 AM, Gary wrote:

Jay Blanchard wrote:

You could even create a function that is more readable than the 'if'
alone;


Of course I can. I could also record an explanation, convert it to mp3,
host it on a website, and include the URL in a comment.

You removed the text in my post that you replied to which said I was
just wondering whether I had missed some construct in the language.


I think you missed the function construct :)

Cheers,
Rob.
--
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