I Learned PHP and I Think I Have a Mental Illness
2024-09-07 17:37 +0000
So, as the title says, I learned PHP… And I kinda like it. This has led me to diagnose myself with some sort of mental illness. I mean, who in their right mind could like PHP. After much thought, I believed that writing it out can help me determine what’s wrong with me. Let’s start at…
The Beginning
As I was exploring the “values” of the GitHub Student Pack, I discovered that it gave free 6-month of access to Frontend Masters. I signed-up for it and went straight into the “courses” tab. This is what I saw:
I don’t know what, but I felt like I had to click on it. Something in me wanted to see it. It felt like fate. So, I went in, and I have never been the same again.
At the start I learned that PHP is “embedded”1 into HTML. Look at this2
<body>
<h1>Hello <?php echo "PHP?" ?></h1>
</body>
This works as expected – printing “Hello PHP?” onto the page. When I first saw this, I. WAS. AMAZED. As a self-entitled HTML programmer, this has changed me entirely. I knew I could do things – amazing things – with this new-found knowledge.
But, if I only knew what was to come…
PHP: The Syntax
In PHP, if you want to declare a variable you MUST prefix it with a $
. So,
this means if I want to have variables saving name
, age
, and nullValue
, I
would have to write
$name = 'nilhiu';
$age = 9001;
$nullValue = null;
Now what if I added the following, what do you think will happen?
$$name = 0;
A syntax error? Creates a variable named $$name
? No. This does something
that absolutely amazed me. I never knew I wanted this before I saw it for
myself. That piece of code will define a variable named $nilhiu
. Why?
Because, the variable $name
is equal to nilhiu
.
Yes, you heard me right, DYNAMIC VARIABLE NAMING IS A THING. So,
if you write the above and try to echo
out $nilhiu
, it will print
0
to the console. WELCOME TO THE FUTURE.3
But, this ain’t the end. Let’s say you were tasked to concatenate
two strings $str1
and $str2
, how would you go about it? Would you write
$concat = $str1 + $str2
? Most likely right? NOT IN PHP. In this house, we
use the .
operator.
$str1 = 'conca';
$str2 = 't me pls';
$concat = $str1 . $str2;
You might not like this, BUT I LOVE THIS. One thing I loved doing in Haskell
was spamming the .
operator everywhere I could, and now I can do the same in
PHP. However, this is not the only way to achieve this task.
$concatBeta = "$str1$str2";
The above is another way of doing the same thing. If you haven’t noticed instead of using single quotes, I used double quotes. Without that change, the above string interpolation won’t work. Even though it’s boring, sometimes it’s useful.
And that “sometimes” is NOW. Say, have you ever thought that bash
has the
best syntax for multiline strings. Well, it’s your lucky day! In PHP, if you
want to save a multiline string in a variable – such as a JSON string – you
would write:
$json = <<<JSON
{
"mesg": $concat,
"ok": true,
"boo": false
}
JSON;
And yes, you can replace JSON
with anything you want. But there’s more.
As you might have noticed, that string works like a double-quoted string.
Meaning, it can do string interpolation. If you don’t want that, I introduce
to you, the beautiful difference, and syntax
$json = <<<'JSON'
{
"mesg": $concat,
"ok": true,
"boo": false
}
JSON;
I don’t know who thought of this amazing syntax, but, honestly, I’m thankful they lived to give us such an amazing language. Now, hopefully, you also understand the beauty of PHP. However, we have so much more beauty to cover.
WTF Is an Array?
Arrays can be defined in two different ways
$arr1 = array(0,1,2,3,4);
$arr2 = [0,1,2,3,4];
With no further discussion, I – and you – will always use the second way. Now, what if I wrote this instead:
$arr2 = [
3 => 0,
1 => 1,
2 => 2,
0 => 4,
4 => 3,
];
What do you think this does? If you answered, turns the array into a dictionary like object, no. Arrays are always a dictionary like objects. The above syntax just clarifies it, and is not just limited to numbers. You can have a beautiful array, like
$beautifulArr = [
'o' => 5123,
true => 0,
false => 1,
54 => 3,
'a string value, because why tf not',
$arr2[3] => true,
];
To access any of the values, you would use the same syntax as in any other
language, just write $beautifulArr[{whatever key you want}]
. As a heads-up,
if for whatever reason you’re going to use true
and false
as keys, just
know if you use 0
or 1
as a key for another value, IT WILL GET OVERRIDDEN.
If you want to print out the key-value pairs of an “array”, you can do it with
the amazing foreach
loop.
foreach ($beatifulArr as $key => $val) {
echo "$key: $val\n";
}
If you don’t define a key for an element in an “array”, PHP defines them for you! It finds the last integer-type key you have given to an element, increments it, and that’s your key.
So, again, what the hell is an array?
True or FaLsE
A boolean value has, of course, two values: true
and false
. However, by
technicality, in PHP, you have (2^4 + 2^5 = 48) boolean values. Why, you may
ask? That’s because PHP treats boolean values without case-sensitivity. Meaning,
$trues = [
true,
True, tRue, trUe, truE,
TRue, TrUe, TruE, tRUe, tRuE, trUE,
TRUe, TRuE, TrUE, tRUE,
TRUE
];
$falses = [
false,
False, fAlse, faLse, falSe, falsE,
FAlse, FaLse, FalSe, FalsE, fALse, fAlSe, fAlsE, faLSe, faLsE, falSE,
FALse, FaLSe, FaLsE, FalSE, FAlSe, FAlsE, fALSe, fAlSE, fALsE, faLSE,
FALSe, FaLSE, FAlSE, FALsE, fALSE,
FALSE,
]
are all the possible boolean values you can use in PHP. So, either choose to use
true
and false
, or use all of them. As to why this is a thing, I have no
idea, but – on the context of everything above – there is a reason for
everything, and you never know when you will need to use fAlSe
instead.
However, boolean values aren’t the only things treated as case-insensitive. PHP also doesn’t care much for any keywords, function names or class names neither. You can write every single function/class in whatever case you desire. BUT, the same thing cannot be said about variables, because – unlike every other named thing – THEY ARE CASE-SENSITIVE.
But that’s not all. At one point – one major version ago, before version 7.3 – you could choose to make the constants you declared case-sensitive. Yes, there was an OPTION for possibly one of the worst things you could ever do in a codebase. How was it done? Like so,
# name val IS_CASE_INSENSITIVE
define("CONSTANT", "value", true );
Honestly, that would be the only time you should have used one of the more
“special” ways of writing true
, like tRUe
. Thankfully, that’s in the past
and no longer a problem to think about… Unless you get to magic constants,
which are treated more like keywords than constants. By that, I mean they are
case-insensitive. At least they aren’t user-definable…
And Finally, The End
As you can see, PHP is not only an interesting language but also a fascinating one at that. Before exploring the language – as I have now – I have always thought of PHP as nothing more than the web’s version of COBOL. At the end, I was nothing less than catastrophically wrong.
There are a lot of parts of PHP that make you really think about your knowledge about programming language design. A language of 48 boolean values. A language of inconsistent naming conventions. A language of arrays that are actually just dictionaries. It’s all just beautiful.
With that being said, I want to apologize to the great minds that have made PHP what it is, and will be in the future. I also want to thank Frontend Masters for predicting my incoming registration onto their platform to put out a PHP course. Finally, thanks to Maximiliano Firtman, the man who made the course, and made the man I am now.
As I’m writing this, I have also started learning Laravel – yes, that how deep I am right now. For now, it’s fun. For later, I have no clue what to expect, and honestly I’m expecting nothing good. I think this is the start of my downfall. First ever blog post is going well, huh?
So when the smoke begins to rise
O’er ashes of your kind
It’s only then you’ll know the cost
Reach inside, expose it all to the lightThis task won’t fulfill you, it might even kill you
But the choosing is easy if you don’t have a choiceThe Devil — Aether Realm
On technicality, PHP actually just outputs anything outside the
<?php
and?>
tags as is. So, there is no need for it to be valid HTML. ↩︎For those interested, to achieve correct highlighting of PHP mixed with HTML in Markdown, you need to use the
phtml
parser. ↩︎Unless this is also a feature in another language. Which I think is unlikely. ↩︎