To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the user of functional cookies. For more information read this page.

Variables and constants in PHP

An introduction to variables

PHP is like any other programming language in that it has variables. Variables are used to store information on a temporary basis whilst a program is running. This tutorial will show how variables work in PHP.

Variables

Variables are ways of storing information temporarily on memory, paritcularly the server based memory.

Variables always start with the $ sign and do not need to be declared with a type. This is because PHP is a loosely typed language.

PHP
<?php
$test = 51;
$test = $test + 3;
//This should	output The answer is 54
echo 'The answer is ';
echo $test;
?>
		

With PHP there is no need to instantiate variables before using them.

Session variables

PHP has something called a session. The concept of a session is when the client is still on that website, the session is still open. Session variables can then be stored so that preferrences or baskets/orders are stored for each user. How this works is that a server keeps a small file, uniquely identifying the machines or users that are using the page and stores information in it.

PHP
<?php
$test = 51;
session_start();
$_SESSION['myTest'] = $test;
?>
		

The example above declares a session variable with the name 'myTest' with the value of $test (51).

We can then retrieve the value of myTest by calling a session variable again as such:

PHP
<?php
session_start();
$test = $_SESSION['myTest'];
?>
		

Note, it is very important to start a session before calling a session variable.

Cookies

Cookies are another type of variable that can be stored in PHP and JSP and other server side languages. In PHP a cookie is stored locally. Cookies are chunks of information stored in bytes. This website uses cookies for Google Analytics amongst other things.

Regarding cookies, it is EU law that a website specifies that it uses cookies as of early 2011 when needing cookies. There is a part of the policy that states that cookies should be used for valid reasons. You can find out more on this website.

In PHP cookies are stored and retrieved as such:

PHP
<?php
setcookie('myTest', 'value');
$test = $_COOKIE['myTest'];
?>
		

Constants

PHP also allows the use of constants.

Constant vs variables

There is a major difference between a constant and a variable. The most crucial part is that a constant cannot be changed, it's value is constantly the same.

A variable can vary. This means the value can change at any time and does not have to defined at the start.

Below is how it is defined in PHP:

PHP
<?php
define ('MYTEST', 'value');
$test = MYTEST;
?>
		

The example above is case-insensitive. This means we can also refer to MYTEST using the name mytest. To make it case sensitive a third parameter is passed with the value true.

PHP
<?php
define ('MYTEST', 'value', true);
$test = Mytest;
?>
		

The true at the end of the define statement tells us that the constant is case-sensitive.