Help with IF (Control Structure) in simple function...
Moderator: General Moderators
Help with IF (Control Structure) in simple function...
I use this bit to mask my links, passing the id=1 variable from a previous page to this one in a link go.php?id=1, but everytime no matter the value, it goes directly to the last IF... taking me (in this example) to google...
<?php
if ($id = "1"){$link = "http://altavista.com";}
if ($id = "2"){$link = "http://yahoo.com";}
if ($id = "3"){$link = "http://google.com";}
header("Location: $link");
exit();
?>
any light? I know it might be a stupid question, but I am "no php guru..." so please... HELP!
<?php
if ($id = "1"){$link = "http://altavista.com";}
if ($id = "2"){$link = "http://yahoo.com";}
if ($id = "3"){$link = "http://google.com";}
header("Location: $link");
exit();
?>
any light? I know it might be a stupid question, but I am "no php guru..." so please... HELP!
Code: Select all
if ($id=='1'){ $link = "http://altavista.com";}
elseif ($id == "2"){$link = "http://yahoo.com";}
elseif ($id == "3"){$link = "http://google.com";}Code: Select all
switch($id){
case '1':
$link = "http://altavista.com";
break;
case '2':
$link = "http://yahoo.com";
break;
case '3':
$link = "http://google.com";
break;
}Code: Select all
$links = array("http://altavista.com", "http://yahoo.com", "http://google.com");
$link = (array_key_exists($id, $links)) ? $links[$id] : "http://point/to/default/link";you prob wanna use something like this
have a read up on "register globals"
Code: Select all
if (isset($_GET['id'])) {
if ($_GET['id'] == '1') $link = 'foo';
// etc...
}- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
While this is true... the code was also putting an extra load on the server (not much on 3 lines i know, but every bit helps... ) by having to process every one of the if statements, which is why an if-elseif or switch control structure is a better option than just if's in this type of situationn00b Saibot wrote:ur original code was just fine. only that u used single = instead of ==. single = is meant for assigning a value so in effect ur code was doing right thing. just changing = to == will do what u wanted it to do in first place
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
ah well, here is how i would write it 
Code: Select all
$link = array();
$link[1] = "http://altavista.com";
$link[2] = "http://yahoo.com";
$link[3] = "http://google.com";
$id = isset($_GET['id']) ? $_GET['id'] : 1;
header('Location: ' . $link[$id]);- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact: