Jump to content

Using Case instead of if else


php4ever

Recommended Posts

I have a situation where I have different pages and I want to echo different content on those pages by using either an include or simple parse change.  The code I have looks tragically heavy.  I'd like a lighter cleaner code snippet and I'm open to any suggestions on this.  Please don't say "learn to program" that part I already know and I'm working on it.

 

<?php 
$P=$_SERVER['REQUEST_URI'];
switch ($P) {
//case "/jared/index.html" || $P == "/" || "$P" == "/jared/index.php":
case "/jared/index.php";
$data1 = 'current';
$data2 = '';
$data3 = '';
$data4 = '';
$data5 = '';
break;
case "/jared/page2.html":
$data1 = '';
$data2 = 'current';
$data3 = '';
$data4 = '';
$data5 = '';
break;
case "page3.html";
$data1 = '';
$data2 = '';
$data3 = 'current';
$data4 = '';
$data5 = '';
$data = 'current'; 
break;
case "page4.html":
$data1 = '';
$data2 = '';
$data3 = '';
$data4 = 'current';
$data5 = '';
break;
case "page5.html":
$data1 = '';
$data2 = '';
$data3 = '';
$data4 = '';
$data5 = 'current';
break;

default:
$data = 'default';
}
?>

 

Then it would be in the HTML as

<h1><?php echo($data); ?></h1> (or something to this effect.)

 

All I want the script snippet to do is IF its on a certain page then display the data for that.

 

~ Jared Ritchey

 

Link to comment
Share on other sites

first off, you're using a semicolon rather than a colon for the first case.  second, why not initialise all $data variables as empty at the start and simply re-assign the value that you intend to change?

 

$data1 = '';
$data2 = '';
$data3 = '';
$data4 = '';
$data5 = '';

switch($P)
{
  case 'first_case':
    $data1 = 'current';
}

 

but i'll be honest, to make a nice set of code, i think you'll have to rethink what you're doing overall here.  what are you using this snippet as a part of?

Link to comment
Share on other sites

agrees with mjdamato...this would look nicer:

 

<?php 
$data1 = '';
$data2 = '';
$data3 = '';
$data4 = '';
$data5 = '';

$P=$_SERVER['REQUEST_URI'];
switch ($P) {
//case "/jared/index.html" || $P == "/" || "$P" == "/jared/index.php":
case "/jared/index.php":
$data1 = 'current';
break;
case "/jared/page2.html":
$data2 = 'current';
break;
case "page3.html";
$data3 = 'current';
$data = 'current'; 
break;
case "page4.html":
$data4 = 'current';
break;
case "page5.html":
$data5 = 'current';
break;

default:
$data = 'default';
}
?>

Link to comment
Share on other sites

Sorry People I'm not getting my emails when you respond to this thread.

 

Basically this little snippet is only to serve a single simple purpose.

 

IF a person is on a specific web page display an advertisement and or header image specific to that page.

 

The idea was that I would enter the name of the page and then set the contents for that page in a variable or in this case $data and the contents of that would load.

 

I could then do a simple php include for various areas of the site too.  Since the only objective here is to first get the http uri and then display the appropriate data accordingly. 

 

I made a mistake by posting that code snippet with "current"  it was meant to be a generic thing.  It could actually be header.php or advertisement.php or randomfooter.php for example. 

 

I just needed a simple way of controlling the contents on a per page basis.

 

I'll do what you suggest.

Link to comment
Share on other sites

Oh yeah, I tried like hell to get the line that is commented out to work and couldn't

 

case "/jared/index.html" || $P == "/" || "$P" == "/jared/index.php":

 

The idea is that if a person lands on the homepage without the file extension of either index.php or index.html they would still get the same $data1 results but for the life of me it doesn't work that way unless the actual page is displayed.

 

Is my syntax wrong?

Link to comment
Share on other sites

In this case (no pun intended) you would just list each "case" statement under one another:

<?php
$P=$_SERVER['REQUEST_URI'];
switch ($P) {
     case '/jared/index.html':
     case '/':
            $data1 = 'current';
            break;
//
// etc...
//
}
?>

 

Ken

 

Link to comment
Share on other sites

You should have a break statement inside every case right? If not, it could keep processing if another case is hit ... say if you're updating a variable that your case is evaluating.

 

Not necessarily. You can have several case statements with a common result as kenrbnsn demonstrated.

 

With a switch statement once a case matches the switch value ALL result code will be executed unless you have break statements:

 

Example:

 

<?php

$var = 10;

switch ($var) {
case 10:
    echo "Value = TEN<br>";
    break;

case 8:
    echo "Value = EIGHT<br>";
    break;
}

//Output:
//  Value = TEN

$var = 10;

switch ($var) {
case 10:
    echo "Value = TEN<br>";

case 8:
    echo "Value = EIGHT<br>";
}

//Output:
//  Value = TEN
//  Value = EIGHT

?>

Link to comment
Share on other sites

You should have a break statement inside every case right? If not, it could keep processing if another case is hit ... say if you're updating a variable that your case is evaluating.

 

Not necessarily. You can have several case statements with a common result as kenrbnsn demonstrated.

 

With a switch statement once a case matches the switch value ALL result code will be executed unless you have break statements:

 

Example:

 

<?php

$var = 10;

switch ($var) {
case 10:
    echo "Value = TEN<br>";
    break;

case 8:
    echo "Value = EIGHT<br>";
    break;
}

//Output:
//  Value = TEN

$var = 10;

switch ($var) {
case 10:
    echo "Value = TEN<br>";

case 8:
    echo "Value = EIGHT<br>";
}

//Output:
//  Value = TEN
//  Value = EIGHT

?>

 

To expand on that example:

 

<?php
$var = 11;

switch ($var) {
// this wil lprint eleven and ten, since there is no break after 11
case 11:
     echo "Value = ELEVEN";
case 10:
    echo "Value = TEN<br>";
    break;

case 8:
    echo "Value = EIGHT<br>";
    break;
}

//Output:
//  Value = TEN

$var = "ten";

switch ($var) {
case "ten":
      echo "value = TEN (Literally)<br />";
case 10:
    echo "Value = TEN (numerically)<br>";
break;

case 8:
    echo "Value = EIGHT<br>";
  break;
}

//Output:
//  Value = TEN
//  Value = EIGHT

?>

 

Which is all valid, just thought I would add some extra flavor to mjdamato's nice example. Basically it will go through cases if a value is true until a break is hit.

Link to comment
Share on other sites

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.