Home » Programming » PHP » How to Declare Variables in PHP (Constants, Static, Global)

How to Declare Variables in PHP (Constants, Static, Global)

This article shows you how to declare variables in PHP, including constant, static, and global variables. Code examples are provided.

What is a Variable?

In computer programming, a variable stores a value, giving it a name that can be used to access it. Once a variable has been created and a value has been assigned to it, the value can be retrieved using the name given to the variable.

The value of a variable can be updated (unless the variable has been declared as a constant), and the variable can be deleted, destroying the value it stores.

Variables can store any kind of value, including numbers, strings, objects, boolean values, and arrays. Some programming languages enforce specific types for variables.

Using variables is one of the core features of object oriented programming – without variables, you would not be able to store values for use elsewhere in your code (unless you were working in a language that let you write directly to memory, which PHP does not).

Declaring Variables in PHP

In PHP, a variable is declared when it is first used.

Declaring a variable in PHP is done as follows:

$myVariable = "Hello LinuxScrew";

Above, a variable named $myVariable is declared using the = (equals) operator, and assigned the string value Hello LinuxScrew.

There are several rules when naming variables in PHP:

  • Variables must all start wit ha $ (dollar sign symbol)
  • Variable names can only contain alphanumeric characters (A-Z, 0-9), and underscores
  • Variables cannot be named $this – this is a special variable that has specific uses

Variables can also be declared without a value:

$myVariable;

Declaring Constants

Constants have a value that cannot be changed after they have been been declared. They aren’t technically variables (as they can’t be changed), but are often referred to as such.

In PHP, constants are created using the const operator:

const GREETING = "Hi there!";

echo GREETING;

Constant naming conventions are the same as those for regular variables, except that they aren’t preceded by a $ (dollar symbol). While it is not mandatory, many programmers choose to name their constants entirely in upper case, so they can be easily identified.

The define() function can also be used to define constants:

define('GREETING', 'hi there!');

echo GREETING;

However, the define() function does not work when creating object classes. As the const operator works both inside and outside of objects, define() can pretty much be ignored.

Declaring Global and Static Variables

Global and static variables exist within different scopes. We dive into this in our article on PHP scopes here.

Variable Variables

Variable variables let you use a variable as another variable’s name – it’s a bit confusing, so we cover it here.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment