Professional Web Applications Themes

Correct Coding - PHP Development

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

  1. #1

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



    Christopher J. Crane Guest

  2. #2

    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 ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >
    >
    --
    ..---------------------------------------------.
    | Worlds of Carnage - [url]http://www.wocmud.org[/url] |
    :---------------------------------------------:
    | Come visit a world of myth and legend where |
    | fantastical creatures come to life and the |
    | stuff of nightmares grasp for your soul. |
    `---------------------------------------------'
    Robert Cummings Guest

  3. #3

    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 ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >
    >
    --
    ..---------------------------------------------.
    | Worlds of Carnage - [url]http://www.wocmud.org[/url] |
    :---------------------------------------------:
    | Come visit a world of myth and legend where |
    | fantastical creatures come to life and the |
    | stuff of nightmares grasp for your soul. |
    `---------------------------------------------'
    Robert Cummings Guest

  4. #4

    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?


    Skate Guest

  5. #5

    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 ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >
    >
    --
    ..---------------------------------------------.
    | Worlds of Carnage - [url]http://www.wocmud.org[/url] |
    :---------------------------------------------:
    | Come visit a world of myth and legend where |
    | fantastical creatures come to life and the |
    | stuff of nightmares grasp for your soul. |
    `---------------------------------------------'
    Robert Cummings Guest

  6. #6

    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" <curtzirzow.dyndns.org>
    To: <php-generallists.php.net>
    Sent: Thursday, August 07, 2003 10:28 AM
    Subject: Re: [PHP] Correct Coding

    > * Thus wrote Christopher J. Crane (ccraneikon.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
    >
    >
    Jim Lucas Guest

  7. #7

    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.


    Roger B.A. Klorese Guest

  8. #8

    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

    Martin Peck Guest

Similar Threads

  1. php coding
    By Unregistered in forum Brainstorming Area
    Replies: 0
    Last Post: April 18th, 12:07 PM
  2. help in coding
    By fsurrey in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 1
    Last Post: September 5th, 08:03 AM
  3. c# coding
    By lovey dovey via .NET 247 in forum ASP.NET Security
    Replies: 1
    Last Post: May 4th, 04:47 AM
  4. Lil fun be4 coding
    By Julien CROUZET aka c2c in forum PHP Development
    Replies: 0
    Last Post: September 2nd, 02:45 PM
  5. [PHP] Correct Coding
    By Christopher Crane in forum PHP Development
    Replies: 1
    Last Post: August 7th, 06:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139