If you’re starting web development, understanding the basics of PHP is essential. PHP is a popular server-side scripting language used to build dynamic websites and web applications. This guide walks you through the PHP fundamentals, introduces a simple PHP program, and helps you write basic code for PHP that you can use in your own projects.
Table of Contents
- What Is PHP? – The Very Basics of PHP
- An Example to Illustrate the Basics of PHP
- Setting Up PHP: Tools You Need
- Writing a Simple PHP Program
- Understanding PHP Syntax: The Basics of PHP Language
- Creating a Simple PHP Website Code Structure
- Summary of PHP Fundamentals and Basics of PHP
- Basics of PHP FAQ
- Final Thoughts on the Basics of PHP
What Is PHP? – The Very Basics of PHP
PHP (Hypertext Preprocessor) is an open-source, server-side scripting language designed for web development. It runs on the server and generates HTML that is sent to the browser. PHP is especially useful for working with databases, handling forms, managing sessions, and more.
Find the official PHP website at: https://www.php.net/
An Example to Illustrate the Basics of PHP
A simple example of a PHP program is to insert the current date in a web page. This demonstrates why PHP is a server side language and what a dynamic website is. A static web page can’t show the current date. This is why it is static, because it is unchanging. When a user surfs to a static web page, the web server simply sends a copy of the web page back to the user’s web browser. None of the contents of the web page is processed or changed.
Getting a Static Web Page
In the image below a user surfs to a web page using a web browser. The request goes over the internet to the server where the requested page is located. After receiving the web page request, the server simply returns the requested web page to the web browser via the internet.

Static Web Page Code
Here is some example HTML code for a very basic web page. This type of file is named with a .html file extension, for example index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to My Web Page</h1>
<p>Enjoy your stay!</p>
</body>
</html>
The above HTML code looks as follows when loaded into a web browser.

Getting a Dynamic PHP Web Page
A PHP web page can have code programmed into it to fetch the current date from the web server. When a user surfs to the web page using a web browser, the web server detect that someone is requesting the web page. The PHP code in the web page is then executed or run. In this example, it fetches the current date from the web server and inserts it into the HTML of the page. This page that was updated with the current date is what is displayed in the user’s browser.
The following diagram shows that the user requests a web page over the internet. The request arrives at the server where the server knows that the requested page is a PHP page. As a result, it hands the request over to the PHP processor which runs any PHP code in the page, and generates an HTML web page as output. This is what is returned to the web browser. In addition to just executing the PHP code, the PHP code can fetch data from a database if needed.

In this simple example, whenever a user visits the web page it dynamically inserts the current date into the page. This explains the very basics of what a dynamic website is. PHP is a server side language, because when the user surfs to, or requests the web page, PHP executes PHP code in the web page on the server and returns the resulting HTML output.
Dynamic PHP Web Page Code
The static web page shown previously can be changed into a PHP page to display the current date. Firstly the web page file must be renamed so that the web server knows that it is a PHP page. A PHP file must have a .php file extension. For example, the static page that was named index.html must be renamed to index.php.
In addition, PHP must be installed and enabled on the web server in order to run the PHP code in the page. PHP code can now be inserted into the page between an opening <?php
tag and a closing ?>
tag. The following code shows PHP code added to the page. Look for the following PHP code in the listing below:
<?php echo date('l jS \\of F Y h:i:s A'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to My Web Page</h1>
<p>Enjoy your stay!</p>
<p>Today's date is: <?php echo date('l jS \of F Y h:i:s A'); ?></p>
</body>
</html>
The above mix of HTML code and PHP produces the following web page in a web browser.

How the Basic PHP Code Works
As can be seen in this example, a PHP page can mix both HTML and PHP. When this page is requested by the browser, the web server hands the page to the PHP processor. The PHP processor goes through the page and finds the code between the PHP tags and executes it, inserting the result back into the HTML page. This modified HTML page is sent back to the web browser that requested it.
Basics of PHP date( ) Function
In the above example, the PHP code that was run is as follows:
<?php echo date('l jS \of F Y h:i:s A'); ?>
This code calls the PHP date( ) function and passes it some information on how to display the date. In this case a long date format of day, date, month, year, hour, minute, second, and AM or PM is specified and passed to the date( ) function. This function can also be used very simply, for example to display only the current year, use: date('Y');
This outputs the current year, e.g. 2025.
In addition to calling the date() function, echo
is called to display the output of the date() function in the HTML code. This causes the output of the date function to be injected into the HTML code. The resulting code is as follows.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to My Web Page</h1>
<p>Enjoy your stay!</p>
<p>Today's date is: Sunday 15th of June 2025 04:43:40 PM</p>
</body>
</html>
PHP can be inserted into a web page as this example shows. In addition the entire contents of a web page can be PHP code that starts with an opening PHP tag. Although this example showed one line of PHP code inserted into a page, many PHP tags can be inserted throughout the page. When the page is passed to the PHP processor, it will find and execute each piece of PHP code in the page.
Setting Up PHP: Tools You Need
Before diving into the basics of PHP, make sure you have the following:
- A web server like Apache (XAMPP, MAMP, or WAMP)
- PHP installed (included in XAMPP/WAMP)
- A text editor or IDE (VS Code, Sublime, PhpStorm)
Details of the above follow in the sub-sections below.
A Web Server with PHP
Web server software must be installed on your development computer because the code in the PHP pages that you write will not execute or be run otherwise. Software like XAMPP is a web server with PHP and database software all in one for development on a local computer.
Once your environment is set up, you can begin writing basic code for PHP.
I have used XAMPP to run a local PHP enabled web server on both my Linux and Windows computers. Find XAMPP at: https://www.apachefriends.org/index.html
Download XAMPP at: https://www.apachefriends.org/download.html
Install this software on your computer. For instructions on how to install XAMPP, see the Documentation / FAQ section on the download page for the operating system that you are using.
After installing XAMPP and starting it, browse to http://localhost/
on your computer to get more documentation and links to installed utilities, such as phpMyAdmin. To set up more than one local development website, look at the documentation ‘Configure Virtual Hosts’ under the ‘HOW-TO Guides’ menu item on the localhost page.
A Code Editor
A popular free code editor that many PHP developers and web developers use is VS Code. This code editor runs on Windows, Apple computers, and on Linux computers.
Writing a Simple PHP Program
As a further example to the previous example that used the PHP date()
function, let’s now continue with a simple PHP program that outputs a message:
<?php
echo "Hello, World!";
?>
Save this file as index.php
, place it in your server’s root directory (e.g., htdocs
for XAMPP), and open it in your browser. You should see:
Hello, World!
In order for this code to work, your server such as XAMPP must be installed and running. You must access the URL of the development server through your web browser. For example if you set up a development server called ‘test’ at http://test/ on your computer using virtual hosts, then you must access it at this address so that the PHP processor runs and executes the code. Or if you installed it in the XAMPP htdocs folder, then you should browse to http://localhost/ to see the web page.
This simple example demonstrates how PHP code is embedded within <?php ?>
tags and executed on the server. This code is even simpler than the previous example, as it does not include the basic HTML page tags, such as the head and body. As a result, the browser simply displays the above text without any HTML tags.
Understanding PHP Syntax: The Basics of PHP Language
Here are some core PHP fundamentals to learn first:
Basics of PHP Variables
In PHP, variables are used to store data such as numbers, text (strings), arrays, or more complex types. Every variable in PHP begins with a dollar sign ($
), followed by the name of the variable. For example:
$name = "Alice";
$age = 30;
Rules for Naming Variables
- Variable names must start with a dollar sign (
$
). - The name must begin with a letter or an underscore (
_
), not a number. - After the first character, you can use letters, numbers, or underscores.
- Variable names in PHP are case-sensitive, so
$Name
and$name
would be treated as two different variables. - Avoid using PHP reserved keywords (like
class
,echo
,function
, etc.) as variable names.
Valid examples:
$user_name = "admin";
$_count = 10;
$score2 = 95;
Invalid examples:
$2ndPlace = "Bob"; // Starts with a number – not allowed
$my-name = "John"; // Contains a hyphen – not allowed
Storing Different Data Types
PHP variables are loosely typed, which means you don’t need to declare a data type explicitly — PHP determines the type automatically based on the assigned value. For example:
$greeting = "Hello, world!"; // String
$year = 2025; // Integer
$price = 19.99; // Float
$is_logged_in = true; // Boolean
You can also change the type later:
$value = "100"; // Initially a string
$value = 100; // Now an integer
This flexibility makes PHP easy to work with, especially for beginners.
Using Variables
Once a variable is defined, you can use it in expressions, output it with echo
, or pass it to functions:
$firstName = "Jane";
echo "Welcome, " . $firstName . "!";
This would output:
Welcome, Jane!
Basics of PHP Data Types Explained
In PHP, every variable holds a value of a specific data type, which determines what kind of data it contains and what operations can be performed on it. PHP is a loosely typed language, meaning you don’t need to declare the data type explicitly — PHP figures it out at runtime based on the value assigned.
Here are the main data types in PHP:
1. String
A string is a sequence of characters enclosed in quotes. You can use either single quotes ('
) or double quotes ("
), though double quotes allow variable interpolation and escape sequences.
$name = "Alice";
$greeting = 'Hello, world!';
Note: Using double quotes will interpret variables and special characters, while single quotes will not.
echo "Hello, $name"; // Outputs: Hello, Alice
echo 'Hello, $name'; // Outputs: Hello, $name
2. Integer
An integer is a whole number without a decimal point. It can be positive or negative.
$age = 30;
$year = -2025;
3. Float (or Double)
A float is a number with a decimal point or in exponential form. PHP also refers to floating-point numbers as “doubles.”
$price = 19.99;
$pi = 3.14159;
4. Boolean
Booleans represent true or false values and are commonly used in conditions and logic.
$is_logged_in = true;
$is_admin = false;
When used in conditions, PHP treats false
, 0
, ""
, null
, and empty arrays as false, and everything else as true.
5. Array
Arrays are used to store multiple values in a single variable. PHP supports both indexed arrays (numerically indexed) and associative arrays (key-value pairs).
// Indexed array
$colors = ["red", "green", "blue"];
// Associative array
$user = [
"name" => "Alice",
"age" => 30,
"email" => "alice@example.com"
];
6. Object
Objects are instances of user-defined classes. They allow you to store data and functions in a single entity.
class Car {
public $brand;
function setBrand($name) {
$this->brand = $name;
}
}
$myCar = new Car();
$myCar->setBrand("Toyota");
echo $myCar->brand; // Outputs: Toyota
7. NULL
The NULL
data type represents a variable with no value. It’s used to indicate that a variable is empty or uninitialized.
$user = null;
Understanding these data types is a fundamental part of learning PHP. As you write more code, you’ll become familiar with how PHP handles type conversion automatically — but being aware of types helps avoid bugs and write clearer logic.
Basics of PHP Operators
Operators in PHP are symbols that perform operations on variables and values. They are essential for performing tasks such as arithmetic, comparisons, and logical evaluations.
Example: Arithmetic Operator – Addition (+
)
The following example demonstrates the addition operator (+
), which is used to add the values of two variables:
$x = 10;
$y = 5;
$sum = $x + $y;
echo $sum; // Outputs: 15
In this code:
$x
holds the value10
$y
holds the value5
$sum
stores the result of adding$x
and$y
Other Common PHP Operators
- Subtraction (
-
):$result = $x - $y;
- Multiplication (
*
):$result = $x * $y;
- Division (
/
):$result = $x / $y;
- Modulus (
%
):$result = $x % $y;
(returns the remainder)
PHP also supports assignment operators, comparison operators, increment/decrement, logical, bitwise, and more.
As you build more complex logic, you’ll find operators are a fundamental part of almost every PHP expression or condition.
Control Structures in PHP
Control structures allow your code to make decisions and perform different actions based on conditions.
Example: if
/ else
Statement
$age = 18;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are underage.";
}
In this example:
- The
if
condition checks whether$age
is greater than or equal to 18. - If the condition is true, it executes the first block and prints
"You are an adult."
. - If the condition is false, it runs the
else
block and prints"You are underage."
.
PHP also supports other control structures such as else if
, switch
, and different types of loops (for
, while
, etc.), allowing for more complex decision-making.
Basics of PHP Functions
Functions are reusable blocks of code that perform a specific task. They help keep your code organized and reduce repetition.
Example: Defining and Calling a Function
function greet($name) {
return "Hello, $name!";
}
echo greet("Alice");
Explanation:
- The
greet
function accepts one parameter:$name
. - It returns a greeting string that includes the name.
- Calling
greet("Alice")
returns"Hello, Alice!"
, which is then printed usingecho
.
You can define your own custom functions or use PHP’s many built-in functions. Functions can take multiple parameters, return any data type, and help make your code modular and readable.
Creating a Simple PHP Website Code Structure
To see how everything fits together, here’s an example of simple PHP website code:
index.php
<?php
$title = "My Simple PHP Page";
$year = date("Y");
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome to <?php echo $title; ?></h1>
<p>This is a basic example of a dynamic PHP page.</p>
<footer>© <?php echo $year; ?></footer>
</body>
</html>
This combines HTML with PHP to dynamically generate content — a cornerstone of PHP web development.
Summary of PHP Fundamentals and Basics of PHP
Here’s a quick recap of the most important basics of PHP:
- PHP scripts start with
<?php
and end with?>
- Variables start with
$
- Statements end with a semicolon (
;
) - Use
echo
orprint
to output data - PHP can be embedded into HTML
- You can build complete web pages with simple PHP website code
Basics of PHP FAQ
Is PHP good for beginners?
Yes. The basics of PHP are easy to learn, making it great for beginners in web development.
Can I use PHP with HTML?
Absolutely. PHP can be embedded directly in HTML files to create dynamic web pages.
What is the difference between PHP and JavaScript?
PHP is a server-side language; JavaScript runs in the browser. PHP handles backend logic, while JavaScript handles client-side interactions.
How do I run PHP code?
Use a local server environment (like XAMPP or WAMP) and access your PHP files via the browser using http://localhost/
.
Final Thoughts on the Basics of PHP
Mastering the basics of PHP gives you a strong foundation to build dynamic and interactive websites. Whether you’re following a basic PHP tutorial or creating a simple PHP program, learning the basics of the PHP language is a practical first step in your programming journey.
Ready to go deeper that the basics of PHP? Check out our PHP category on Programmer Notes for more advanced topics.