Real World PHP

About   |   EN   |   ES
« Previous 004 Comments
006 Data Types Next »

005 Variables

While programming, it is useful to be able to store information in a container that you can use, manipulate, pass around, and store. To do that, we use a variable. Like the name suggests, a variable is a container the content of which can vary or change.

Variable names in PHP must begin with the US dollar sign: $. Following that is the variable name. Some examples: $a, $example1, $thisIsAVariable, and $so_is_this. The name of the variable must be letters, numbers, and/or underscores. (But it cannot start with a number!) No spaces or special characters are allowed, and variable names are case sensitive. So $name is NOT the same as $Name. Some examples of illegal variable names: $3invalid, $a-b-c-no-go, $don’tCome@MeBro.

​​The Assignment operator

Once you have your variable name, you fill it using the assignment operator, which is the equals sign: =. To use the assignment operator (=), whatever is on the right of the equals sign gets assigned to whatever is on the left of the equals sign.

Examples:

<?php
// A variable can contain a number:
$a = 45;
$b = 3.1415;
$c = -1600;

// Or letters:
$d = "abc";
$e = 'This is a string of letters.';

// Or booleans:
$f = true;
$g = false;

// Or null:
$h = null;

// Or arrays:
$i = array();
$j = [];

// Or objects (more on this in later lessons):
$k = new stdClass();

// Or even other variables:
$l = $a;
$m = "The definition of PI is: $b";
?>

Don’t worry if some of those examples don’t make sense yet. Everything will be covered in later chapters. The point is that variables are versatile and are constructed using the assignment operator.

Variable variables

On occasion it is helpful to use a variable variable. This concept may stretch your mind a bit. It is a variable name that can be set dynamically. It uses the value of a variable as the name of a variable.

<?php
// First, set a variable to a string:
$device = 'cell';

// Second, set the value using two $
$$device = 'phone';

// Then we can output
echo $device; // output: cell
echo $$device; // output: phone
// Note that we can now reference a the variable directly
echo $cell; // output: phone
// Use braces to keep things clear
echo ${$device}; // output: phone
?>

This feature can be quite confusing. So it is not recommended to use often. However, on occasion, it can be helpful. Consider a situation where you have a related series of variables, methods, or classes only one of which needs to be used and which will be used is determined by some input. It is a trivial matter to use a conditional to determine which to use. But if the list of related series grows, the number of conditionals will grow with it. A variable variable will elegantly keep this simple, no matter how many items are added to the series. Examples of this could be a router or the strategy pattern.

For now it is enough to know that this exists. PHP developers don't typically use it often, but it is good to know that it is there when you need it.

Constants and Predefined Constants and Magic Constants

A constant is like a variable in that you can assign it a value, and it is case-sensitive. But unlike a variable, once a constant has been assigned its initial value, it cannot change. By convention, constants are typically all uppercase so that they are easy to recognize. Also unlike a variable, constants do not begin with a $. A constant must start with a letter or an underscore and can contain only letters, numbers, and underscores. Constants have global scope, which means that they can be accessed from anywhere within a script.

<?php
// Constants are declared by using the define() method
define("PI", 3.1415);
define("HTTP_OK_STATUS", "200 OK");

// You can begin and end a constant with two underscores, but it is a bad practice
define("__HELLO__", "Hello"); // Do not do this.
?>

Predefined Constants

PHP provides some constants for you to use. There are a lot of them. You can see a complete list in the PHP Documentation for Predefined Constants. Here are a few that are especially noteworthy:

Magic Constants

PHP has a group of nine constants they call magic in that they change depending on where they are used. These constants begin and end with two underscores. This is why it is not a good idea to use that convention in your code as one day PHP may create a magic constant with the same name as yours, creating a conflict. You can see a full list in the PHP Documentation Magic constants page. Here are three:

Predefined Variables and Superglobals

PHP has a lot of variables predefined for your use in any script. There is a list in the PHP Documentation Predefined Variables page. These variables can vary depending on the server and system in which PHP is being run.

Nine of the predefined variables are superglobals, which means they are built-in variables that are always available.


Resources


Challenges

Variable Swap

Create three variables: $a, $b, and $temp.

Using the assignment operator, give $a the value of "Apple" and give $b the value of "Banana." Finally, give $temp the value of "" (an empty string).

Next, using the $temp variable, swap the values of $a and $b so that $b will contain "Apple" and $a will contain "Banana."

Configuration Information

Use the following script to see a lot of information about PHP's configuration in the browser.

<?php
phpinfo();
?>

You can run this from the command line by first running

php -a

and then

phpinfo();


« Previous 004 Comments
006 Data Types Next »