url encode

Discussion in 'PHP' started by Jeremy Benson, Jun 25, 2014.

  1. #1
    Ahh, this'll be my last post for a bit, give the board some time to recuperate, lol

    I was just sandboxing with urlencode, but it's not working. Not sure what I'm doing wrong... I'm writing the code the same as in the documentation...

    <?php
    
        $apples = 'yes';
        $oranges = 'no';
       
        $queryString = '?apples=' . urlencode($apples) . '&' . 'oranges=' . urlencode($oranges);
       
        echo '<a href="localhost/'.$queryString.'">Test Link</a> ';   
    
    ?>
    
    Code (markup):
     
    Jeremy Benson, Jun 25, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    What, exactly, doesn't work? Not to mention, what documentation? That's definitely not from php.net
     
    PoPSiCLe, Jun 25, 2014 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    yeah, straight from the chm file. I hardly changed anything...

    from the help file...
    <?php
    $query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
    echo '<a href="mycgi?' . htmlentities($query_string) . '">';?>
    PHP:
    Basically it does nothing... Nothing is encoded. The output string is exactly as it would be if I did nothing, lol.
     
    Jeremy Benson, Jun 25, 2014 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    That's because "yes" and "no" don't need to be encoded. They're already URL safe strings.

     
    nico_swd, Jun 25, 2014 IP
  5. Arniel Montero

    Arniel Montero Banned

    Messages:
    78
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    33
    #5
    hi, try this code:
    $apples = 'yes';
    $oranges = 'no';

    $queryString = '?apples=' . urlencode($apples) . '&' . 'oranges=' . urlencode($oranges);
    echo '<a href=./test.php'.$queryString.'>Test Link</a> ';
     
    Arniel Montero, Jul 2, 2014 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Great, you actually made it worse.
     
    nico_swd, Jul 3, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Okay - first off, a .chm-file is not the manual. PHP.net is the manual. The page you're looking for is here: http://no1.php.net/manual/en/function.urlencode.php
    Second, as others have informed you, urlencode only encodes things that needs to be encoded. Hence the $apples and $oranges example is completely idiotic, since the variables doesn't actually contain anything that needs encoding.
     
    PoPSiCLe, Jul 3, 2014 IP
  8. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #8
    Yeah, I hear ya. What I was looking for was base64_encode, lol :p
     
    Jeremy Benson, Jul 7, 2014 IP
  9. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #9
    "Okay - first off, a .chm-file is not the manual."

    I don't think you're right about that. As far as I can tell the chm file is a downloadable version of the manual. Got it from here.

    http://php.net/download-docs.php

    Why they would have an example in their documentation that has NOTHING to do with doing what the function would do properly is beyond me, lol.
     
    Jeremy Benson, Jul 7, 2014 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Aha. Downloaded from php.net - that's fine then :D Point is that .chm-files are usually Windows help-files, which may or may not be worth the look-up. Usually the concensus is "not".
    Hence why it's usually better to just go to the source online, and read through that, AND the comments - often the comments contain more information, better examples, and maybe, just maybe, just the right function or example to solve your own problem.
     
    PoPSiCLe, Jul 7, 2014 IP