PHP, like any language has its particularities. One of the them is the inability to understand escaped characters which aren’t in double quotes. So yes, there is a technical difference between a quote and a double quote in a programming language, in PHP at least, if anyone asks.
How does it work? Let’s look at some examples!
$stringData = '<?xml version="1.0" encoding="UTF-8"?> \n SoraGami';
fwrite($fh, $stringData);
So, that PHP script generates an XML file as you can see in our variable $stringData. However I’ve omitted the other code as it isn’t the focus of this article. So, highlighted in blue is the famous escaped character which, in PHP and in all C-insipired languages creates a new line. However, to use it, you must put it inside double quotes (“) and not single quotes (‘). Unfortunately, in the scenario presented here, we needed to put the whole string inside single quotes so that PHP wouldn’t mess itself up with the double quotes inside the XML doctype.
In case you didn’t know, if you have elements with quotes in your string, you can use a combination of double and single quotes to make it work, like this:
' "..." ' " '...' "
Maybe that helped. Anyway, back to our previous example. The way we made it, because \n is within single quotes, it won’t work and appear as text instead. In this case, $stringData would give this:
<?xml version=”1.0″ encoding=”UTF-8″?> \n SoraGami
Unfortunately this is far from what we wanted to achieve, which was to have SoraGami on a new line. We could do the following:
$stringData = "<?xml version='1.0' encoding='UTF-8'?> \n SoraGami";
fwrite($fh, $stringData);
That would work, but in my opinion Single Quotes don’t look like XML. So, instead, you can do it like in two sequences:
$stringData = '<?xml version="1.0" encoding="UTF-8"?>';
fwrite($fh, $stringData);
$stringData = "\n SoraGami";
fwrite($fh, $stringData);
While this works, you might be wondering why the second sequence doesn’t overwrite what we did before. Our PHP $fh variable is written like this:
$fh = fopen($myFile, 'w') or die("can't open file");
‘w’ says to write, but PHP is session-based in the browser, and so as long as we are in the same session and didn’t explicitly fclose() our writing script, PHP is going to append whatever we write to the file. To overwrite, either reload the page (new session) or fclose() the file and make a new fopen() and fwrite().
If you want to append to the file, just changed the ‘w’ parameter for ‘a’. Tizag has a nice tutorial for PHP File Handling that explains further on what we did here.
You might also be wondering why ‘\n’ doesn’t work and “\n” works. The reason is simple, PHP will escape \n regardless of context when inside double quotes. That means if you wrote that:
“The server can be found through Windows on \\nopi”, you would obtain that:
The server can be found through Windows on \ opi
However, since you don’t want PHP to escape the \n in this situation, you would write the following:
‘The server can be found through Windows on \\nopi’, which would give the correct:
The server can be found through Windows on \\nopi