Today I had to work with PHP. I’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’t really remember much about it, except that it sucks. Today I re-learned why.

End every line with “;”
This is redundant. Every line already ends with \n, so clearly the programming language could use that. Yes, but what if one wants to minimize the code? Then one can make “;” optional as done in R.

Printing an array just results in “Array” not the content
I don’t know why anyone would do this. Python and R both get it right.

Variables have to use $ in front
This is just plain annoying. Even worse is that when copying variables, double-clicking will not select the extra $ in front of words automatically.

When working, one has to constantly upload changes to the server
This slows down the development process quite a bit.

When using regex, one has to add /’s
So instead of calling preg_split(“;” , “A;B;C;”); one has to do: preg_split(“/;/” , “A;B;C;”); seemingly for no reason. This is not necessary in Python or R.

Constant encoding problems
Apparently, 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’m working this Danish language strings, this was a problem.

And the headache of adding dynamically generated form data to the database with mysql
If 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. Tutorials 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: implode(), which is .join in Python and paste() in R.

I ended up with something like this:

<?
//database login info
$username="username";
$password="pass";
$database="database";
$host="host";
//data
echo "Arrayet er ".count($_POST)." rows langt <br>";
$inputData = "'','" . implode("','", $_POST) . "'"; //reformat for mysql
print_r($inputData)."<br>"; //output array to check for errors

//connect
$link = mysql_connect($host,$username,$password);
mysql_set_charset("utf8",$link); //encoding
@mysql_select_db($database) or die( "Unable to select database");
//insert data
$query = "INSERT INTO questions_short_responses VALUES ($inputData)";
mysql_query($query);
mysql_close();
?>