{"id":4737,"date":"2015-02-15T06:33:04","date_gmt":"2015-02-15T05:33:04","guid":{"rendered":"http:\/\/emilkirkegaard.dk\/en\/?p=4737"},"modified":"2015-02-15T06:33:04","modified_gmt":"2015-02-15T05:33:04","slug":"php-sucks","status":"publish","type":"post","link":"https:\/\/emilkirkegaard.dk\/en\/2015\/02\/php-sucks\/","title":{"rendered":"PHP sucks"},"content":{"rendered":"<p>Today I had to work with PHP. I&#8217;m making a website to gather survey data because Google Forms cannot do what I need to do, e.g. give the user instant response in form of various statistics and figures. 7 months ago I took the PHP course at <a href=\"http:\/\/codecademy.com\/\">CodeAcademy<\/a> but don&#8217;t really remember much about it, except that it sucks. Today I re-learned why.<\/p>\n<p><strong>End every line with &#8220;;&#8221;<\/strong><br \/>\nThis is redundant. Every line already ends with \\n, so clearly the programming language could use that. <em>Yes, but what if one wants to minimize the code?<\/em> Then one can make &#8220;;&#8221; optional as done in R.<\/p>\n<p><strong>Printing an array just results in &#8220;Array&#8221; not the content<\/strong><br \/>\nI don&#8217;t know why anyone would do this. Python and R both get it right.<\/p>\n<p><strong>Variables have to use $ in front<\/strong><br \/>\nThis is just plain annoying. Even worse is that when copying variables, double-clicking will not select the extra $ in front of words automatically.<\/p>\n<p><strong>When working, one has to constantly upload changes to the server<br \/>\n<\/strong>This slows down the development process quite a bit.<strong><br \/>\n<\/strong><\/p>\n<p><strong>When using regex, one has to add \/&#8217;s<\/strong><br \/>\nSo instead of calling <a href=\"http:\/\/php.net\/manual\/en\/function.preg-split.php\">preg_split<\/a>(&#8220;;&#8221; , &#8220;A;B;C;&#8221;); one has to do: preg_split(&#8220;\/;\/&#8221; , &#8220;A;B;C;&#8221;); seemingly for no reason. This is not necessary in Python or R.<\/p>\n<p><strong>Constant encoding problems<\/strong><br \/>\nApparently, encoding is very difficult for php+mysql. One has to save all files with the right one, set the variables in mysql to the right one, and tell php to interpret data from mysql as the right encoding. If any step is missing, expect unreadable characters. Since I&#8217;m working this Danish language strings, this was a problem.<\/p>\n<p><strong>And the headache of adding dynamically generated form data to the database with mysql<\/strong><br \/>\nIf one wants to collect survey data, generally one wants to autogenerate the input forms from some database and collect the information. The alternative is to hardcore every question and answer form. However, this presents the problem of getting this information over to the database. <a href=\"http:\/\/www.freewebmasterhelp.com\/tutorials\/phpmysql\/4\">Tutorials<\/a> usually only cover hardcoded examples. However, someone suggested using serialize(), which turned out not to work, but it did lead me to find a solution that works: <a href=\"http:\/\/stackoverflow.com\/questions\/27056176\/inserting-serialized-array-in-mysql\">implode()<\/a>, which is <a href=\"http:\/\/www.tutorialspoint.com\/python\/string_join.htm\">.join in Python<\/a> and <a href=\"http:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/base\/html\/paste.html\">paste() in R<\/a>.<\/p>\n<p>I ended up with something like this:<\/p>\n<pre>&lt;?\r\n\/\/database login info\r\n$username=\"username\";\r\n$password=\"pass\";\r\n$database=\"database\";\r\n$host=\"host\";\r\n\/\/data\r\necho \"Arrayet er \".count($_POST).\" rows langt &lt;br&gt;\";\r\n$inputData = \"'','\" . implode(\"','\", $_POST) . \"'\"; \/\/reformat for mysql\r\nprint_r($inputData).\"&lt;br&gt;\"; \/\/output array to check for errors\r\n\r\n\/\/connect\r\n$link = mysql_connect($host,$username,$password);\r\n<strong>mysql_set_charset(\"utf8\",$link);<\/strong> \/\/encoding\r\n@mysql_select_db($database) or die( \"Unable to select database\");\r\n\/\/insert data\r\n$query = \"INSERT INTO questions_short_responses VALUES ($inputData)\";\r\nmysql_query($query);\r\nmysql_close();\r\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I had to work with PHP. I&#8217;m making a website to gather survey data because Google Forms cannot do what I need to do, e.g. give the user instant response in form of various statistics and figures. 7 months ago I took the PHP course at CodeAcademy but don&#8217;t really remember much about it, [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2089],"tags":[2091,1529],"class_list":["post-4737","post","type-post","status-publish","format-standard","hentry","category-programming","tag-php","tag-suicide","entry"],"_links":{"self":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/4737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/comments?post=4737"}],"version-history":[{"count":3,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/4737\/revisions"}],"predecessor-version":[{"id":4740,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/4737\/revisions\/4740"}],"wp:attachment":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/media?parent=4737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/categories?post=4737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/tags?post=4737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}