views:

313

answers:

5

How do i display the percent sign % in PHP using unicode %?

Here is the code.

echo $percent . "%";

I have the code inside a PHP function on a separate page it is then displayed on the HTML page.

+1  A: 

Do you mean how to display it in HTML once echo()'d from PHP? Your code there should do it. I just tested it on jsbin (the HTML entity at least).

Judging by your comment,

The php code just displays 99 % for example.

Well, it should, of course, in the HTML. If your viewing that in the front of your browser, it would be likely your encoding htmlspecialchars() or similar. But since you are echo'ing, that should be hard to capture and encode, unless using output buffer functions. Can you provide any more information please?

alex
I want for example `99%` to be displayed. sorry for the confusion.
OpenID
I have the code inside a PHP function on a separate page it is then displayed on the HTML page.
OpenID
A: 

Is it on an HTML wrapped page? % seems to work for me.

Pez Cuckow
I have the code inside a PHP function on a separate page it is then displayed on the HTML page.
OpenID
A: 
$var = $percent . "%";
echo str_replace('%', '%', $var); // POG :O
TiuTalk
This would be easier but I really want to use unicode`echo $percent . "%";`, Whats the ups in using your version of the code?
OpenID
+1  A: 

have you looked at the html source that's being output? does it say

99%

or

99% 

or something similar?

Why are you using the entity? There's nothing wrong with

echo $percent . '%';
sprugman
Using the entity is better then using just the percent sign %
OpenID
@OpenID What is your reasoning behind this?
alex
A: 

Try using the following: sprintf("99%%. Hopefull will echo out a percent sign!",$str,$number);

link text

Let me know if that works?

alvincrespo