paint-brush
PHP basics for expert web developers (1' part)by@anastasionico
873 reads
873 reads

PHP basics for expert web developers (1' part)

by anastasionicoDecember 11th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this series, starting with this blog post, I am going to explain to you exactly what <strong>you must learn in order to start your journey as a web developer</strong> using PHP.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - PHP basics for expert web developers (1' part)
anastasionico HackerNoon profile picture

In this series, starting with this blog post, I am going to explain to you exactly what you must learn in order to start your journey as a web developer using PHP.

In fact,

These will be a series full of thorough tutorials and detailed explanations suitable for beginner and for experienced programmers.

The information here made me ready to land on my first job and improved my fundamental skills incredibly useful in order to understand more complex features of this language

… and from today you will be able to do literally the same.

The renaissance of PHP

The Origin

If you are on this page right now it is very likely that you have heard about PHP at least once.

PHP is one of the most famous and durable programming languages ever existed.

Into specific PHP is an interpreted server-side scripting language.

This long series of words mean that the code you write is executed by an interpreter.

PHP is typically used with a web server such as Apache and Nginx in order to serve dynamic content to web pages.

This is only one of its characteristics.

In fact,

It also can be used to build surprising powerful command-line applications.

This is a feature that only a few developers practice,

And I hope that keep reading this article you will become one of them.

In all honesty,

PHP has had a quite problematic past,

In the beginning, It started as a collection of CGI scripts written by Rasmus Lerdorf.

A half Danish half Canadian programmer who was born in Greenland in the winter of ‘68.

Legend has it, that he wanted to keep track of the number of visitors that landed in his online CV so he created a collection of CGI and called it Personal Home Page tools.

Spot it?

At that time it was completely unrecognizable from the current version of the language, it only provided several variables and an HTML embedded syntax.

Paleolithic!

From 1994 to 1998 PHP was revisited, reset and started again numerous times.

The process of transforming PHP from a collection of CGI to the language what we know today started when 2 developers, Andi Gutmans and Zeev Suraski, both from Tel Haviv decided to join Rasmus on his project.

As said, it was a draining work but after several years the 6th June 1998, the final outcome was finally released and called PHP 3.

Although still different, this was the first version that had a stable syntax and looked like the code we see nowadays.

Among its most important features, there was extensibility to various databases, protocols and APIs.

These and the simplicity of the code were the main reason PHP attracted so many developers.

After just a few months from its official release PHP conquered already 10% of the web server in the world.

Of course, the Internet was not available everywhere like today but, it still is a pretty awesome statistic.

Present days

After almost a decade of purgatory, now things started to rapidly accelerate.

Thanks to a dozen core-team developers PHP in its 7th version has seen an incredible enhancement.

Have a look at the list of features present in its last release.

Not only Its speed and popularity have now reached the trendier languages like Python or NodeJs, but the practices are improving as well making PHP creating secure and easily manageable.

Years ago a normal deployment process would have been to write PHP files, upload them to a production environment using an FTP and verify if everything worked after the file reached the live server.

I do not need to explain why this was dangerous other than time-consuming.

Now we can leverage version control software like Git that allows us to take care of all our versions, we can branch out, we can fork, merge and so on.

At the same time, we can be sure that the local environments are exactly like the production one by using Vagrant and Docker.

Also, we can get parts of scripts that we need from outside our project using packages downloaded with the Composer dependency manage.

We have PSR which define standard we really want to adhere.

We automated the test of our code with PHPUnit having a key role throughout the whole development phase.

Eventually, we increase the performance of the application with an opcode cache.

There are dozens, maybe hundreds of new practices that we are going to adopt in the next few years.

And this can seem overwhelming, and it is, but at the same time is healthy for both the future of the language and the career of the web developers.

In the following sections of this article, you are going to travel along the basics of PHP in its 7.0 version and upwards,

When there will be the case I will link to some particular part of other posts I have done.

For instance, topics regarding the brand new PHP 7.3 and Object-Oriented programming.

Going forward there will be info about PHP frameworks or MVC architectural pattern.



Good. Now, let’s cut to the chase.

The very basics

PHP 101

As I wrote above, PHP is an interpreted language,

The interpreted get the commands, also called statements, and “translate” their code into tasks,

A PHP script is composed of several different statements, and the interpreter understand when a command finish and the other start by a semicolon that separate them.

Even though it is actually possible to write all the PHP code of the application in a single line and without whitespaces, it is advised (especially if you care about your mental health) to do a vast use of indentation.

You can write either a single statement or a code block which will be indicated by the brace symbols {}

Language constructs are not case-sensitive but constant and variable names are.

This means that $var, $Var and $VAR are three different variables and have three different values;

do not forget to check the official manual as well

Be careful when creating and invoking a variable

For the majority of the time, you will end up using PHP as a server-side language using it for building web pages.

One of the reasons PHP become so popular in the early stages is that it is very easy to implement it on an HTML page, the PHP parser parses only the code inside the specific tags and does not even inspect the lines outside.

There were different ways to include PHP in a page some of them now are deprecated some of them are not used anymore.

The only delimiter tags adviced at the moment are.

The first one is the most common and it is used to indicate start and finish of code blocks.

The second one is just a shortcut for

<?php doStuff(){} ?>

<?= $var

Using the echo tag is helpful to keep the syntax short and the code neater.

I am a firm believer of creating smart and neat code,

Whether web development is your career, your passion or you are lucky enough that it is both, you will spend a lot of time in front of a computer screen reading and writing code.

Having to work with a messy code with tags and statement all over the place will make your eyes tired soon, this feeling will move quickly into the brain and the outcome will be errors and more time spent in front of the screen.

A variation of these 2 tags in certain parts of your code is enough to solve this problem and make the code easier to read.

Have a look at the image above,

This is what is happening:

The PHP parser will evaluate the first conditional statement because it is within the PHP tags

Then, according to the value of the variable $color the code will create a “p” tag with either the class blue or red.

the echo tag will eventually echo out the value of the variable $greetings and the HTML will close the paragraph.

(This code will also work with curly braces on the if statement)

A little tip I can give you is to NOT use the closing tag in a PHP file.

“?>” will create a new line that will be sent to the PHP interpreter and might interfere with the HTTP header of the page or cause several other side-effects.

Is it a good practice to don’t close it.

Conclusion

“The journey of a thousand miles begins with one step.” Lao Tzu

What you saw in this post is the history and some incredibly simple examples of what PHP is, and how it should be implemented on your web pages.

Consider this post as the first small step in a long journey that must be done in order to master this server-side language.

Over the next few weeks, we will focus on different basic aspects such as the construct of the language, the types of variables and more.

Although the series may seem simple for an expert web developer you will see several details and I can guarantee that few will know everything that will be written on these pages.

so,

Stay tuned and Subscribe to be notified when new posts are published.

If you like this content and you are hungry for some more join the Facebook’s community in which we share info and news just like this one!