PHP variables let you store, transform, and ma­nip­u­late data. For example, you can drop strings, filter and sort arrays, or perform complex cal­cu­la­tions.

What are PHP variables?

A PHP variable is a type of container that can store in­form­a­tion while a PHP program is running for later access. Variables are iden­ti­fied by a name and can contain values from different data types, like numbers, strings, booleans, or complex data struc­tures like arrays or objects. Variables are often ele­ment­ary com­pon­ents of PHP functions and PHP classes. They’re used to collect user input, store in­ter­me­di­ate results, per­son­al­ise content in web ap­plic­a­tions, or manage session data. You can also use PHP to retrieve in­form­a­tion from a MySQL database and store and process the entries in variables.

Tip

With Deploy Now from IONOS you’re in control of the de­vel­op­ment process. Choose your Git re­pos­it­ory and use automatic framework detection with GitHub Actions to deploy your ap­plic­a­tion. With Deploy Now, you’ll get custom domains, SSL security, and log-based visitor stat­ist­ics.

The syntax of PHP variables

In PHP, variables are created by prefixing the variable name with a dollar sign ($) and then assigning a value to it. Here’s the basic syntax:

$var = value;
php

The rules for PHP variables are:

  • Variable name: A variable name in PHP must start with a dollar sign ($) followed by letters, numbers, or un­der­scores. The first letter after the dollar sign must not be a number. For example: $MyVariable, $value_1, but not $1variable.
  • Case sensitive: PHP is case sensitive. $myVariable and $MyVariable are con­sidered different variables.
  • Reserved words: Avoid PHP reserved words as variable names. For example, ‘echo’, ‘if’, ‘while’, and ‘foreach’ are reserved words.
  • Special char­ac­ters: Variable names must not contain special char­ac­ters (except for the un­der­score) or spaces.
Tip

If you want to learn more about PHP variables and their functions, we recommend checking our PHP tutorial from our guide. We’ve also done com­par­is­on pieces on PHP vs Python and PHP vs JavaS­cript.

IONOS Developer API
Manage your hosting products through our powerful API
  • DNS man­age­ment
  • Easy SSL admin
  • API doc­u­ment­a­tion

What types of PHP variables are there?

PHP is a weakly typed language, meaning you don’t need to specify the PHP variable type ex­pli­citly. The data type is auto­mat­ic­ally detected, depending on the assigned value. This lets you flexibly implement variables in PHP. Here are some of the most important data types:

  • Integer (int): This type of data is used to represent integers without decimals.
  • Float (float): Floats, also known as floating point numbers, are numbers with decimal places.
  • String (string): Strings refer to strings of text.
  • Boolean (bool): Booleans represent truth values and can be either true or false.
  • Array (array): An array is an ordered list of values that can be stored under a single name. Arrays can contain values of different data types.
  • Object (object): An object is an instance of a class and can store methods and prop­er­ties (variables).
  • NULL: NULL is a special value that indicates that a variable has no value. As a result, the PHP variable is defined, i.e. it has been defined with a value of NULL.

Examples using PHP variables

PHP variables have many uses. Here’s how you can output, link, and use variables globally.

Echo PHP variable

PHP echo is a simple way to display variables and their contents on the screen.

$var = "blue";
echo $var;
php

The output will be:

blue
php

Con­cat­en­ate PHP variables

You can link PHP variables with PHP operators like the con­cat­en­a­tion operator . to strings.

$var = "blue";
echo "The sky is " . $var
php

On the screen you see:

The sky is blue
php

PHP variable global

In PHP, you can create and ini­tial­ise global variables using the $GLOBALS su­per­vari­able.

$GLOBALS['myVar'] = "This is a global variable";
php

This as­sign­ment makes the variable $myVar global and can be accessed from any part of your PHP code.

IONOS Cloud Object Storage
Cloud storage at an un­beat­able price

Cost-effective, scalable storage that in­teg­rates into your ap­plic­a­tion scenarios. Protect your data with highly secure servers and in­di­vidu­al access control.

Go to Main Menu