Guide

PHP Variables, Data Types and Control Flow for Beginners

Learn PHP variables, data types, operators, conditions and loops in plain language. A beginner guide to the core building blocks of PHP programs.

Key takeaways

  • A PHP variable starts with a dollar sign, and its name is case-sensitive.
  • PHP is dynamically typed by default: the type follows the value, and optional type declarations add strictness.
  • The built-in types are null, bool, int, float, string, array, object, callable and resource.
  • Prefer strict comparison, which checks value and type, over loose comparison, which converts types first.
  • Use foreach for arrays, and make sure every loop has a way to end.

The building blocks of any program

Every programming language gives you a few core tools: places to store values, ways to describe different kinds of data, and ways to decide what happens next. In PHP these are variables, data types and control flow. This guide explains them in plain language, without code listings, so that you understand the ideas before you start typing.

What is a variable in PHP?

A variable is a named container for a value. In PHP a variable name begins with a dollar sign followed by a name. The name can contain letters, digits and underscores, but it cannot start with a digit. Names are case sensitive, so a name written in capitals is a different variable from the same name in small letters.

PHP does not require you to declare the type of a variable in advance. You simply assign a value, and the type follows the value. This is called dynamic typing. It makes quick scripts easy to write, but it also means you must be careful, because a variable can change its type during a script.

Good names matter. Choose names that describe the purpose, such as a customer name or a total amount, instead of single letters. Readable names make your own code easier to understand after a few weeks.

Which data types does PHP have?

PHP has several basic data types:

  • String: text, such as a name or an address.
  • Integer: whole numbers, positive or negative.
  • Float: numbers with a decimal part.
  • Boolean: a true or false value.
  • Array: an ordered collection of values, which can use numbers or names as keys.
  • Object: an instance of a class, which bundles data and behaviour.
  • Null: a special value that means there is no value.

Arrays deserve special attention because PHP uses them everywhere. An array can hold a simple list, such as a set of city names, or an associative collection, such as the fields of a user record with names like email and phone. Form data, database results and configuration are all commonly handled as arrays.

Everyday data types

TypeHoldsExample use
stringTextA name or an address
intWhole numbersA quantity or a roll number
floatNumbers with a decimal partA price or an average mark
booltrue or falseWhether a password matched
arrayAn ordered collection of valuesForm fields or database rows
objectAn instance of a classA student record with behaviour
nullNo valueAn optional field that was left out

What is the difference between loose and strict comparison?

Because PHP is flexible about types, it sometimes converts values automatically. A text value that looks like a number can be treated as a number in arithmetic. This convenience can cause surprises when comparing values. PHP offers two kinds of comparison: a loose one that converts types before comparing, and a strict one that checks both value and type. For most checks, especially with user input and security decisions, the strict comparison is the safer habit.

Modern PHP also supports optional type declarations for function inputs and outputs, and a strict types mode for a file. Using them helps catch mistakes early and makes your intentions clear to other developers.

What do operators do?

Operators perform actions on values. Arithmetic operators handle addition, subtraction, multiplication, division and remainder. Assignment operators store results in variables. Comparison operators test whether values are equal, different, greater or smaller. Logical operators combine conditions with the ideas of and, or and not. A special operator joins two strings together. Learn the order in which operators are evaluated, and use brackets whenever you are unsure, because clarity is better than cleverness.

How does PHP make decisions?

Programs must react to different situations. The basic decision tool is the if statement, which runs a block only when a condition is true. You can add an else part for the other case and else if parts for further cases. For choosing between many fixed possibilities, the switch statement can be tidier. Newer versions of PHP also include a match expression, which compares a value against several options and returns a result, and which uses strict comparison.

Think of a login page: if the password matches, allow entry, otherwise show a message. Think of a shop: if the cart total is above a threshold, offer free delivery. Each of these is a condition that leads to different actions.

How do loops repeat work?

Loops repeat a block of work. The while loop continues as long as a condition remains true. The do while loop runs the block at least once before checking the condition. The for loop is handy when you know how many times to repeat, because it keeps the counter, the condition and the update in one place. The foreach loop is designed for arrays and goes through each item one by one, which makes it the most common loop in everyday PHP.

Two keywords help you control loops: break stops the loop entirely, and continue skips to the next round. Always make sure that a loop has a way to end, because a loop whose condition never becomes false will run until the script is stopped by the server.

What is variable scope?

Scope decides where a variable can be seen. A variable created inside a function is visible only inside that function. A variable created outside is not automatically available inside functions. This protects your code from accidental changes, and it encourages you to pass values into functions deliberately instead of relying on hidden shared state.

Constants and null handling

A constant is a value that does not change after it is set, such as a site name or a tax label. Use constants for fixed settings so that you do not repeat the same text in many places. When working with values that might be missing, PHP provides operators that supply a default if a value is not set or is null. These are very helpful when reading optional form fields.

Practice ideas

  • Store a student name, marks in several subjects and calculate the average.
  • Use a condition to assign a grade according to the average.
  • Loop through a list of subjects and display each with its mark.
  • Create an associative array for a book and display each field.

These exercises use only the ideas in this guide and build confidence before you move on to functions and objects.

Key points to remember

Variables hold values, types describe them, and control flow decides what happens. Prefer clear names, strict comparison and loops that always end. With these basics in place, you are ready for functions and object oriented design.

Sources and further reading

Frequently asked questions

Do I need to declare variable types in PHP?

No. PHP is dynamically typed, so the type follows the value, although modern PHP lets you add optional type declarations.

What is the difference between loose and strict comparison?

Loose comparison converts types before comparing, while strict comparison checks both the value and the type.

Which loop is best for arrays?

The foreach loop is designed for arrays and goes through each item in turn.

What is an associative array?

It is an array that uses names as keys instead of numbers, such as email and phone for a user record.

All guides