Correct Coding

This is a discussion on Correct Coding within the PHP General forums, part of the PHP Programming Forums category; Is this the best way to do this? if(isset($Task) && $Task == "Add") { Do something } I ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-07-2003
Christopher J. Crane
 
Posts: n/a
Default Correct Coding

Is this the best way to do this?

if(isset($Task) && $Task == "Add") { Do something }

I want to check if the variable is set and if so, if it is "Add".



Reply With Quote
  #2 (permalink)  
Old 08-07-2003
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Correct Coding

Looks good.

Cheers,
Rob.

On Thu, 2003-08-07 at 13:09, Christopher J. Crane wrote:
> Is this the best way to do this?
>
> if(isset($Task) && $Task == "Add") { Do something }
>
> I want to check if the variable is set and if so, if it is "Add".
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
..---------------------------------------------.
| Worlds of Carnage - http://www.wocmud.org |
:---------------------------------------------:
| Come visit a world of myth and legend where |
| fantastical creatures come to life and the |
| stuff of nightmares grasp for your soul. |
`---------------------------------------------'
Reply With Quote
  #3 (permalink)  
Old 08-07-2003
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Correct Coding

That can generate an error if $Task was never assigned a value.

Cheers,
Rob.


On Thu, 2003-08-07 at 13:17, Juan Nin wrote:
> > Is this the best way to do this?
> > if(isset($Task) && $Task == "Add") { Do something }
> > I want to check if the variable is set and if so, if it is "Add".

>
> why don't just do:
>
> if($Task == "Add") { Do something }
>
> regards,
>
> Juan
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
..---------------------------------------------.
| Worlds of Carnage - http://www.wocmud.org |
:---------------------------------------------:
| Come visit a world of myth and legend where |
| fantastical creatures come to life and the |
| stuff of nightmares grasp for your soul. |
`---------------------------------------------'
Reply With Quote
  #4 (permalink)  
Old 08-07-2003
Skate
 
Posts: n/a
Default Re: [PHP] Correct Coding



> That can generate an error if $Task was never assigned a value.
>


could you not do

if(@$Task == "Add" ){do something }

to suppress the error of the variable not being set?


Reply With Quote
  #5 (permalink)  
Old 08-07-2003
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Correct Coding

You can -- but correct me if I'm wrong -- won't that possibly cause an
exception to fire which could be extremely heavy if a custom exception
handler is implemented?

Cheers,
Rob.

On Thu, 2003-08-07 at 13:35, skate wrote:
>
>
> > That can generate an error if $Task was never assigned a value.
> >

>
> could you not do
>
> if(@$Task == "Add" ){do something }
>
> to suppress the error of the variable not being set?
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
..---------------------------------------------.
| Worlds of Carnage - http://www.wocmud.org |
:---------------------------------------------:
| Come visit a world of myth and legend where |
| fantastical creatures come to life and the |
| stuff of nightmares grasp for your soul. |
`---------------------------------------------'
Reply With Quote
  #6 (permalink)  
Old 08-07-2003
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Correct Coding

Could you explain a little better why this would make things better?

I don't understand how this would improve things.

Jim Lucas
----- Original Message -----
From: "Curt Zirzow" <curt@zirzow.dyndns.org>
To: <php-general@lists.php.net>
Sent: Thursday, August 07, 2003 10:28 AM
Subject: Re: [php] Correct Coding


> * Thus wrote Christopher J. Crane (ccrane@ikon.com):
> > Is this the best way to do this?
> >
> > if(isset($Task) && $Task == "Add") { Do something }
> >
> > I want to check if the variable is set and if so, if it is "Add".

>
> Yes, that is good. Remember, though, it is case sensitive so "ADD"
> wont match.
>
> Concerning the $Task == "Add", I'd like to make a comment. It can
> be a wise decision to compare your variables with strings like:
>
> if ("Add" == $Task)
>
> This can help preventing typo's like:
>
> if ($Task = "Add")
>
> I've seen people tear their hair out wondering why the if statement
> is always true even if $Task is not "Add". I don't see this method
> used very often but it can prevent serious logic typos.
>
>
> HTH,
>
> Curt
> --
> "I used to think I was indecisive, but now I'm not so sure."
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Reply With Quote
  #7 (permalink)  
Old 08-07-2003
Roger B.A. Klorese
 
Posts: n/a
Default RE: [PHP] Correct Coding

> Could you explain a little better why this would make things better?
>
> I don't understand how this would improve things.



> > Concerning the $Task == "Add", I'd like to make a comment. It can
> > be a wise decision to compare your variables with strings like:
> >
> > if ("Add" == $Task)
> >
> > This can help preventing typo's like:
> >
> > if ($Task = "Add")


$Task = "Add" (typo'd for $Task == "Add") would assign the value,
clearly not your intent.

"Add" = $Task would be an illegal assignment to a constant, so your
error would be detected.


Reply With Quote
  #8 (permalink)  
Old 08-07-2003
Martin Peck
 
Posts: n/a
Default Re: [PHP] Correct Coding

> > That can generate an error if $Task was never assigned a value.
> >

>
> could you not do
>
> if(@$Task == "Add" ){do something }
>
> to suppress the error of the variable not being set?


I have never seen php give an error if $Task is not set to anything. I
would have said that

if ("Add" == $Task) { Do something }

would always be fine - what am I missing?

Martin

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:49 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0