How to learn PHP from scratch

The PHP guide for beginner developers.

How to learn PHP

Do you want to learn PHP, even if you have no experience?
Then this is the perfect guide for you.

I’m going to show you the exact steps to go from absolute beginner to PHP developer quickly and easily.

Let’s get started.



Before you begin.

In this tutorial I’m going to guide you through all the steps to become a PHP developer, starting from the very beginning.

I’ll show you which tools to install on your computer, which topics to study and in which order, and the important PHP documentation pages to read.

If you prefer an easier, faster and more efficient approach, consider enrolling in my Jump Start course.
It’s a course specifically designed for absolute beginners, 100% written by me.

In Jump Start I take care of explaining every single detail, as well as providing plenty of code examples to make it all super clear.
Look at the details and judge for yourself.

If you prefer to follow this guide for a more “do-it-yourself” approach, then keep reading.

Step 1: your study environment.

You need two things to start coding in PHP:

  • A local development environment.
  • A code editor.

Install a local development environment.

To run a PHP script, you need an interpreter that understands and runs the code.

When you open a PHP webpage online, the remote web server acts as the PHP interpreter executing the PHP code and sending the output to your browser.

A local development environment, or LDE for short, is a software package that lets you run PHP scripts on your computer.

It provides you with everything you need to develop PHP applications: the PHP interpreter, a web server, a SQL database (usually MySQL/MariaDB) and other utilities like PhpMyAdmin.

There are many free LDEs you can choose from, including:

If you’re not sure which one to go with, I suggest you try XAMPP because it’s easy and fast to set up.

This is how the XAMPP control panel looks like:


Install a code editor.

A code editor is an advanced text editor that helps you write your code.

Code editors provide useful functionalities such as:

  • line numbers
  • syntax highlighting
  • code autocompletion and assist
  • advanced search functionalities
  • plugins and extensions

Popular code editors include Atom, Brackets and Notepad++.

For example, here’s a screenshot of a PHP script opened with Atom:


Atom and Brackets are modern and simple editors.
If you prefer a more classic-looking, feature-packed editor, then Notepad++ is the right choice.
In any case, you can’t go wrong with any of them.

When you have chosen and installed your editor, you are ready to move on to the next step.

Side note:

There are also more advanced editors called IDEs (integrated developers environments), such as Eclipse, Visual Studio Code, Netbeans, and more.
They are a better choice for more experienced developers, but as long as you are learning the basics I suggest you use a simpler code editor instead.

Step 2: your first PHP page.

Before you write your first PHP page, it’s worth spending just a few minutes reading about the language basics.

Here are two pages from the official PHP documentation that I want you to read:

All right, now it’s finally time to write your first PHP Hello World script.

Start your local development environment and make sure the web server is running.

Then, open a web browser and type localhost in the URL bar. You will see the local web server’s homepage. If you are using XAMPP, the page will look like this:


Where to save your PHP scripts.

Your PHP scripts must be placed inside a directory called webserver root.

This directory is usually the htdocs directory located inside the development environment’s own installation directory.

In XAMPP, you can open this directory from the XAMPP Control Panel by clicking on the Explorer button (this will open the XAMPP main directory), and then opening the htdocs directory:


Remember: you must save your scripts in this htdocs directory.

Now open your code editor and paste the following PHP code:

echo '<h1 style="text-align: center;">';
echo 'Hello World!';
echo '</h1>';

Note: every PHP script must start with the <?php tag.

Save the file inside the htdocs directory as hello_world.php (you can use any file name you want, just be sure to use the .php extension).

Then go back to your web browser and open the URL:


Congratulations, you just created your first PHP page!

Now I suggest you read the following page to learn about a few important details:

If you have any doubt, or if you need help running your development environment, leave a comment or join my Facebook group Alex PHP café.

Now let’s dive a little bit more into the language.

Step 3: variables and operations.

If you have some experience with any programming language, I’m sure you already know what variables are.
But if you do not, don’t worry: they are very intuitive and you’ll learn them quickly.

A variable is a placeholder for a value.

Variables can store texts, numbers, Booleans (true / false), and more complex types such as arrays and objects.

PHP variables start with a dollar sign: $

For example:

/* A text variable */
$name = 'Isaac';
/* A numeric variable */
$apples = 10;
/* A Boolean variable */
$admin = FALSE;
/* An array */
$components = ['cpu', 'motherboard', 'ram'];

Here’s a useful PHP documentation page about variable types (2 minutes read):

You can perform many operations on variables. Including:

  • Merging text strings together.
  • Performing arithmetic operations on numbers.
  • Comparing two variables to see if they are the equal.
  • … and much more.

Here are a couple of simple examples:

/* Merging two text variables together */
$name = 'Isaac';
$surname = 'Asimov';
$fullName = $name . ' ' . $surname;
echo $fullName; // Output: Isaac Asimov
/* Arithmetic operations */
$apples = 10;
$oranges = 5;
$fruit = $apples + $oranges;
echo $fruit; // Output: 15

Before moving on, I want you to read the following PHP documentation pages about variables and comparison:

It’s perfectly fine if you don’t remember everything. Your goal is to discover what you can do with PHP and understand its potential.

Step 4: functions.

Functions are one of the basic tools of programming languages.

A function is identified by:

  • A name.
  • A list of input values called arguments.
  • A return value.

Functions perform operations on the passed arguments and then return a value back.

Here are a couple of examples:

/* The date() function returns the current datetime.
   It takes a string as an argument to define the format of the returned datetime.
echo date('Y-m-d'); // Prints: 2022-07-31
echo date('H:i:s'); // Prints: 15:03:12
/* The substr() function returns part of a string.
   It takes three arguments:
   1. The original string.
   2. The start offset (number of characters from the start)
   3. The length of the returned string.
echo substr('hello', 1, 3); // Prints: ell
echo substr('hello', 4, 1); // Prints: o

You can use the many functions provided by the PHP library, as well as make your own functions.

Here’s how to define and use your own function:

/* Create a function that prints all odd numbers between two numbers. */
function printOddNumbers(int $start, int $end) {
   foreach (range($start, $end) as $number) {
      if (($number % 2) != 0) {
         echo $number . '<br>';
/* Print all odd numbers from 10 to 30 */
printOddNumbers(10, 30);

This is the output:


Before moving on to the next section, read the following PHP documentation pages about functions:

Step 5: control structures.

With PHP, you can execute a code block conditionally depending on specific factors.

For example, you can decide whether to display an HTML string depending on the value of a variable.

The PHP tools that let you make such decisions are called control structures.

By using control structures you can implement two fundamental programming patterns: conditional execution and loops.

Conditional execution means that a piece of code is executed depending on a value. The main control structure for conditional execution is the IF/ELSE block.

For example:

/* Show the title only if the $showTitle variable is TRUE */
$showTitle = FALSE;
if ($showTitle) {
   echo '<h1>Page title</h1>';
/* Perform a division only if the divisor is not zero */
$dividend = 20;
$divisor = 5;
if ($divisor == 0) {
   echo 'Cannot divide by zero.';
else {
   echo 'Result: ' . $dividend / $divisor;

Loops let you repeat the same piece of code a specific number of times, or until a condition is met. The most common loop control structures are WHILE loops, FOR loops and FOREACH loops.

Here are some examples:

/* While loop: count all numbers between two provided numbers */
$start = 5;
$end = 15;
while ($start <= $end) {
   echo $start;
   echo "<br>";
/* For loop: execute a command 5 times  */
for ($i = 0; $i < 5; $i++) {
   echo 'Step #' . ($i + 1) . '<br>';
Step #1
Step #2
Step #3
Step #4
Step #5
/* Foreach loop: print all the elements of an array  */
$cars = ['Toyota', 'BMW', 'Ford', 'Ferrari'];
foreach ($cars as $car) {
   echo $car;
   echo '<br>';

Now, I want to read the following PHP documentation pages to learn about control structures:

Step 6: the request string.

Data is sent from the web browser to the PHP script by using the Request String.

The request string is a set of key=value pairs called request parameters.

The key is the name of the parameter, and the value is a text string value associated with that parameter.
For example, “username=my user” and “password=my password”.

The request string can be sent in two ways:

  • In GET mode, directly in the URL. For example: page.php?key1=value1&key2=value2
  • In POST mode, not visible in the URL.

In every PHP script, the GET parameters are automatically available into the special array $_GET.

$_GET is an array with string keys. Each element of $_GET has the parameter’s name as key and the parameter’s value as value.

For example, let’s say that you have the following URL request:


And the print_get.php script contains the following code:

echo 'Request parameters:<br><br>';
foreach ($_GET as $name => $value) {
   echo $name . ' -> ' . $value;
   echo '<br>';

This is the output you’ll get:

Request parameters:
username -> myUser
password -> myPassword
login -> 1

POST data is available in the $_POST array (instead of $_GET).

POST is often used for HTML forms.

For example, let’s say that you have this HTML form that uses the POST method:

<form method="POST">
<input type="hidden" name="image_id" value="3">
<input type="hidden" name="image_type" value="png">
<input type="hidden" name="image_size" value="2048">
<input type="submit">

To print the data from that form, you need to look into the $_POST array.

Like this:

echo 'Request parameters:<br><br>';
foreach ($_POST as $name => $value) {
   echo $name . ' -> ' . $value;
   echo '<br>';


Request parameters:
image_id -> 3
image_type -> png
image_size -> 2048

The $_REQUEST array contains the elements from both POST and GET data.

In other words, it includes the elements of both $_POST and $_GET.

If you want your PHP script to work with any request method, you can use $_REQUEST instead of $_POST or $_GET.

Now go and read the following PHP documentation pages about $_GET, $_POST and $_REQUEST:


In the Jump Start course everything is explained by me, without the need to read the PHP documentation pages. I also provide many more examples as well as in-depth explanations.

Step 7: Sessions.

PHP uses Sessions to keep track of consecutive remote user access.

Sessions are used extensively for user authentication to keep track of user logins, in e-commerce websites to keep track of purchases and in many other scenarios.

Almost every PHP-based website uses Sessions.

It’s a good idea to learn Sessions from the start, because they will let you write stateful websites very easily.
You can go straight to my Sessions complete guide to learn how to use Sessions in practice and to see some code examples.

Next steps?

At this point, you already have a solid background to build your own PHP apps.
Congratulations for staying with me all through the way!

Of course, there is much more about PHP than just the basics.

When you feel ready to dive into more advanced topics, I suggest you start from these three topics:

  • Object-Oriented programming, or OOP
  • Databases
  • Security

Object-Oriented Programming

To get started, you can look at the following tutorials:


Here’s my complete guide about using PHP with MySQL:


Here are some tutorials about security topics:


In this step-by-step guide you have learned how to get started with PHP, including how to install a development environment and a code editor.

Then, you went through all the basic PHP functionalities such as variables, functions, control structures and Sessions.

Make sure to leave a comment and let me know your personal experience with learning PHP.

P.s. If this guide has been helpful to you, please spend a second of your time to share it. Thanks!



Image copyright: Remote learning vector created by vectorjuice –

73 thoughts on “How to learn PHP from scratch”

  1. Dear Alex

    Good day,

    We are developing a logistics platform, and unfortunately the developer could not complete the Platform due to some health issue, the platform or system build with PHP – and now we are looking for best option to complete the system, we have divided into 3 phases, the phases one is more for external use with a stakeholders such as ( Customer, shipper, shipping line, freight companies and others, and as per our knowledge 80% of the phase one completed where we need support to complete 20% – and our target was to release the phase one on Nov 2021 to the market, but unfortunately this is what happened to us.

    Thanks and looking forward for your support and advise

    Mansoor Al Rawahi -CEO of Thunder Logistics

    • Hello,
      I guess you need to look for another developer or to outsource the job to a development team.
      If your previous developer is able to offer some support (even just a phone call to describe how the app works) that would save a lot of time.

  2. Thanks for sharing this step by step information about PHP and read your complete post, you have explained in really very understanding method so keep posting such informative blog post.

  3. Thanks For Sharing such a wonderful article the way you presented is really amazing It was extraordinarily simple to discover my way around and amazingly clear, this is staggering!!!

  4. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck with the upcoming articles

  5. I have learnt php but I have a problem to write a php code I confused at the time to write a code so what can I do?

    • Yes, a basic knowledge of HTML and CSS is required for any web project.
      I don’t have such tutorials on my website, but you can easily find many of them on Google.

    • If you wanna learn HTML & CSS, just go to W3school they have all the tutorials stepwise. And if you still don’t understand some part, you can search it on youtube for better grip/understanding.

  6. Really happy that I get to find your blog on how to learn PHP. These are must read for all. This article is a good one to look at. You will get best review over here and would suggest others too. Great blog indeed, will visit again future to read new stuff!!


Leave a Comment