beny lim
beny lim

Reputation: 1304

Getting a string with a space in between in echo

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>");

Upvotes: 0

Views: 343

Answers (6)

Marijke Luttekes
Marijke Luttekes

Reputation: 1293

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.

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 157967

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>');

Upvotes: 1

sandy
sandy

Reputation: 1158

Check with this

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

Upvotes: 0

mark
mark

Reputation: 21743

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.

Upvotes: 0

Daryl Gill
Daryl Gill

Reputation: 5524

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

Upvotes: 0

bwoebi
bwoebi

Reputation: 23777

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.

Upvotes: 0

Related Questions