Learning PHP – Introduction

Learning PHP
Views: 41
0 0
Read Time:4 Minute, 12 Second

With this series of article I want to introduce you to one of the most used languages of these days: PHP.

Introduction

Welcome to Learning PHP – Introduction! This is the first of a series of articles that is introducing you to one of the most used languages of these days: PHP.

Believe it or not as of January 2022 W3Techs reports that “PHP is used by 78% of all the websites whose server-side programming language we know (https://w3techs.com/technologies/details/pl-php). Some of the websites that uses PHP includes Facebook, Wikipedia, Instagram, Slack, Opensea…and so on. There are infinite articles on why PHP is still so important, but in general because: it’s widely used, it’s easy to learn and use, it’s free, there is a big community behind and has a great documentation, and, more important, is still being required if you are looking for a job as Web Developer.

What is PHP

PHP is a scripting language, and by “scripting language” is meant that is a language that is interpreted at run-time rather than compiled. This means that you will write your lines of code into your .php file and when is being invoked for the execution an interpreter will load the file, will parse, convert into something that the computer can understand and execute.

What PHP can do

Technically with PHP you can do anything and there are three main areas where PHP is being used (https://www.php.net/manual/en/intro-whatcando.php) :

  • Server side scripting
  • Command line scripts or tools
  • Desktop apps

Where can be used

PHP can be used on all the major OS and web servers. You can find all the version you want here (if you have Windows, make sure you look for the “Windows downloads”)https://www.php.net/downloads

Getting Started

For our learning lessons (at least for big part of them) we are going to use a simple Dockerfile , but if you have it accessible on your computer even better, just skip the following steps.

Docker environment setup

For using your Dockerfile you must have docker installed (just in case you missed):

Create a folder on your computer and create a file in it called Dockerfile, and add the content below, i.e. you can create the Docker file in /user/work/learning-php

from php

Once this step is done, run this command from the folder in which that file is

docker build -t learning-php .

This operation will take one minute or two and as end result you will have your docker image with php running on it. Now we will make it run using that folder as our base folder mounted in the docker image.

What we have to do is to create a new folder “examples” in the /user/work/learning-php folder (just in case run this command

mkdir -p /user/work/learning-php/examples

At this stage our directory should look something like

learning-php/
-- Dockerfile
-- examples/

So far nothing extremely complex.

Now what we can do is to create another file (lesson-01.php) in examples/

touch /user/work/learning-php/examples/lesson-01.php

Now run this magic command

docker run -it -v /user/work/learning-php/examples/:/home  --name learning_container learning-php /bin/bash

What this command will do is to instantiate a new container called “learning-php” and link the /home container folder to your /user/work/learning-php/examples (mount operation). In this way you can edit files in the examples/ folder and this will be automatically reflected in the container. All you need to do is to access to the container shell for executing the php script and check the output. Everything is now ready for executing our first PHP script.

Your first PHP script

In the examples/ folder create lesson-01.php with this content and save:

<?php
    # lessson-01.php
    echo "Hello World!\n";
?>

Where:

  • <?php is the opening tag
  • # lesson-01.php is a comment
  • echo “Hello World!\n”; is an instruction that is telling to echo
  • ?> is the closing tag

To execute this script all we have to do is to run this command

php lesson-01.php

that will produce (guess a bit)

root@e3421893ae4a:/home# php lesson-01.php
Hello World!

This is the end of this introduction, next we are going through other topics that will give you a initial understanding of the basic constructs the PHP language has to offer

Learning PHP – Introduction

Learning PHP – print and echo

Learning PHP – The variables

Learning PHP – if else

Learning PHP – Arrays

Learning PHP – The loops

Learning PHP – The Functions

If you really like this series of articles, please share and help us to grow.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
setup Phalcon 5 Previous post How to setup Phalcon 5 framework in a Docker container V2
PHP print and echo Next post Learning PHP – Lesson 1 – PHP print and echo
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x