Jump to content

Character Set inside a PHP class?


drewjoh

Recommended Posts

I've been programming for a while... and it's been a long road to understanding and getting all the character sets correct for, well, everything.

 

We have lots of international customers, and I quickly found that having MySQL = latin_1, HTML = 8859-1, and our credit processor = UTF-8... doesn't work very consistently. So going forward... I'm using MySQL = UTF-8_general, the MySQL connection = UTF-8_general... HTML = utf-8... and it's all working fine and good... until just now. This is what I'm finding....

 

This is my test string that I'm using: Bièr hjärt Roló 202 3º 1ª

 

Creating a variable and echoing this in PHP works. Example:

 

$var = 'Bièr hjärt Roló 202 3º 1ª';
echo $var;

 

Creating a class with an empty variable, filling it, and echoing it works: Example:

 

class Test {
public $var;

__construct($input)
{
$this->var = $input;
}
}

$test = new Test('Bièr hjärt Roló 202 3º 1ª');
echo $test->var;

 

However, creating a class with a pre-defined variable, and outputting it... doesn't. Example:

 

class Test {
public $var = 'Bièr hjärt Roló 202 3º 1ª';

__construct()
{ }
}

$test = new Test();
echo $test->var;

 

I have searched high and low and can't figure this out for the life of me. Can anyone explain this? Or confirm/debunk my results?

 

In the last example that doesn't work. I can say this:

 

echo utf8_encode($test->var);

 

And it works ok, but this doesn't explain why it does this, and is not really an acceptable solution. Seeing how pre-defined variables need to be encoded (shouldn't they be already?), but class variables set from outside don't.

 

Thanks to anyone who can shed some light on this!

 

Other information: using phpinfo();...

- My "default_charset" in the php config is set to "no value". My character set for the HTML page is set to utf-8 and that's the type the page is using to render text.

- Using a similar class, if I set a variable in the class and it then saves it to my database, and then retrieves it and outputs it... works correctly also. So it seems to only happen with a variable that's predefined in a class.

Link to comment
Share on other sites

Thanks for your reply JD2007! When I saw your post I was like "oh, duh!" and went to do that, but... "function" was there, I just didn't include it when typing it out here in the forum.

 

So... no go,  ??? I must be doing something wrong... or something. Especially if this works for you... then I don't know what to think!

 

I tried it some more, and this is what happens with two examples:

- The first example of the test string is when saying "echo $test->var" when the variable was set in the class definition.

- The second example of the test string is when setting the $var after initiating an instance of the class, and then outputting it.

 

In Firefox, when I view source (Firefox source browser), I get:

Bi?r hj?rt Rol? 202 3? 1? - Bièr hjärt Roló 202 3º 1ª

 

And in IE, when I view source (in notepad), I get:

Bièr hjärt Roló 202 3º 1ª - Bièr hjärt Roló 202 3º 1ª

 

Firefox displays in the browser just what you see in source. IE displays "Bi�r hj�rt Rol� 202 3� 1� - Bièr hjärt Roló 202 3º 1ª" in the browser.

 

I still don't get it! An hour or so more of searching and googling hasn't provided anything new. But at least I've figured out my limitations...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.