1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple PHP help please?

Discussion in 'PHP & Perl' started by Entrepreneur, Nov 7, 2008.

  1. Entrepreneur

    Entrepreneur Regular Member

    Joined:
    Oct 12, 2007
    Messages:
    438
    Likes Received:
    379
    I'm building a page and for tracking purposes I'm trying to use switch to create a variable which is a link ($link) that can then be shown throughout the page, by echoing this new variable.

    What i need to do is, check the url, (is this index or other) and if index it displays one URL everywhere $link appears, and using the switch default, i'll display another link for all other pages.

    The code below worked until i decided to use it as a variable.

    Please help my lovely Blackhat Friends. Oh, and the error is in line 2, so probably something to do with me trying to assign the output to a variable?

    btw, $val is the page and my php knowledge is very low! :)

    Code:
    <?php 
    $link = switch ($val) {
    case "index":
    echo "http://buy.php";
    break;
    default:
    echo "http://optin.php";
    }
    ?>
    
    Thanks
     
  2. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    So depending on what $val is you want a certain URL to be displayed everywhere you put the $link variable?

    PHP:
    <?php 
    switch ($val
    {
    case 
    "index":$link =  "http://buy.php";
    break;
    default: 
    $link =  "http://optin.php";
    }
    ?>

    Now to dispay the URL everywhere, type something like:

    PHP:
    <?php print $link?>

    Hope I have understood what you were after!
     
    • Thanks Thanks x 1
  3. Entrepreneur

    Entrepreneur Regular Member

    Joined:
    Oct 12, 2007
    Messages:
    438
    Likes Received:
    379
    Excellent! That's exactly what i needed! Thank you. :D

    On a side note i didn't realise that you don't need to echo the $link for it to then become a variable, which is cool and also, what's the difference between print and echo, because i was plannning to use the link as:

    PHP:
    <?php echo "$link"?>
    Thanks again! I really appreciate it. +rep
     
  4. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    Not really sure if there is any difference between print and echo :)
    Some pro PHP person will probably tell you the difference, but I am a mere PHP hacker so get something up thats dirty but works!
     
    • Thanks Thanks x 1
  5. frostbyte

    frostbyte Registered Member

    Joined:
    Nov 7, 2007
    Messages:
    56
    Likes Received:
    19
    They can be used interchangeably most of the time. But if you really want to know the difference, check out

    Code:
    http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40
    
     
    • Thanks Thanks x 1