First of all I am very hungover today, and looking at the screen hurts so I am not going to vouch for the quality of this article. Or, indeed, whether it'll even see the light of day.
Straight after doing my PHP install & "Hello World" exercise the other day, I went to the Codeacademy site and worked through their PHP exercises. They were all pretty easy, but were a good reintroduction to PHP. I distilled a coupla things along the way, which I'll observe here. Just a note: this will be of no interest to anyone who already knows PHP, as it's really superficial stuff. I don't know enough about PHP yet to be interesting about it.
Even before starting with the tutorials, I learned a coupla things from people looking at my one-line Hello World code. PHP works similar to CFML in that one intermingles code with "noise" which the code parser basically ignores. The noise is stuff like HTML, inline JS, and other text inside the .cfm or .php file. In CF one has special tags starting with "<cf", eg: <cfabort> etc. The other options is to write in CFScript which is anything within <cfscript> tags. PHP works similar to CFScript in that a code block starts with "<?php" (and ends with "?>"), eg:
<?php $msg = 'Hello World'; ?>
That's a single statement, so not much different from a <cfset>, but the "<?php" construct is more analogous to a <cfscript> tag because it's just the start of a statement block. So more comprensively I could have:
[blimey... I thought Eclipse was slow to start... NetBeans is absolutely glacial!]
<?php
$msg = "G'day World";
echo $msg;
?>
From my brief foray into PHP in the past, I also recalled that one can abbrev. the "<?php" bit as just "<?", eg:
<?
$msg = "G'day World";
echo $msg;
?>
I was surprised that this didn't work for me. Looking into it, I discovered this syntax has fallen out of favour (explained in various ways depending on where I was reading), and it's switched off by default. In php.ini there's a setting short_open_tag which controls this. I can change it, restart Apache, and then the short-hand tags work. I'll stick with the zeitgeist on the matter and not use them though.
My original "Hello World" code was like this:
<?php $msg = "Hello World"; ?>
<html>
<head>
<title><?php echo $msg; ?></title>
</head>
<body>
<h1><?php echo $msg; ?></h1>
</body>
</html>
Someone pointed out a coupla shortcuts here. Firstly "<?=" is a shortcut for echoing an expression, and that one doesn't need the semi-colon on the last statement in a code block, so I could simply have:
<?php $msg = "Hello World" ?>
<html>
<head>
<title><?= $msg ?></title>
</head>
<body>
<h1><?= $msg ?></h1>
</body>
</html>
Good to know. And apparently the move away from the "<?" syntax does not extend to this short cut, ie: it seems to be "OK" to use it. Although, as with anything one can have an opinion on, there is some divergence of opinion on this. Most of what I've read (which is not a lot, granted) seems to suggest it's OK to use. Pls correct me if I'm wrong (but I'll want evidence, rather than just opinion).
Aside: this is really interesting. Having been doing CFML for over a dozen years, I already know all the "best practices". But I have no idea with PHP, so I'm needing to do a lot of googling!
This is getting a bit stream-of-consciousness, but a cool thing I just noticed in Netbeans is that if one changes the starting quote of a string, it automatically changes the closing quote too. EG, above I have this:
<?php $msg = "Hello World" ?>
And if I change the first " to be ', then Netbeans changes the end one for me automatically. Nice one.
<?php $msg = 'Hello World' ?>
I'm gonna raise that as a feature request for ColdFusion Builder: 3542285.
The other thing I learned whilst messing around with quotes is that - like CFML - PHP seems to support either single or double quotes for strings. One difference is that in PHP one escapes a quote with the backslash, rather than just doubling them up like in CFML, eg:
<?= 'G\'day World' ?>
I was gonna talk about those tutorials today, but I think that's enough for the time being... the article'd be too long if I wrote up all of that too.
It's quite silly, but do you know what? I am quite enjoying finding out stuff even about how to write "G'day World" on the screen, and researching the whys and wherefores.
Oh... and my hangover seems to have mostly gone. Which is good, cos it's time for me to commence my day job...
--
Adam