How do i start learning php programming?

Learn PHP

PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.

PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

Start learning PHP now »


Easy Learning with "PHP Tryit"

With our online "PHP Tryit" editor, you can edit the PHP code, and click on a button to view the result.

Example



echo "My first PHP script!";
?>


Try it Yourself »

Click on the "Try it Yourself" button to see how it works.


PHP Exercises



PHP Examples

Learn by examples! This tutorial supplements all explanations with clarifying examples.

See All PHP Examples


PHP Quiz Test

Learn by taking a quiz! This quiz will give you a signal of how much you know, or do not know, about PHP.

Start PHP Quiz!


My Learning

Track your progress with the free "My Learning" program here at W3Schools.

Log into your account, and start earning points!

This is an optional feature. You can study W3Schools without using My Learning.

How do i start learning php programming?


PHP References

W3Schools' PHP reference contains different categories of all PHP functions, keywords and constants, along with examples.


Kickstart your career

Get certified by completing the course

Get certified

w3schoolsCERTIFIED.2022



The PHP guide for beginner developers.

How do i start learning php programming?

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.
  • Step 1: your study environment.
  • Step 2: your first PHP page.
  • Step 3: variables and operations.
  • Step 4: functions.
  • Step 5: control structures.
  • Step 6: the request string.
  • Step 7: Sessions.
  • Next steps?

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:

  • XAMPP (for Windows and Mac)
  • MAMP (for Windows and Mac)
  • Laragon (for Windows)

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:

How do i start learning php programming?

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:

How do i start learning php programming?

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:

  • What is PHP?
    A very short introduction to the language (3 minutes read)
  • What can PHP do?
    An overview of what PHP can do (5 minutes 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:

How do i start learning php programming?

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:

How do i start learning php programming?

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

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

';
echo 'Hello World!';
echo '';

Note: every PHP script must start with the 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

How do i start learning php programming?

Congratulations, you just created your first PHP page!

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

  • Your first PHP-enabled page (5 minutes read)

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:

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

  • Types Introduction

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:

  • Arithmetic Operators (2 minutes read)
  • Assignment Operators (5 minutes read)
  • Comparison Operators (10 minutes read)
  • Incrementing/Decrementing Operators (3 minutes read)
  • Logical Operators (2 minutes read)
  • String Operators (2 minutes read)

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 . '
'; } } } /* 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:

  • User-defined functions (7 minutes read)
  • Function arguments (12 minutes read)
  • Returning values (3 minutes read)

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 '

Page title

'; }
/* 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 "
"; $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) . '
'; }
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 '
'; }
Output:

Toyota
BMW
Ford
Ferrari

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

  • IF block (2 minutes read)
  • ELSE block (2 minutes read)
  • ELSE IF (2 minutes read)
  • While loop (3 minutes read)
  • For loop (5 minutes read)
  • Foreach loop (5 minutes read)

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:

'; foreach ($_GET as $name => $value) { echo $name . ' -> ' . $value; echo '
'; }

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:

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

Like this:

echo 'Request parameters:

'; foreach ($_POST as $name => $value) { echo $name . ' -> ' . $value; echo '
'; }

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:

  • $_GET (3 minutes read)
  • $_POST (3 minutes read)
  • $_REQUEST (2 minutes read)

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:

  • PHP Inheritance: How to Inherit Classes in PHP
  • PHP Abstract Classes Explained
  • PHP Constructors Explained
  • PHP Interfaces Explained
  • PHP Traits Explained

Databases

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

  • How to use PHP with MySQL: the complete tutorial (with examples)

Security

Here are some tutorials about security topics:

  • SQL injection complete guide
  • How to hash passwords in PHP

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

How do I start learning PHP?

PHP Tutorial.
Easy Learning with "PHP Tryit" With our online "PHP Tryit" editor, you can edit the PHP code, and click on a button to view the result. ... .
PHP Exercises. Test Yourself With Exercises. ... .
PHP Examples. Learn by examples! ... .
PHP Quiz Test. Learn by taking a quiz! ... .
PHP References..

What should I learn first for PHP?

Step 1: Learn HTML and CSS Before you learn PHP, you should take some time to learn HTML and CSS. HTML and CSS are used to define the structure and style of a web page, respectively. They are the fundamental building blocks that make up the web.

Can I learn PHP alone?

php is a stand alone language. it's a must for server side content creators. you can learn python or any other language but you'll have to leaem the framework that connects it to the web so in my opinion learninh php is very beneficial. if you want to connect it to a database use sql.