SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I was just wondering if there was anything wrong with this switch statment - as it doesn't seem to be working:
    switch ($Folder[2])
    {
    case $Folder[2] != "index.php":
    $Text = $TextDPurple;
    break;
    }

    What I'm aiming to do is :- if Folder[2] is not index.php then text is TextDPurple.

    However, when viewed, TextDPurple is not being incorporated within any of the files - therefore the script must be wrong.

    Is there a better way of writing this "switch".

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, like this:

    Code:
      if ($Folder[2] != "index.php") {
        $Text = $TextDPurple;
      }
    I don't think the switch statement can handle negated equalities like that...I think it's just for comparing values. I use if/else statements for anything that requires less than 3 possible outcomes, for the most part.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are set on a switch statement you could use this, but the if statement is shorter.

    switch ($Folder[2])
    {
    case "index.php";
    $Text = "insert whatever you want it to be when not index.php";
    break;

    default;
    $Text = $TextDPurple;
    break;
    }
    Marty H.

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the help - think I'll go for the switch statement, as it is one part of one huge switch statement.

    Are you sure that switch can't handle 'negated'? I don't understand why it shouldn't as you can use most boolean operators eg:

    {
    case $Folder[2] > 4:
    $Text = $TextDPurple;
    break;
    case $Folder[2] <= 4:
    $Text = $TextBlue;
    break;
    }

    It seems very strange if it does not handle ! when the above is ok. Could someone clarify this.

    TWTCommish - Just my general thoughts :- I tend to do the opposite, us switch for a greater number of possible outcomes (use a switch within a switch etc etc) - I think it makes code a little easier to read, but essentially no difference.

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure why, but obviously it doesn't work. My big red book says that PHP's switch statement is much more flexible than other languages, so maybe they only added a few little functionalities to it.

    Heck, I dunno. I use Selects if I need to alter the value of the variable with more than three possible outcomes, but that's about it.


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
  •