You Down With PHP?

Robert M Ricci
May 6 · 3 min read
Photo by Ben on Unsplash

So in my last post, I wrote a little about WordPress. In that same vain I’m going to write about PHP, the language that WordPress is written with. I will get into the history, and some best practices. This might end up being a multi-post topic, much like Java was. Then we will talk about some syntax, with examples. So let’s get to it.

PHP or Hypertext Preprocessor is a general purpose scripting language, that works well for web development. Created in 1994 by Danish-Canadian programmer Rasmus Lerdorf, but is now controlled by the PHP Group. It is usually processed on a web server by a PHP interpreter. On a web server, the result of the interpreted and executed PHP code would form the whole or part of an HTTP response. Additionally, PHP can be used for many programming tasks outside of the web context. PHP code can also be directly executed from the command line.

From what I can tell most of the best practices are the same for any language, but I will still mention a few. The first one is to get read the PHP manual which is free you can find it here. Indent and use white spaces for read ability, this is one of those habits that just a good idea for any coding. Look into using MAMP as a database. I recently started using it for learning WordPress. These are just a few but the standards still apply such as using DRY(Don’t Repeat Yourself) principles. Make sure you are using comments, as well as using correct naming conventions.

For syntax, we will get into a few of the simple ones. PHP has a few keywords, probabaly one of the most useful is echo. We will also go over variables in the same examples to save space. In this example you will notice that variables start with $ symbol, and can contian letters numbers and underscores. You will also notice in the example that I used Echo and echo. Now I can do this because PHP is not case-sensitive, and both versions are acceptable.

 $name = "marvin";
echo $name;
//prints marvin
$new_name = "Ford Prefect";
Echo $new_name;
//prints Ford Prefect
$new_9_variable = 42;
echo $new_9_variable;
//prints 42

Just like in most other languages, you can reassign variables just by using the assignment operator.

$name = "marvin";$name = "Ford Prefect";
echo $name;
/prints Ford Prefect

If you want to concat two strings together you just need to use the .(dot) operator.

echo "Intergalactic"."Gargle Blaster";
//prints Intergalactic Gargle Blaster

Something I find neat about PHP is the (.=), which appends a string to the end of another one.

$gold_brick = "Intergalactic";
$gold_brick .= "Gargle Blaster";
echo $gold_brick;
//prints Intergalactic Gargle Blaster

One of the issues with echo is that it does not create a page break. Thats where the string escape seqeunce comes in handy. It is actually good for adding special characters into a string.

echo "Oh freddled gruntbuggly,";
echo "Thy micturations are to me, (with big yawning)";
//prints Oh freddled gruntbuggly,Thy micturations are to me, (with big yawning)". -This is onlt on this line because of the size of the box.
echo "Oh freddled gruntbuggly,";
echo "\n Thy micturations are to me, (with big yawning)";
//prints Oh freddled gruntbuggly,
Thy micturations are to me, (with big yawning)

That’s a start at diving into PHP. I’m excited to get into it, as I'm starting a part-time job that uses WordPress a lot. I’m hoping that undrestanding PHP will help with the nuts and bolts of WordPress.