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­for­ma­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 el­e­men­tary com­po­nents of PHP functions and PHP classes. They’re used to collect user input, store in­ter­me­di­ate results, per­son­al­ize content in web ap­pli­ca­tions, or manage session data. You can also use PHP to retrieve in­for­ma­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 repos­i­to­ry and use automatic framework detection with GitHub Actions to deploy your ap­pli­ca­tion. With Deploy Now, you’ll get custom domains, SSL security, and log-based visitor sta­tis­tics.

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­sid­ered 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­i­son pieces on PHP vs. Python and PHP vs. JavaScript.

IONOS Developer API
Manage your hosting products through our powerful API
  • DNS man­age­ment
  • Easy SSL admin
  • API doc­u­men­ta­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­plic­it­ly. The data type is au­to­mat­i­cal­ly 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­cate­nate PHP variables

You can link PHP variables with PHP operators like the con­cate­na­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­ize 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
Secure, af­ford­able storage

Cost-effective, scalable storage that in­te­grates into your ap­pli­ca­tion scenarios. Protect your data with highly secure servers and in­di­vid­ual access control.

Go to Main Menu