php-general Digest 21 Oct 2010 12:51:29 -0000 Issue 6999
Topics (messages 308902 through 308928):
Re: Returning results
308902 by: David McGlone
308903 by: Bastien
308904 by: Daniel P. Brown
Re: Independent Contractor Suggestions
308905 by: musicdev.gmail.com
308907 by: Jason Pruim
Re: Possible foreach bug; seeking advice to isolate the problem
308906 by: Tommy Pham
I need some thoughts on code duplication and separation
308908 by: Rico Secada
308909 by: David McGlone
308910 by: Tommy Pham
308911 by: Peter Lind
308912 by: [email protected]
"My truth comes out" [1]
308913 by: Gary
308914 by: Russell Dias
308915 by: [email protected]
308916 by: Russell Dias
308917 by: [email protected]
308918 by: Russell Dias
308920 by: Gary
308921 by: Jason
308922 by: Russell Dias
308923 by: Alexander Schrijver
308924 by: Jay Blanchard
308925 by: Richard Quadling
308926 by: Richard Quadling
308927 by: Richard Quadling
308928 by: Midhun Girish
Re: Weird Behavior
308919 by: Alexander Schrijver
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
On Wed, 2010-10-20 at 20:11 -0400, Bastien wrote:
>
> On 2010-10-20, at 7:59 PM, David McGlone <[email protected]> wrote:
>
> > Hi everyone, it's me again :-/
> >
> > I'm having a whole lot of trouble figuring out how to return a result
> > from a query such as in this code example:
> >
> > function LearnIt(){
> > $query = "SELECT learn_id FROM mydatabase ";
> > $result = mysql_query($query);
> > return $result;
> > }
> >
> > I already achieved a connection to the database with no problem, to
> > verify I wrote this query also:
> >
> > function GetMeARow(){
> >
> > $query = "SELECT learn_id FROM mydatabase ";
> > $result = mysql_query($query);
> > while($row = mysql_fetch_array($result)){
> > echo $row[learn_id];
> > }
> >
> > With the latter, It works as expected, echoing the 2 rows I have in the
> > DB, but I'm trying to understand the method used in the former.
> >
>
>
> David, the $result variable/object is the handle to the result data set,
> not the resulting data. You would still need to access the data set via
> the Mysql_* set of functions like Mysql-fetch_array or Mysql_result like
> you did in the second example
HaHa! How burnt out does one have to be to overlook this a million times
and still not get it?
Thanks I see now :-)
--
Blessings
David M.
--- End Message ---
--- Begin Message ---
Yeah, it's got a few downsides!
Bastien Koert
905-904-0334
Sent from my iPhone
On 2010-10-20, at 8:27 PM, "Daniel P. Brown" <[email protected]> wrote:
> On Wed, Oct 20, 2010 at 20:11, Bastien <[email protected]> wrote:
>>
>> David, the $result variable/object is the handle to the result data set, not
>> the resulting data. You would still need to access the data set via the
>> Mysql_* set of functions like Mysql-fetch_array or Mysql_result like you did
>> in the second example
>
> Must've been typing from the iPhone here. ;-P
>
> --
> </Daniel P. Brown>
> Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
> (866-) 725-4321
> http://www.parasane.net/
--- End Message ---
--- Begin Message ---
On Wed, Oct 20, 2010 at 21:04, Bastien <[email protected]> wrote:
> Yeah, it's got a few downsides!
The next app for the iPhone should be a modification to the
spell-check to verify PHP functions in emails.
--
</Daniel P. Brown>
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/
--- End Message ---
--- Begin Message ---
Thanks Jason, Alexis and Kris. I appreciate the time taken to respond.
Kris, you definitely touched upon a few of my current concerns. Gladly I
have not quoted the client at $20 p/h. I can definitely confirm that the
characteristics you described are already taking place (drama and quite
demanding). I definitely like the suggestion from yourself, Jason and
Alexis which is to work on the bare minimum and cost extra development
accordingly...and, not under-cost my work for fear or consideration to the
client's state in terms of budget.
Kris, I greatly appreciate the offer for your SOW/contract sample. Please
send it my way when you have a chance.
Thanks again for all the great responses and excellent suggestions.
Regards,
Ervin
--------------------------------------------------
From: "Kris Craig" <[email protected]>
Sent: Wednesday, October 20, 2010 8:47 PM
To: <[email protected]>
Subject: [PHP] Re: Independent Contractor Suggestions
Hi musicdev,
There are a couple issues I think need to be addressed with what youd
described. First and foremost, $20/hr is considerably below the going
rate for PHP work, especially for projects as large as the one you're
talking about.
I used to do freelance PHP work for about 5 years before I went to
work for Microsoft, and when I originally started I was just charging
about 20 bucks an hour as well. It proved to be a disaster. Contrary
to what common sense might tell you, I've found that lower-budget
clients always produce the most drama, are the most demanding, and
least reliable when it comes to paying the invoices on-time.
Furthermore, larger clients will generally ignore you in favor of
developers who charge more, the mindset being that, if you're charging
such a low amount, the quality of your work probably isn't that good.
I did a lot better after I started charging $100/hr for my work. A
*lot* better! This was after my research showed that PHP development
firms generally charge a minimum of $80/hr for PHP work, and can go as
high as $200/hr. So if you're going at $20/hr, the companies with
deep pockets probably won't take you seriously, and the clients you do
get will be the ones who want a ton of work done but don't have the
budget available to make it worth your while. Those are the clients
who will take advantage of you if you're not careful.
With your specific dilemma, if you already quoted $20/hr then of
course you should honor that. However, I would strongly suggest that
you do *not* allow the client to
determine the cost of the deliverables after the work has already been
done. Nine times out of ten you will get taken advantage of, because
the client knows you already did the work which means they have all
the leverage. After all, ten cents on the dollar is better than zero,
right? You don't want to put yourself in that situation.
Instead, here's what I would suggest: Work with the client to
determine a bare minimum of work that would need to be done for the
"first phase." Based off the $20/hr you quoted, determine how many
hours that will take and give them a quote. Then, develop a statement
of work document (that both you and the client will have to sign) for
that phase, outlining specifically what work will be done and break it
up into deliverable pieces. Upon completion of each "chunk" of work,
a percentage of the total quote will be due. This ensures that you
get paid for your work and that the client receives the work s/he is
paying for. It also breaks-up the payment schedule so that it's a bit
easier on the client's budget. If the client runs out of money midway
through or whatever, you simply suspend work until they get more funds
and then resume where you left off. I've found this model to be the
most successful on projects like this.
I hope this helps. If you like, I'd be happy to email you a sample
copy of the SOW/contract I've used with past clients.
--Kris
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--- End Message ---
--- Begin Message ---
On Oct 20, 2010, at 8:47 PM, Kris Craig wrote:
I hope this helps. If you like, I'd be happy to email you a sample
copy of the SOW/contract I've used with past clients.
Hey Kris,
If you don't mind I would love a copy my self! I have started doing
some freelance work and I'm always looking for info that I can use :)
Jason Pruim
--- End Message ---
--- Begin Message ---
On Wed, Oct 20, 2010 at 4:16 PM, David Harkness
<[email protected]> wrote:
> On Wed, Oct 20, 2010 at 11:08 AM, Tommy Pham <[email protected]> wrote:
>>
>> hmm.. About 8-9 years ago I did a project where I used the reference
>> in a foreach loop as the OP. unset not only remove the variable but
>> also the value in the array. I tried several methods at that time and
>> ended up assigning null to get what I wanted without modifying the
>> array. I'll have to dig up that project later to see.
>
> To be thorough, I ran the same test as above using unset(), and it correctly
> clears the reference and leaves $x in tact.
>
> $x = 5;
> $y = &$x;
> unset($y);
> var_dump($x);
> --> 5
> var_dump($y);
> --> NULL
>
> It's quite possible this behavior was changed along the way. I started using
> PHP last year with 5.2.
>
> David
>
Hi David,
IIRC, I think at that time, PHP5 was still fresh. I remember a lot of
the open source projects were written with PHP4 compatibility. I
don't remember if that particular project of mine was written for PHP4
or 5. Any case, changes are good to an extent :). I also did confirm
prior to your previous email, right after Gary showed me that quote
from the manual. I'll have to remember that from now on :).
Thanks,
Tommy
--- End Message ---
--- Begin Message ---
Hi.
I am working on a small system where I am both trying to avoid code
duplication and at the same time I am trying to keep the presentation
logic separated from the application logic.
I am using sessions and are avoiding "headers already sent" problem by
keeping the HTML out of the application.
For example, I would like to have a common header.php file, but it is
difficult to create this since one file needs to have some specific
Javascript located in the <head> </head> tags, but the other files
doesn't need this.
Another file needs to have a specific "onload" call in the <body> tag,
while yet another file also needs to have an "onload" call, but with
different attributes.
I have been looking around in other systems to see what kinds of
solutions are being used - as inspiration.
I have been thinking about the following solutions:
1. Create only ONE header.php file that contains a lot of conditionals
depending on what file is including it - the output of HTML/Javascript
changes.
I believe this would turn into a very ugly hack. Difficult to maintain.
2. Create a HTML generating class with a set of methods that each
contains an adequate amount of parameters. Each method maintains its
own HTML tag. For example, docType($type) would generate the doctype
specification.
I believe this is a "cleaner" solution, but the problem with code
duplication isn't avoided.
Some of the presentation logic contains conditionals and the HTML
changes when the conditional changes, hence the header content changes,
but the <doctype>, <html>, and <head> doesn't necessarily change and
they would get duplicated a couple of times in some files.
3. Avoid the problem all together, use output buffering and completely
forget about separation between application and presentation.
I hope I make sense.
Any thoughts on these kinds of problems?
Best regards.
--- End Message ---
--- Begin Message ---
On Thu, 2010-10-21 at 04:05 +0200, Rico Secada wrote:
> Hi.
>
> I am working on a small system where I am both trying to avoid code
> duplication and at the same time I am trying to keep the presentation
> logic separated from the application logic.
>
> I am using sessions and are avoiding "headers already sent" problem by
> keeping the HTML out of the application.
>
> For example, I would like to have a common header.php file, but it is
> difficult to create this since one file needs to have some specific
> Javascript located in the <head> </head> tags, but the other files
> doesn't need this.
>
> Another file needs to have a specific "onload" call in the <body> tag,
> while yet another file also needs to have an "onload" call, but with
> different attributes.
>
> I have been looking around in other systems to see what kinds of
> solutions are being used - as inspiration.
>
> I have been thinking about the following solutions:
>
> 1. Create only ONE header.php file that contains a lot of conditionals
> depending on what file is including it - the output of HTML/Javascript
> changes.
>
> I believe this would turn into a very ugly hack. Difficult to maintain.
>
> 2. Create a HTML generating class with a set of methods that each
> contains an adequate amount of parameters. Each method maintains its
> own HTML tag. For example, docType($type) would generate the doctype
> specification.
>
> I believe this is a "cleaner" solution, but the problem with code
> duplication isn't avoided.
>
> Some of the presentation logic contains conditionals and the HTML
> changes when the conditional changes, hence the header content changes,
> but the <doctype>, <html>, and <head> doesn't necessarily change and
> they would get duplicated a couple of times in some files.
>
> 3. Avoid the problem all together, use output buffering and completely
> forget about separation between application and presentation.
>
> I hope I make sense.
>
> Any thoughts on these kinds of problems?
sounds like a job for smarty http://www.smarty.net/
I've used smarty and it's nice.
--
Blessings
David M.
--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: David McGlone [mailto:[email protected]]
> Sent: Wednesday, October 20, 2010 7:59 PM
> To: [email protected]
> Subject: Re: [PHP] I need some thoughts on code duplication and separation
>
> On Thu, 2010-10-21 at 04:05 +0200, Rico Secada wrote:
> > Hi.
> >
> > I am working on a small system where I am both trying to avoid code
> > duplication and at the same time I am trying to keep the presentation
> > logic separated from the application logic.
> >
> > I am using sessions and are avoiding "headers already sent" problem by
> > keeping the HTML out of the application.
> >
> > For example, I would like to have a common header.php file, but it is
> > difficult to create this since one file needs to have some specific
> > Javascript located in the <head> </head> tags, but the other files
> > doesn't need this.
> >
> > Another file needs to have a specific "onload" call in the <body> tag,
> > while yet another file also needs to have an "onload" call, but with
> > different attributes.
> >
> > I have been looking around in other systems to see what kinds of
> > solutions are being used - as inspiration.
> >
> > I have been thinking about the following solutions:
> >
> > 1. Create only ONE header.php file that contains a lot of conditionals
> > depending on what file is including it - the output of HTML/Javascript
> > changes.
> >
> > I believe this would turn into a very ugly hack. Difficult to maintain.
> >
> > 2. Create a HTML generating class with a set of methods that each
> > contains an adequate amount of parameters. Each method maintains its
> > own HTML tag. For example, docType($type) would generate the doctype
> > specification.
> >
> > I believe this is a "cleaner" solution, but the problem with code
> > duplication isn't avoided.
> >
> > Some of the presentation logic contains conditionals and the HTML
> > changes when the conditional changes, hence the header content
> > changes, but the <doctype>, <html>, and <head> doesn't necessarily
> > change and they would get duplicated a couple of times in some files.
> >
> > 3. Avoid the problem all together, use output buffering and completely
> > forget about separation between application and presentation.
> >
> > I hope I make sense.
> >
> > Any thoughts on these kinds of problems?
>
> sounds like a job for smarty http://www.smarty.net/
>
> I've used smarty and it's nice.
>
> --
> Blessings
> David M.
>
>
Thanks David! I was looking for something like that too!
Regards,
Tommy
--- End Message ---
--- Begin Message ---
On 21 October 2010 04:59, David McGlone <[email protected]> wrote:
> On Thu, 2010-10-21 at 04:05 +0200, Rico Secada wrote:
>> Hi.
>>
>> I am working on a small system where I am both trying to avoid code
>> duplication and at the same time I am trying to keep the presentation
>> logic separated from the application logic.
>>
>> I am using sessions and are avoiding "headers already sent" problem by
>> keeping the HTML out of the application.
>>
>> For example, I would like to have a common header.php file, but it is
>> difficult to create this since one file needs to have some specific
>> Javascript located in the <head> </head> tags, but the other files
>> doesn't need this.
>>
>> Another file needs to have a specific "onload" call in the <body> tag,
>> while yet another file also needs to have an "onload" call, but with
>> different attributes.
>>
>> I have been looking around in other systems to see what kinds of
>> solutions are being used - as inspiration.
>>
>> I have been thinking about the following solutions:
>>
>> 1. Create only ONE header.php file that contains a lot of conditionals
>> depending on what file is including it - the output of HTML/Javascript
>> changes.
>>
>> I believe this would turn into a very ugly hack. Difficult to maintain.
>>
>> 2. Create a HTML generating class with a set of methods that each
>> contains an adequate amount of parameters. Each method maintains its
>> own HTML tag. For example, docType($type) would generate the doctype
>> specification.
>>
>> I believe this is a "cleaner" solution, but the problem with code
>> duplication isn't avoided.
>>
>> Some of the presentation logic contains conditionals and the HTML
>> changes when the conditional changes, hence the header content changes,
>> but the <doctype>, <html>, and <head> doesn't necessarily change and
>> they would get duplicated a couple of times in some files.
>>
>> 3. Avoid the problem all together, use output buffering and completely
>> forget about separation between application and presentation.
>>
>> I hope I make sense.
>>
>> Any thoughts on these kinds of problems?
>
> sounds like a job for smarty http://www.smarty.net/
>
> I've used smarty and it's nice.
>
How does Smarty solve this problem? Just curious.
Regards
Peter
--
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype>
--- End Message ---
--- Begin Message ---
Bit late to this one, but what about an mvc framework like codeigniter? It's
perfectly acceptable to use if statements and loops in the view, which would
solve your problem. You can set a trigger variable in the controller, and in
the header view, check to see if its set and output your javascript lines as
appropriate.
Thanks,
Ash
http://www.ashleysheridan.co.uk
----- Reply message -----
From: "Peter Lind" <[email protected]>
Date: Thu, Oct 21, 2010 07:01
Subject: [PHP] I need some thoughts on code duplication and separation
To: "David McGlone" <[email protected]>
Cc: <[email protected]>
On 21 October 2010 04:59, David McGlone <[email protected]> wrote:
> On Thu, 2010-10-21 at 04:05 +0200, Rico Secada wrote:
>> Hi.
>>
>> I am working on a small system where I am both trying to avoid code
>> duplication and at the same time I am trying to keep the presentation
>> logic separated from the application logic.
>>
>> I am using sessions and are avoiding "headers already sent" problem by
>> keeping the HTML out of the application.
>>
>> For example, I would like to have a common header.php file, but it is
>> difficult to create this since one file needs to have some specific
>> Javascript located in the <head> </head> tags, but the other files
>> doesn't need this.
>>
>> Another file needs to have a specific "onload" call in the <body> tag,
>> while yet another file also needs to have an "onload" call, but with
>> different attributes.
>>
>> I have been looking around in other systems to see what kinds of
>> solutions are being used - as inspiration.
>>
>> I have been thinking about the following solutions:
>>
>> 1. Create only ONE header.php file that contains a lot of conditionals
>> depending on what file is including it - the output of HTML/Javascript
>> changes.
>>
>> I believe this would turn into a very ugly hack. Difficult to maintain.
>>
>> 2. Create a HTML generating class with a set of methods that each
>> contains an adequate amount of parameters. Each method maintains its
>> own HTML tag. For example, docType($type) would generate the doctype
>> specification.
>>
>> I believe this is a "cleaner" solution, but the problem with code
>> duplication isn't avoided.
>>
>> Some of the presentation logic contains conditionals and the HTML
>> changes when the conditional changes, hence the header content changes,
>> but the <doctype>, <html>, and <head> doesn't necessarily change and
>> they would get duplicated a couple of times in some files.
>>
>> 3. Avoid the problem all together, use output buffering and completely
>> forget about separation between application and presentation.
>>
>> I hope I make sense.
>>
>> Any thoughts on these kinds of problems?
>
> sounds like a job for smarty http://www.smarty.net/
>
> I've used smarty and it's nice.
>
How does Smarty solve this problem? Just curious.
Regards
Peter
--
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
preg_match("/false/i", $string) ? false : true;
On Thu, Oct 21, 2010 at 7:39 PM, Gary <[email protected]> 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
>
>
--- End Message ---
--- Begin Message ---
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" <[email protected]>
Date: Thu, Oct 21, 2010 10:51
Subject: [PHP] "My truth comes out" [1]
To: <[email protected]>
preg_match("/false/i", $string) ? false : true;
On Thu, Oct 21, 2010 at 7:39 PM, Gary <[email protected]> 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
--- End Message ---
--- Begin Message ---
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, [email protected]
<[email protected]> 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" <[email protected]>
> Date: Thu, Oct 21, 2010 10:51
> Subject: [PHP] "My truth comes out" [1]
> To: <[email protected]>
>
> preg_match("/false/i", $string) ? false : true;
>
>
>
> On Thu, Oct 21, 2010 at 7:39 PM, Gary <[email protected]> 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
>
>
--- End Message ---
--- Begin Message ---
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" <[email protected]>
Date: Thu, Oct 21, 2010 11:03
Subject: [PHP] "My truth comes out" [1]
To: "[email protected]" <[email protected]>
Cc: <[email protected]>
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, [email protected]
<[email protected]> 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" <[email protected]>
> Date: Thu, Oct 21, 2010 10:51
> Subject: [PHP] "My truth comes out" [1]
> To: <[email protected]>
>
> preg_match("/false/i", $string) ? false : true;
>
>
>
> On Thu, Oct 21, 2010 at 7:39 PM, Gary <[email protected]> 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
>
>
--- End Message ---
--- Begin Message ---
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, [email protected]
<[email protected]> 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" <[email protected]>
> Date: Thu, Oct 21, 2010 11:03
> Subject: [PHP] "My truth comes out" [1]
> To: "[email protected]" <[email protected]>
> Cc: <[email protected]>
>
>
> 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, [email protected]
> <[email protected]> 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" <[email protected]>
>> Date: Thu, Oct 21, 2010 10:51
>> Subject: [PHP] "My truth comes out" [1]
>> To: <[email protected]>
>>
>> preg_match("/false/i", $string) ? false : true;
>>
>>
>>
>> On Thu, Oct 21, 2010 at 7:39 PM, Gary <[email protected]> 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
>>
>>
>
--- End Message ---
--- Begin Message ---
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.
--- End Message ---
--- Begin Message ---
>-----Original Message-----
>From: Gary [mailto:[email protected]]
>Sent: 21 October 2010 11:39
>To: [email protected]
>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
--- End Message ---
--- Begin Message ---
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 <[email protected]> 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
>
>
--- End Message ---
--- Begin Message ---
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.
--- End Message ---
--- Begin Message ---
[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);
--- End Message ---
--- Begin Message ---
On 21 October 2010 10:39, Gary <[email protected]> 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
--- End Message ---
--- Begin Message ---
On 21 October 2010 10:39, Gary <[email protected]> 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
--- End Message ---
--- Begin Message ---
On 21 October 2010 12:42, Richard Quadling <[email protected]> wrote:
> On 21 October 2010 10:39, Gary <[email protected]> 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
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
On Wed, Oct 20, 2010 at 07:02:49PM -0400, [email protected] wrote:
> Hmmmmm
>
> I guess the little person inside, just hates the long way of doing
> something.
>
>
> My suggestion
>
> $CD = 1287583899;
> $q1s = 1283313600;
> $q1e = 1291093200;
> $q2s = 1291179600;
> $q2e = 1298869200;
> $q3s = 1298955600;
> $q3e = 1306814400;
> $q4s = 1306900800;
> $q4e = 1314763200;
>
> for($a=1; $a<=4; $a++)
> {
> $sSQ = "q".$a."s";
> $eSQ = "q".$a."e";
> if(($CD >= $$sSQ) && ($CD <= $$eSQ)){ $qCUR = "$a";}
> }
>
> echo $qCUR;
>
> The result is 1
>
>
>
>
> Richard L. Buskirk
There is also a very cool Wikipedia page for more code obfuscation hints:
http://en.wikipedia.org/wiki/Obfuscated_code
--- End Message ---