Help with IF (Control Structure) in simple function...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bonovox
Forum Newbie
Posts: 3
Joined: Fri Dec 31, 2004 5:56 pm

Help with IF (Control Structure) in simple function...

Post by bonovox »

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!
User avatar
potsed
Forum Commoner
Posts: 50
Joined: Sat Oct 09, 2004 12:00 pm
Location: - South Africa

Post by potsed »

Code: Select all

if ($id=='1'){ $link = "http://altavista.com";}
elseif ($id == "2"){$link = "http://yahoo.com";}
elseif ($id == "3"){$link = "http://google.com";}
although it may b better to use a switch...

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;
}
or you could use an array to hold the links..

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";
bonovox
Forum Newbie
Posts: 3
Joined: Fri Dec 31, 2004 5:56 pm

Post by bonovox »

Thanks and a Happy New Year... I'll work on your code, but so far I'm getting undefined variable errors... Thanx again...
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

you prob wanna use something like this

Code: Select all

if (isset($_GET['id'])) {
    if ($_GET['id'] == '1') $link = 'foo';
    // etc...
}
have a read up on "register globals"
bonovox
Forum Newbie
Posts: 3
Joined: Fri Dec 31, 2004 5:56 pm

Post by bonovox »

Thanks a lot... and Happy New Year... It works fine... As I said I don't know a whole lot of PHP... just getting the hang of it... Thanks again.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

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
User avatar
potsed
Forum Commoner
Posts: 50
Joined: Sat Oct 09, 2004 12:00 pm
Location: - South Africa

Post by potsed »

n00b 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
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 situation
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

ya, right. i only wanted to tell him exactly where he missed it. thats it. if even i'm starting out a new thing i wont like to ehar that. i would only like to know where i went wrong:)
u r right bt that info and thinking comes later on when we r able to put together a working code.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

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]);
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

shortest route home. eh! :)
Post Reply