Why doesn't my simple function work?

Discussion in 'PHP' started by x0x, Jan 22, 2010.

  1. #1
    function pllink($pid=0,$rnd=0){
    $value = "<a href=player.php?pid=$pid&rnd=$rnd>$pid</a>";
    echo $value;
    }
    PHP:

    pllink("Jack The Man",1);

    gives me:

    <a href=player.php?pid=Jack&rnd=1>Jack The Man</a>

    Why does it not show what's after the space?
     
    x0x, Jan 22, 2010 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    need to quote the href part. function works fine. though you should url encode the spaces.

    function pllink($pid=0,$rnd=0){
    $value = "<a href=\"player.php?pid=$pid&rnd=$rnd\">$pid</a>";
    echo $value;
    }
     
    shallowink, Jan 22, 2010 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thanks you!
     
    x0x, Jan 22, 2010 IP
  4. fourdesign

    fourdesign Member

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    Couple things I might consider:

    First, if you are using double quotes to enclose your variable then it is also acceptable to use single quotes on anything inside. For example, your URL would look like this:

    $value = "<a href='player.php?pid=$pid&rnd=$rnd' title='$pid'>$pid</a>";

    It serves the same purpose and using the escape character, \, but is a bit easier to read. No matter which method you choose be aware that your PHP can be broken by passing a variable that contains a quote. If you are concerned about this then you might want to use str_replace() to escape any quotes.

    Second, shallowink brings up a good point about the spaces in your variable. When translated to a URL, spaces get rewritten as %20. It's not really a big deal but if you want a clean looking URL then you might want to consider replacing spaces with dashes.
     
    fourdesign, Jan 23, 2010 IP