0

I have a code in html that allow me to get string with a space in between such as "Play game".

In html:

<li><a href="allProduct.php?category=Play Game">Play Game</a></li>

But I couldn't do so in echo(), I only managed to get the word "play":

    echo("<table width=80%>
    <tr>
    <ul>
        <li><a href="allProduct.php?category=Play Game">Play Game</a</li>
    </ul>
    </tr>");
beny lim
  • 1,312
  • 3
  • 28
  • 57

6 Answers6

1

Note, although most browser will just eat that whitespaces in urls, they are not allowed per standard. You can refer to this documentation, which says:

URLs cannot contain spaces. URL encoding normally replaces a space with a + sign.

In PHP use urlencode() to encode the whitespace into a +:

$encoded = urlencode('allProduct.php?category=Play Game');
// will give you : allProduct.php?category=Play+Game

Also you cannot use double quotes to surround a string that contains double quotes. I would use single quotes to surround it.

Full example:

echo('<table width=80%>
<tr>
<ul>
    <li><a href="' . urlencode('allProduct.php?category=Play Game') .'">Play Game</a</li>
</ul>
</tr>');
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
1

You are surrounding your echo string in double quotes while also using double quotes inside the string itself. You can solve this by either enclosing your echo string in single quotes, or by escaping the double quotes inside your string using a backslash. (Like so: \")

Edit: you may also want to remove the space from "Play" and "Game". Use PHP's urlencode() function to achieve this. You can also use a &nbsp; instead, as suggested by another poster.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Marijke Luttekes
  • 1,253
  • 1
  • 12
  • 27
0

Use proper escaping of your string:

echo("<table width=80%>
<tr>
<ul>
    <li><a href=\"allProduct.php?category=Play%20Game\">Play Game</a</li>
</ul>
</tr>");

You also have to encode your whitespaces in URL-format with %20.

bwoebi
  • 23,637
  • 5
  • 58
  • 79
0
echo('<table width=80%>
<tr>
<ul>
    <li><a href="allProduct.php?category=Play%20Game">Play Game</a</li>
</ul>
</tr>');

Change your double quotes for single quotes and it will work for you

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
0

Use single ' for echo. Than you can use normal " for HTML

echo '<tag class="xyz">...</tag>';

By the way: You should not use spaces in links.

You can - and should use - the appropriate methods for it. in your case

urlencode('Music Albums')

which encodes the space accordingly.

mark
  • 21,691
  • 3
  • 49
  • 71
0

Check with this

echo("<table width=80%>
        <tr>
        <ul>
        <li><a href='allProduct.php?category=Play Game'>Play Game</a</li>
        </ul>
        </tr>");
sandy
  • 1,126
  • 1
  • 7
  • 17