PHP Lessons - Part 1 - PHP Programming Basics
In this part of the PHP tutorial we will go over the basics of the PHP language from the very beginning. As you learn more and more about PHP, the examples will become more complex.
Basic PHP Syntax - Lesson 1
This lesson marks the real beginning of our PHP journey. Here, we’ll start executing PHP scripts and generating HTML pages using PHP code. Right from the first lesson—we’re diving in! I’ve intentionally named this one "Lesson 1" because I prefer action over lengthy introductions. If you’re curious about the history of programming languages or PHP itself, you can explore that later on your own.
PHP Lessons - Lesson 2 - Variables in PHP
PHP variables are used to store values such as strings, numbers, or arrays. Once a variable is declared, it can be reused throughout your script. All variables in PHP begin with the $ symbol.
Variables are one of the core concepts in programming. To understand programming, you must have a clear understanding of what a variable is, how and where it is stored, and what happens to it during code execution. A variable is a value that can change during program execution.
PHP Lessons - Lesson 3 - PHP String Variables
In the previous lesson, we discussed that PHP supports variables. In this lesson, we'll take a look at one type of variable — string variables.
String variables in PHP are used to store values that consist of characters. A PHP string can be saved in a variable. Below is a PHP script that assigns the text "Hello, World!" to the string variable $txt
:
<?php
$txt = "Hello, World!";
echo $txt;
?>
The result of the code above:
PHP Lessons - Lesson 4 - PHP Numeric Variables
In the previous lesson, we explored string variables. Now, let’s take a look at numeric variables. The simplest type of number is the integer.
Integers in PHP
Integers are whole numbers in the range from –2,147,483,648
to 2,147,483,647
. This range is determined by the 32-bit memory space used to store integers.
PHP Lessons - Lesson 5 - PHP Logical Variables
So far, we’ve learned that PHP supports numbers and strings. But this is not all—this lesson introduces another type of variable: Boolean variables. They are called Boolean because they are used to build logical structures in code.
PHP Lessons - Lesson 6 - PHP Operators
We’ve already explored string and numeric variables, and learned that numeric variables in PHP can be of various types: integers, floats, booleans. Now it’s time to learn how to operate on them, modify them, and perform arithmetic.
Below are tables illustrating the use of different PHP operators.
PHP Lessons - Lesson 7 - PHP Functions and Functional Programming
I think we've reached the point where it's time... it's time to finally start programming. After this lesson, you can say you've written code in PHP. Quite often, you'll need to reuse pieces of code on different pages or in different PHP files. To avoid duplicating code, PHP provides functions.
PHP Lessons - Lesson 8 - The if statement
Quite often, depending on circumstances, we have to make decisions. In programming—just like in life—actions depend on conditions. In life we use our head to make decisions, in PHP we use the if statement. For example: if it rains, I’ll take an umbrella; if it’s warm, we’ll go to the beach. In PHP, we evaluate expressions for truth and perform actions accordingly:
PHP Lessons - Lesson 8-2 - The switch statement
In the previous lesson, we learned about the if
statement. The if
statement allows us to check a condition and execute specific actions based on the result.
Now imagine you need to perform ten different checks and execute different actions based on each result. Sure, you could write this using multiple if
statements: