SwanseaMick
Posts: 45
Joined: Wed Aug 08, 2012 8:05 am
Location: Gower, Swansea

Outputing a £ Symbol in PHP

Tue Aug 21, 2012 9:16 am

I am slowly getting to grips with a LAMP system (Linux, Apache2, MySQL and PHP), but am having trouble getting the "£" symbol onto the webpage.

When I use the "£", it outputs "A£" (A has a circumflex above it). I read that one uses the "\" escape symbol with "$", to output the "$" character, so I tried that with the "£" character, but it made no difference.

What do I need to do to get the "£" on screen?

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Outputing a £ Symbol in PHP

Tue Aug 21, 2012 9:23 am

Use standard HTML character encodings

£ for £

& for &

Look up HTML characters there are lots of them

&frac12 for 1/2
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
jojopi
Posts: 3108
Joined: Tue Oct 11, 2011 8:38 pm

Re: Outputing a £ Symbol in PHP

Tue Aug 21, 2012 9:38 am

SwanseaMick wrote:When I use the "£", it outputs "A£" (A has a circumflex above it).
This means that the server is using UTF-8 encoding, where the pound is encoded as two bytes: 0xC2 0xA3, but the browser is expecting single-byte ISO-8859-1 encoding, where 0xC2 is  and 0xA3 is £.

You should be able to override the browser encoding in the menus (View→Character Encoding in firefox/iceweasel, Tools→Encoding in chrome/chromium). But the correct solution is to ensure that the page has the correct HTTP header: "Content-type: text/html; charset=UTF-8" or http-equiv.

SwanseaMick
Posts: 45
Joined: Wed Aug 08, 2012 8:05 am
Location: Gower, Swansea

Re: Outputing a £ Symbol in PHP

Tue Aug 21, 2012 10:38 am

jojopi,

That did the trick!

Many thanks

SwanseaMick
Posts: 45
Joined: Wed Aug 08, 2012 8:05 am
Location: Gower, Swansea

Re: Outputing a £ Symbol in PHP

Tue Aug 21, 2012 6:46 pm

Further to my previous posts, I discovered a drawback in getting MySQL to include the £ in the returned result! Although the resulting page looks 'pretty', one cannot sum the different results to get a grand total, as the individual totals are regarded as strings, and the sum of their results is '0' (zero)!
Goes to show, some you win, some you lose!

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: Outputing a £ Symbol in PHP

Wed Aug 22, 2012 6:37 am

SwanseaMick wrote:Further to my previous posts, I discovered a drawback in getting MySQL to include the £ in the returned result! Although the resulting page looks 'pretty', one cannot sum the different results to get a grand total, as the individual totals are regarded as strings, and the sum of their results is '0' (zero)!
Goes to show, some you win, some you lose!
Its always best to represent things as their "natural" data types, so currency should be a decimal, a yes/no choice would be a tinyint (effectively a boolean) and do all your formatting in the output layer. A String is really a very generic type and while you can manipulate them in a lot of ways its not a suitable way to store numeric data. The other problem is that its bad programming practice where you should be trying to separate the data, logic and presentation layers. So in this case your data is in MySQL and the logic and presentation are in PHP. However in that PHP you should have the logic in a different class to your HTML. This lets you in the future switch things around more easily.

In your case you found you wanted to do some sums on your data that you can't do easily with it as a string. An example that I've just come across was I was trying to get my Pi to output data to a 16x2 display however it wasn't working. I had a suspicion it was due to the slow GPIO library I was using thankfully though I had used interfaces to give me some separation and so to use a new library I just wrote one new file for the new library and changed a couple of words and I was up and running with the new interface that worked. If I hadn't then I would of had to rewrite all my bits of code in all my classes that used the GPIO.

SwanseaMick
Posts: 45
Joined: Wed Aug 08, 2012 8:05 am
Location: Gower, Swansea

Re: Outputing a £ Symbol in PHP

Wed Aug 22, 2012 9:27 am

Fruitloaf,

Without sounding tetchy, I am fully aware of the difference between strings and numerical data types.
The point I was trying to make, was that in order to get a 'pretty' output, by using CONCAT & FORMAT in my SQL to get MySQL, I effectively turned numerical data into strings.

My last post was to highlight to others, the unforeseen consequences of these actions.

I didn't want unnecesary £'s on my page, so I took the easy root to eliminate them. I guess I'll now have to go the extra mile and add extra coding to check for value of returns before adding the £, as and where necessary.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Outputing a £ Symbol in PHP

Wed Aug 22, 2012 10:45 pm

SwanseaMick wrote:Fruitloaf,

Without sounding tetchy, I am fully aware of the difference between strings and numerical data types.
The point I was trying to make, was that in order to get a 'pretty' output, by using CONCAT & FORMAT in my SQL to get MySQL, I effectively turned numerical data into strings.

My last post was to highlight to others, the unforeseen consequences of these actions.

I didn't want unnecesary £'s on my page, so I took the easy root to eliminate them. I guess I'll now have to go the extra mile and add extra coding to check for value of returns before adding the £, as and where necessary.
Yes you can do a lot of things in MySQL and any SQL it is more a matter of whether they should be there for efficiency. Formatted string in PHP or half hard coded in your presentation layer HTML would be easier and faster.

Unless you are doing multi-currecy, then back to PHP code before you do any HTML outputting,
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Return to “Other programming languages”