shane18 Posted October 8, 2009 Share Posted October 8, 2009 I got a question but charsets and stuff, when you take data from a database and put it into a php varible.... is it like converted to a php charset that php uses? Quote Link to comment https://forums.phpfreaks.com/topic/176906-php-charset/ Share on other sites More sharing options...
shane18 Posted October 8, 2009 Author Share Posted October 8, 2009 like i know php doesn't like UTF-8.. so if the data in the mysql database is in UTF-8 will that mess stuff up? Quote Link to comment https://forums.phpfreaks.com/topic/176906-php-charset/#findComment-932758 Share on other sites More sharing options...
corbin Posted October 8, 2009 Share Posted October 8, 2009 like i know php doesn't like UTF-8.. so if the data in the mysql database is in UTF-8 will that mess stuff up? No, UTF-8 data will be fine as long as you don't do any non-binary or non-UTF-8 safe operations on it. When ever a variable holds something, PHP is pretty good about storing the value as expected and leaving it alone regardless of the data. If you want to use functions on the data or something though, you'll have to make sure the function is multibyte safe. For example, instead of strlen, you would have to use mb_strlen. As for outputting, you'll have to make sure that the client knows it's UTF-8 by the way. Usually something like the following works fine: header("Content-Type: text/html; charset=UTF-8"); Quote Link to comment https://forums.phpfreaks.com/topic/176906-php-charset/#findComment-932760 Share on other sites More sharing options...
shane18 Posted October 8, 2009 Author Share Posted October 8, 2009 sooo for the most part, you should set the mysql tables to the charset you save your .php files as so it all matches? Quote Link to comment https://forums.phpfreaks.com/topic/176906-php-charset/#findComment-932762 Share on other sites More sharing options...
corbin Posted October 8, 2009 Share Posted October 8, 2009 Hrmmm, kind of. In PHP 6, I want to think that the default charset PHP uses is the one that the source file is in, but I don't think it really matters much. Also, when saving source files in UTF-8, you have to be careful to save without BOM or the PHP engine stutters a bit. The important part is making sure not to manipulate the data with a function that is not designed to handle that charset, and making sure the web browser knows what charset it is. Quote Link to comment https://forums.phpfreaks.com/topic/176906-php-charset/#findComment-932763 Share on other sites More sharing options...
shane18 Posted October 8, 2009 Author Share Posted October 8, 2009 Well, I really don't understand the whole byte things and stuff.... i hate character sets and wish i didn't have to work with them... all i know how to do is write html,css,javascript,php,mysql.. then like 4 days ago i was trying to figure out how to setup my character sets for my files/mysql data... btw is the mysql server only made to understand UTF-8 because thats what my mysql server charset is set for Basiclly, I program php/mysql sites for simple database driven web sites.. i don't work with bytes... just simple stuff.. so what should I save my .php files as and what character collation should i use for all my tables... and can you explain a bit about character sets and bytes or w/e.. i googled it a bit but i don't understand what i found that well. Quote Link to comment https://forums.phpfreaks.com/topic/176906-php-charset/#findComment-932766 Share on other sites More sharing options...
corbin Posted October 8, 2009 Share Posted October 8, 2009 A character set is essentially a way for a computer to know how to display stored strings. http://www.w3.org/International/questions/qa-what-is-encoding Explains it fairly well. There's a link I saw a while back that explains it really well, but I can't find it. If you need more info, I'll try to find it for you. Edit: Oh, by the way, as to which charset to use, you need to use which ever one can store all of the data you want to store. For example, ASCII is only 1 bytes (8 bits), so it can only hold 255 characters. So ASCII and any charset that uses ASCII (for example, "US-ASCII") is pretty much only good for English. UTF-8 on the other hand is expandable. It has only 7 bits available for use, but that's because the last bit of the byte is reserved to mark whether the next byte is the same character or a different one. In other words, UTF-8 can expand. It can have between 1 and 4 bytes, meaning it can have up to 2^(7*4) characters (about 250 million). Quote Link to comment https://forums.phpfreaks.com/topic/176906-php-charset/#findComment-932773 Share on other sites More sharing options...
shane18 Posted October 8, 2009 Author Share Posted October 8, 2009 thanks... basically all i gota worrie about is 1. what i save my file as(encoding wise) 2. and mysql charset/collation i use right? Quote Link to comment https://forums.phpfreaks.com/topic/176906-php-charset/#findComment-932778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.