Home

Introduction to Variables

Fundamentals of Variables

Introduction

A variable is an area of the computer memory (RAM) used to store a value. To perform this operation, the browser needs two pieces of information: a name and the amount of memory needed to store (and hold) the value.

Declaring a Variable

Before using a variable, you must let the browser know. Letting the browser know about the variable is referred to as declaring a variable. To do this, add a line of code that has the name of the variable.

The Name of a Variable

In PHP:

PHP uses some reserved keywords for its own use. As a result, the name of a variable should (must) not be one of the following:

__halt_compilerabstractandarrayas
boolbreakcallablecasecatch
classcloneconstcontinuedeclare
defaultdiedoechoelse
elseifemptyenddeclareendforendforeach
endifendswitchendwhileevalexit
extendsfalsefinalfinallyfloat
forforeachfunctionglobalgoto
ifimplementsincludeinclude_onceinstanceof
insteadofintinterfaceissetlist
mixednamespacenewnullnumeric
objectorprintprivateprotected
publicrequirerequire_onceresourcereturn
staticstringswitchthrowtrait
truetryunsetusevar
while xor yield

Here is an example of declaring a variable:

<?php $element ?>

In the same same way, you can declare as many variables as you want. Each variable can (and should) be on its own line and ending with a semicolon. Here are examples:

<?php
$element;
$SYMBOL;
$atomic_number;
$atomicWeight
?>

Initializing a Variable

Initializing a variable consists of giving it an initial or default value. This is done when declaring the variable. To do this, on the right side of the name of the variable, add the assignment operator, which is =, followed by the desired value:

Declaring and Initializing Variables in a Chain

Initializing variables in a chain consists of giving the same value to many variables. To do this, assign one variable to another but the last factor is the actual value. Here is an example:

<?php
$months = $magnesium = $noon = 12
?>

Inferring the Type of a Variable

When declaring a variable, you must indicate the type of value it will use. To do this, you must assign a value to the variable. The formula to use is:

variable-name = value

There are many types of values used in a webpage, and values are represented in different ways. Based on the type of value you assign to the variable, the PHP interpreter will know, or infer, how much memory space is necessary to store the value.

Setting or Changing the Value of a Variable

After declaring or using a variable, you can assign a new value to it, which is equivalent to changing its value. Besides assigning a known value to a variable, the value of a variable can come from another variable. In this case, you can assign the name of a variable to another variable. Here is an example:

<?php
    $number = 28
    $nbr = $number
?>

The value of a variable can also come from an operation that involves two or more values. In fact, one of the operands can be a variable. Here are examples:

<?php
    $side = 28.46;
    $perimeter = $side * 4.00;
    $area = $side * $side;
?>

Using the Value of a Variable

Introduction

After declaring and initializing a variable, you can use it. The minimum you can do is to display it in a webpage. To do this, you can type the name of the variable, with its $ symbol, on the right side of echo. Here is an example:

<?php
    $number = 28;
    
    echo "Number: ";
    echo $number
?>

This would produce:

The Value of an Item

You can also include the variable in a string in double-quotes. Here is an example:

<?php
    $number = 28;
    
    echo "Number: $number";
?>

The variable can also be used in an HTML element where you would include it in the appropriate PHP delimiters. Here are examples:

<!DOCTYPE html>
<html>
<head>
<title>Geometry: Square</title>
</head>
<body>

<?php
    $side = 28.46;
    $perimeter = $side * 4.00;
    $area = $side * $side;
?>

<form name="frmCalculation">
  <table border="4">
    <tr>
      <td>Side:</td>
      <td>
	<input name="txtSide" value="<?php echo $side ?>" /></td>
    </tr>
    <tr>
      <td>Perimeter:</td>
      <td><input name="txtPerimeter" value="<?= $perimeter ?>" /></td>
    </tr>
    <tr>
      <td>Area:</td>
      <td><input name="txtArea" value="<?= $area ?>" /></td>
    </tr>
  </table>
</form>

</body>

</html>

This would produce:

Introduction to Using the Value of a Variable

A Variable From the Value of a Variable

Consider the following code:

<?php
    $compound = "carbon";
    
    echo "Compound: $compound<br>";
?>

This would produce:

A Variable From the Value of a Variable

You can declare a variable whose name is built from the value of another variable. This is referred to as a variable variable. To do this, when declaring the second variable, use the name of the first variable but precede it with $. Here is an example:

<?php
    $compound = "carbon";
    $$compound = "Carbon is the main component of most minerals such as diamonds and graphite.";
?>

From, there is a new variable made of the $ symbol and the value of the first variable. You can use that new variable like any other. Here is an example:

<?php
    $compound = "carbon";
    $$compound = "Carbon is the main component of most minerals such as diamonds and graphite.";

    echo "Compound: $compound<br>";
    echo "Description: $carbon";
?>

This would produce:

A Variable From the Value of a Variable

You can also use the second variable as ${$first-variable}. Here is an example:

<?php
    $compound = "carbon";
    $$compound = "Carbon is the main component of most minerals such as diamonds and graphite.";

    echo "Compound: $compound<br>";
    echo "Description: ${$compound}";
?>

Characteristics of Variables and their Values

Changing the Value of a Variable

After declaring and initializing a variable, you can use it as if it were a value. At any time you can assign a new value to the variable. When you do this, the value of the variable changes to the new one. Here is an example:

<?php
    $PI = 0.00;
    $radius = 24.88;

    $PI = 3.14;

    $area = $radius * $radius * $PI;

echo "Radius: $radius <br>";
echo "Area: $area"
?>

This would produce:

Changing the Value of a Variable

Constants

A constant is a value that doesn't change. Normally, any value you can think of is a constant. For example, 1 never changes, "James" never changes, 19.95 never changes, etc. As a programmer, you can make a variable become a constant. The value of such a variable cannot change in your program.

There are two ways you can create a constant. You can use the const keyword. The formula to follow is:

const VARIABLE-NAME = value

You start with the const keyword that is followed by a name, the assignment operator, and the desired value. The other formula to create a contant variable is:

define("VARIABLE-NAME", "value")

You start with the define macro that includes parentheses. Inside the parentheses, create a name for the constant. This time, the name is in double-quotes. In both cases, the name must start with a letter or an underscore, followed by letters, digits, and/or underscores. By tradition, the name of a constant is in uppercase. Then, specify the value of the contant. Here is an example:

<?php
    const PI = 3.14156
?> 

After creating the constant, you can use it. Here is an example:

<?php
    const PI = 3.14156;
    $radius = 24.88;

    $area = $radius * $radius * PI;

echo "Radius: $radius <br>";
echo "Area: $area"
?>

This would produce:

Constants

If you created your constant with define(), don't use the double-quotes when accessing the constant. Here is an example:

<?php
    define("PI", 3.14156);
    $radius = 24.88;

    $area = $radius * $radius * PI;

echo "Radius: $radius <br>";
echo "Area: $area"
?>

You should avoid starting the name of a constant with 2 underscores and ending it with 2 underscores, as in __CONSTANT__, because it may create conflicts with other names.

The NULL Constant

PHP provides a special constant named NULL. It is used for a variable that currently doesn't have a recognizable value. For example, if you declare a variable but don't assign a value to it, computer memory is reserved for that variable but there is no real value in that memory. Such a variable is said to be null or to have/hold the NULL constant.

When declaring a variable, if you don't yet have a value for it, or you want to indicate that it doesn't yet have a value, you can assign NULL to it. Here is an example:

<?php $element = NULL ?>

This declaration is equivalent to indicating that you want to use a variable but you haven't (yet) decided what to store in it. Later on, or at any time, you can assign the desired value to the variable.

Type Juggling

Type juggling is the ability to store different types of values in the same variable at different times. Normally, when you declare a variable, you have two options, you can omit assigning a value to it or assign NULL to it, or you can assign a known value to it. Here is an example:

<?php
$element = "Helium";

echo "<h3>Chemical Element</h3>";
echo $element;
?>

This would produce:

Type Juggling

Thanks to type juggling, at any time, you can store another type of value in the same variable and from there under, the variable would hold the new value. Here are examples:

<?php
$element = NULL;

echo "<h3>Chemical Element</h3>";

$element = 2;

echo "<ul>";
echo "<li>Atomic Number: ";
echo $element;

$element = "Helium";

echo "<li>Element: ";
echo $element;

$element = 4.002602;

echo "<li>Atomic Weight: ";
echo $element;
?>

This would produce:

Type Juggling

Assigning a Variable by Value

As mentioned already, you can assign one variable to another. Here is an example:

<?php
    $water = "H<sub>2</sub>O";
    $molecule = $water;

    echo "Substance: $water<br>";
    echo "Molecule: $molecule"
?>

When you have done that, both variables hold the same value. The above code would produce:

Assigning a Variable by Value

Assigning a Variable by Reference

After assigning one variable to another, each variable holds its own value. You can change the value of one of the variables and the change would not affect the other variable. Consider the following example:

<?php
    $water = "H<sub>2</sub>O";
    $molecule = $water;

    echo "Substance: $water<br>";
    echo "Molecule: $molecule<br>";

    $water = "Water is the most abundant chemical compound on earth";

    echo "Description: $water<br>";
    echo "Molecule: $molecule";
?>

This would produce:

Assigning a Variable by Value

This is referred to as assigning a variable by value: The value of the variable is copied into the other variable. An option is to only create a reference to the other variable. In this case, when one variable A is assigned to another variable B, the address of variable A is given to variable B so that variable B doesn't actually have its own value: it holds the address of the other variable. In this case, when the second value is accessed, it refers to the address of the first variable for its actual value.

To assign a variable by reference, precede its name with &. Here is an example:

<?php
    $water = "H<sub>2</sub>O";
    $molecule = &$water;

    echo "Substance: $water<br>";
    echo "Molecule: $molecule<br>";

    $water = "Water is the most abundant chemical compound on earth.";

    echo "Description: $water<br>";
    echo "Emphasis: $molecule";
?>

This would produce:

Assigning a Variable by Reference


Previous Copyright © 2015-2022, FunctionX Next