PHP Basics

This article introduces the most important PHP basics, including installation, syntax, comments, variables, echo and print, data types, strings, numbers, casting, math, constants, and magic constants.

Jun 10, 2026
PHP Basics

PHP Basics: Install, Syntax, Variables, Data Types, and Core Concepts

PHP is a server-side programming language used to build dynamic websites, backend systems, APIs, dashboards, and database-driven applications. Before building real PHP projects, it is important to understand the basic concepts that form the foundation of the language.

This article gives a clear introduction to the most important PHP basics, including installation, syntax, comments, variables, echo and print, data types, strings, numbers, casting, math operations, constants, and magic constants.

PHP Install

Before writing PHP code, you need a working PHP environment. PHP runs on the server side, so your computer needs PHP installed in order to execute PHP files.

For beginners, the easiest way to start is by using a local development environment such as XAMPP, Laragon, MAMP, or a built-in PHP server. These tools help you run PHP code locally without needing a real online server.

A basic PHP file usually has the .php extension. After installing PHP, you can create a file such as index.php and run it through a local server to see the output in your browser.

PHP Syntax

PHP code is written inside special PHP tags. These tags tell the server where the PHP code starts and ends.

<?php
echo "Hello, PHP!";
?>

PHP statements usually end with a semicolon. The semicolon tells PHP that the current instruction is finished.

PHP can be written together with HTML, which makes it useful for generating dynamic web pages. The server executes the PHP code first, then sends the final HTML result to the browser.

PHP Comments

Comments are notes written inside the code. They are ignored by PHP and do not affect the output. Developers use comments to explain code, leave reminders, or make the code easier to understand later.

PHP supports single-line comments and multi-line comments.

<?php
// This is a single-line comment

# This is also a single-line comment

/*
This is a multi-line comment.
It can contain more than one line.
*/
?>

Good comments can make a project easier to maintain, especially when the code grows larger or when multiple developers work on the same application.

PHP Variables

Variables are used to store values in PHP. A variable starts with the dollar sign $, followed by the variable name.

<?php
$name = "Adnan";
$age = 30;

echo $name;
?>

Variables can store different types of data, such as text, numbers, true or false values, arrays, and objects.

Variable names should be clear and meaningful. For example, $userName is better than $x because it explains what the variable represents.

PHP Echo / Print

PHP provides echo and print to output text or values to the page. Both can be used to display content in the browser.

<?php
echo "Hello from echo!";
print "Hello from print!";
?>

The echo statement is commonly used because it is simple and can output multiple values. The print statement is also valid, but it is used less often in modern PHP examples.

In real projects, echo may be used to display dynamic values such as usernames, article titles, messages, or calculated results.

PHP Data Types

Data types describe the kind of value stored in a variable. PHP supports several important data types.

Common PHP data types include:

  • String: text values such as names and messages.

  • Integer: whole numbers.

  • Float: decimal numbers.

  • Boolean: true or false values.

  • Array: a collection of values.

  • Object: an instance of a class.

  • NULL: a variable with no value.

<?php
$name = "PHP";
$year = 1995;
$price = 19.99;
$isActive = true;
$items = ["HTML", "CSS", "PHP"];
?>

Understanding data types is important because every operation in PHP depends on the kind of value you are working with.

PHP Strings

A string is a sequence of characters. Strings are used to store text such as names, titles, descriptions, emails, and messages.

<?php
$message = "Welcome to PHP";
echo $message;
?>

PHP provides many useful functions for working with strings, such as measuring string length, converting text to uppercase or lowercase, replacing text, and searching inside strings.

<?php
$text = "Learning PHP";

echo strlen($text);
echo strtoupper($text);
?>

Strings are used heavily in web development because websites display text content, labels, messages, form values, and database records.

PHP Numbers

PHP supports different types of numbers. The most common numeric types are integers and floats.

An integer is a whole number without a decimal point, while a float is a number with a decimal point.

<?php
$quantity = 10;
$price = 15.75;

echo $quantity;
echo $price;
?>

Numbers are used in calculations, prices, counters, statistics, pagination, ratings, and many other parts of web applications.

PHP Casting

Casting means converting a value from one data type to another. In PHP, you can cast values to types such as integer, float, string, boolean, array, or object.

<?php
$value = "100";

$number = (int) $value;

echo $number;
?>

Casting is useful when data comes from user input, forms, URLs, or APIs. For example, form input is often received as a string, even if the value looks like a number.

By casting values correctly, developers can avoid unexpected behavior and make the code more predictable.

PHP Math

PHP can perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.

<?php
$a = 10;
$b = 3;

echo $a + $b;
echo $a - $b;
echo $a * $b;
echo $a / $b;
echo $a % $b;
?>

PHP also provides built-in math functions such as round(), ceil(), floor(), min(), max(), and rand().

<?php
echo round(12.7);
echo max(5, 10, 20);
echo rand(1, 100);
?>

Math operations are useful in many real applications, including invoices, statistics, reports, discounts, shopping carts, and dashboards.

PHP Constants

A constant is a value that cannot be changed after it is defined. Constants are useful for values that should remain fixed throughout the application.

<?php
define("SITE_NAME", "My PHP Website");

echo SITE_NAME;
?>

Constants are commonly used for configuration values, fixed labels, application settings, and values that should not be modified accidentally.

Unlike variables, constants do not use the dollar sign. By convention, constant names are often written in uppercase letters.

PHP Magic Constants

Magic constants are special predefined constants in PHP. Their values change depending on where they are used in the code.

Common PHP magic constants include:

  • __LINE__: returns the current line number.

  • __FILE__: returns the full path of the current file.

  • __DIR__: returns the directory of the current file.

  • __FUNCTION__: returns the current function name.

  • __CLASS__: returns the current class name.

  • __METHOD__: returns the current class method name.

  • __NAMESPACE__: returns the current namespace name.

<?php
echo __FILE__;
echo __DIR__;
echo __LINE__;
?>

Magic constants are useful for debugging, logging, file paths, class-based development, and understanding where code is being executed.

How These PHP Basics Work Together

These basic PHP concepts are connected. A real PHP script may use variables to store data, strings to handle text, numbers for calculations, comments to explain logic, constants for fixed settings, and echo to display the final result.

For example, a simple page may define a website name as a constant, store a username in a variable, calculate a value using math, and display a welcome message using echo.

<?php
define("SITE_NAME", "PHP Learning");

$userName = "Adnan";
$lessonsCompleted = 3;
$totalLessons = 12;

echo "Welcome " . $userName . " to " . SITE_NAME;
echo "You completed " . $lessonsCompleted . " of " . $totalLessons . " lessons.";
?>

This small example combines constants, variables, strings, numbers, concatenation, and output in one simple script.

Conclusion

Learning PHP basics is the first step toward building real web applications. Concepts such as installation, syntax, comments, variables, echo and print, data types, strings, numbers, casting, math, constants, and magic constants help you understand how PHP code works.

Once these fundamentals become clear, it becomes easier to learn conditions, loops, arrays, functions, forms, databases, object-oriented programming, and frameworks such as Laravel.

In the next articles of this PHP series, we will explain each topic step by step with practical examples and beginner-friendly exercises.