How do you pass variables between PHP scripts using the URL string, that have a "+" in the variable you are passing

echo '<a href="host.com/cart.php?printer='.$selectedPrinter..'">'.$selectedPrinter.'</a>';

If $selectedPrinter is HP 4+

the urlsring turns out to be
http://host.com/cart.php?printer=HP 4+

Tried using urlencode and urldecode but the + sign at the end only shows up in the URL string when I run the
line

echo '"'.$_GET['printer'].'" has been selected.';

in the cart.php file.

I get
"HP 4 " has been selected, instead of "HP 4+" has been selected

Any suggestions?

    Didn't believe this until I tried it for myself...

    I've experimented and found that + is used to build a variable value with spaces

    index.php?myname=Nemo+Noman

    echo $_SERVER['QUERY_STRING'];
    ->myname=Nemo+Noman

    echo $myname;
    ->Nemo Noman

    I'm stumped how to GET the + unless you act directly on the $_SERVER['QUERY_STRING'];

      As long as there is no + in the printer string it works fine.

      I'll try working from the $_SERVER['QUERY_STRING'];

      Thanks for the reply.

        Rule of thumb for inserting arbitrary strings into URLs: use [man]urlencode[/man].

          Like the man said in his first message:

          Tried using urlencode and urldecode but...

          This doesn't work quite as advertised: Try it yourself!

          $EncodePrinter=urlencode('HP4+') returns
          $EncodePrinter=='HP4%2B' ( which displays as HP4+)

          pass this in a url:

          header("Location: zzz.php?printer=$EncodePrinter");

          url becomes: http://localhost/sql/zzz.php?printer=HP4%2B

          echo $printer; returns HP4+ (don't know what happened to %2😎

          $DecodePrinter=urldecode($printer) returns
          $DecodePrinter=='HP4 ' <<NOTE space

            Well ... yeah ... PHP urldecodes form variables automatically on receipt; you don't need to do it a second time yourself. Otherwise you'd have to urldecode every form variable you receive.

              Write a Reply...