Jump to content

Recommended Posts

Hi chaps,

 

I have some code as follows:

if ($_GET['function'] == "")
{
$content .= 'Foo';
echo $content;
}

else if ($_GET['function'] == "1")
{
$content .= 'Foo';
echo $content;
}

else if ($_GET['function'] == "2")
{
$content .= 'Foo';
echo $content;
}

else if ($_GET['function'] == "3")
{
$content .= 'Foo';
echo $content;
}

else if ($_GET['function'] == "4")
{
$content .= 'Bar';
echo $content;
}

else if ($_GET['function'] == "5")
{
$content .= 'Bar';
echo $content;
}

else if ($_GET['function'] == "6")
{
$content .= 'Bar';
echo $content;
}

else
{
$content .= 'woot';
echo $content;
}

The above works great, but as you can see I'm reusing the same code and making the file very large when it doesn't need to be.  So, I tried the following:

if ($_GET['function'] == "")
{
$content .= 'Foo';
echo $content;
}

else if ($_GET['function'] == "1" || "2" || "3")
{
$content .= 'Foo';
echo $content;
}

else if ($_GET['function'] == "4" || "5" || "6")
{
$content .= 'Bar';
echo $content;
}

else
{
$content .= 'woot';
echo $content;
}

The above code always defaults to "Foo".  Am I doing it right?  Or am I missing something really obvious?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/142089-solved-php-if-or-and-else/
Share on other sites

i would also suggest a switch statement:

 

<?php

/* This says 
   "if 'function' is 'set' or exists, then set $func to that value, otherwise set $func to null"
   (this is only really to rid yourself of any "Notice: Undeclared Variables" warnings.)
*/
$func = (isset($_GET['function']))? $_GET['function'] : null;

// This is like a clean and easy if statement block, with the "else" being "default".
Switch($func){
   default:
      // Put any code here that would be displayed if function did not match any criteria
   break;
   
   case "1":
      // Maybe Include("file.php");
   break;
   
   case "2":
      // Include("file2.php");
   break;
}

?>

uniflare, thanks for the help, and thanks everyone else for your kind replies.  I've converted my script using uniflare's code but I'm still getting the same problem.  I have changed it to:

 

<?php

$func = (isset($_GET['function']))? $_GET['function'] : null;

Switch($func){
   default:
      $content .= 'woot';
      echo $content;
      break;
   
   case "1" or "2" or "3":
      $content .= 'Foo';
      echo $content;
   break;
   
   case "4" or "5" or "6":
      $content .= 'Bar';
      echo $content;
   break;
}

?>

 

Using the above code it always defaults to "Foo", even when the function the script is using is 4, 5 or 6.  It will display "woot" however when the function is null.

 

Any ideas?

 

Thanks

 

There was a problem with your use or or, the following works;

 

<?php
$func = (isset($_GET['function']))? $_GET['function'] : null;
$func = (int) $func;

Switch($func){
case 1:
case 2:
case 3:
	$content = 'Foo';
	echo $content;
	break;

case 4:
case 5:
case 6:
	$content = 'Bar';
	echo $content;
	break;

default:
	$content = 'Foo';
	echo $content;
	break;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.