Konstantinos "xkon" Xenos

PHP Variables & Constants

← Back to PHP Basics Tutorial

You can retrieve in your code anything that you have stored in PHP variables & constants. Variables data can be changed and constants are permanent, that's a key difference.

If you've read the PHP Syntax article, you already know that it's always good to name your variables and constants with something that is related to your code. You will easily remember and identify them this way.

PHP Variables & Constants TOC


All variables must start with a $ sign and can contain only letters, numbers and underscores. The first character after the $ cannot be a number and remember they are case sensitive!

// This is correct.
$name = 'Konstantinos';

// This is correct as well.
$_last_name_ = 'Xenos';

// This is also correct!
$_middlename123 = 'debugger';

// This is not correct.
$66_route = 'get your kicks';

// This is also not correct.
$current age = 36;

You can declare new values since the data in a variable can be changed, but only the last one will be returned.

Don't get confused for the time being about the single quotes that are sometimes used to wrap the contents of a variable. You will be able to read about data types, type casting and strings in future tutorials.

// First declaration.
$myvar = 'first';

// Second declaration.
$myvar = 'second';

# This would output "second" as that's
# the last value that was declared.
echo $myvar;

Variables can have 3 scopes. They can be global, local and static. Let's see some examples in code!

 * Variables declared in the general area of your code
 * are global.
 * As an example here the $name can be parsed directly,
 * but have to be accessed from a function either by adding
 * "global" in front of it, or by the "$GLOBALS" PHP array.

$name = 'xkon';
$site = 'xkon.gr';

// This would output "xkon".
echo $name;

// This would output "xkon xkon.gr" as well.
function information() {
	global $name;

	echo $name . ' ' . $GLOBALS['site'];


// This would be wrong though as the variable is global.
function wrong() {
	echo $name;

 * Variables declared in functions have a local scope.
 * You will not be able to parse them outside
 * of the function.
 * Since the variable is local, think of it as if it
 * is gettingremoved by the memory and re-declared
 * each time you are running the function.

function mambo() {
	$number = '5';

	echo $number;

// This would output "5"

// This would output "5" again.

// But this wouldn't work.
echo $number;
 * As seen on the previous example since
 * a variable declared inside a function
 * is getting "reset" each time we run the
 * function we also have the "static" variables.
 * To declare them as static simply write "static"
 * infront of the variable.
 * This way they are not getting reset and you
 * can dynamically alter them.
 * Essentially on this example each time you run
 * mambo(), the variable will always be +3 of its
 * last value.

function mambo() {
	static $number = '2';

	echo $number . '
'; $number = $number + 3; } /** * This would output "2" as the echo * is before the + 3. */ mambo(); /** * But this would output "5" as we run * the function again and the variable * was updated. */ mambo();

Superglobal Variables

Since we covered the variables let's also make a really brief mention at the superglobal variables. I'm mentioning this as I've already shown you one of them that is $GLOBALS.


Constants are a bit easier to understand and remember. They are always of global scope even if you declare them inside a function and they are case insensitive. It's better to be consistent as I've mentioned so you should declare constants with uppercase letters.

The naming convention is similar to the variables with the difference that $ is not used. To declare a constant you need to use a PHP statement instead called define().

// Declaring a constant.
define( 'FIRST_CONSTANT', 'first' );

// This would output "first".

 * Declaring a constant is global
 * so it can be used anywhere without
 * the need of "global" or "$GLOBALS".
define( 'SECOND_CONSTANT', 'second' );

function second() {

// This would output "second".

 * Even though the constant is defined inside
 * the function. When this function is run
 * the constant becomes global from that point
 * on.
function third() {
	define( 'THIRD_CONSTANT', 3 );


// This would output "3".

 * Since constants are permanent and can't
 * be changed. This would produce a notice
 * as it is wrong.
 * The output would always return "yellow"
 * as that's the first declaration that
 * the constant had in the code.
define( 'COLOR', 'yellow' );

define( 'COLOR', 'blue' );

// This would output "yellow".
echo COLOR;

In WordPress you will find some constants in your wp-config.php such as the database credentials since their values should be the same always when used in future code references.

Extra Tips on PHP Variables & Constants

You should not use the $this variable on "general" code as it is a pseudo-variable. You can only use it to refer to an object of a method. I'll explain more about $this in the future in the PHP Advanced tutorial as it will cover Object-Oriented Programming (OOP) in PHP.