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.

 

Contents


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:

XAMPP
 

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
 

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:

<?php

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:
http://localhost/hello_world.php

 

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:

<?php

/* 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:

11
13
15
17
19
21
23
25
27
29
 

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>";
   $start++;
}
Output:

5
6
7
8
9
10
11
12
13
14
15
 
/* For loop: execute a command 5 times  */

for ($i = 0; $i < 5; $i++) {
   
   echo 'Step #' . ($i + 1) . '<br>';
}
Output:

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>';
}
Output:

Toyota
BMW
Ford
Ferrari
 

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:

http://localhost/print_get.php?username=myUser&password=myPassword&login=1

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">
</form>
 

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>';
}
 

Output:

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:

P.s.

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:

Databases

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

Security

Here are some tutorials about security topics:

Conclusion.

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!

Alex

Do You Want to Improve Your PHP Skills?

Grow your PHP skills every week, with my free weekly PHP tips.

 

Image copyright: Remote learning vector created by vectorjuice – www.freepik.com