5.2: Variables

Section 5.2 PHP Variables

  • Think of variables as containers that can hold very specific types of data. A good analogy is the plastic storage containers you find at home. There are containers perfect for sandwiches, soup, fresh vegetables, there is even one that is triangle shaped for pizza slices.

  • PHP VARIABLES ARE CASE SENSITIVE $a is not the same variable as $A

  • All PHP variables are prefixed with the dollar sign $.

  • PHP variable names must start with a letter or the underscore character

  • PHP variables can contain a number but a number cannot be the first character after the $ character.

  • Variable names can only contain A-z, 0-9, and _ (underline)

  • PHP supports several data types:

    • String

    • Integer

    • Float (floating point numbers - also called double)

    • Boolean

    • Array

    • Object

    • NULL

    • Resource

String

  • Strings are a character sequence; e.g., "what a great course!" use single or double quotes around the text you wish to assign to a string variable.

Figure 5.2.1

Integer

  • Non-decimal numbers that can between -2,147,483,648 and 2,147,483,647. Without going into a lot of detail, all you need to know about this range is that it has to do with the amount of memory the computer utilizes to store our variables.

  • Can be either positive or negative

  • Must contain at least one digit, i.e., cannot be null or alphabetic. No value is 0.

  • Cannot include a decimal point

  • Although Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0) we will only deal with the decimal format in this course due to time constraints. You should follow up on the hexadecimal and octal formats once you have completed this course.

Figure 5.2.2

  • That curious looking third line containing var_dump($x) shows how a php function can be used to complete certain operations from simple to very complex without needing to write the code yourself. We will learn about PHP library functions in a later section of this course. For now you only need know that the var_dump($variableName) function is a PHP library function that provides structural information about a variable. In this case var_dump($x) will return int(1955). The var_dump($x) function is especially useful in debugging

Float

  • A float (floating point number) is a number with a decimal point or a number in exponential form.

  • In the following example $x is a float. The PHP var_dump() function returns the data type and value of float(15431.99)

Figure 5.2.3

Boolean

  • Boolean variables can only contain one of two values, True and False.

  • Uses include loop control, branching logic and more. These concepts will be covered further in the course.

$isCurrentlyTouring = true;

Array

  • Array variables are useful for processing collections of data of the same type.

  • Array variables are a little more complex and we will cover these in details when we arrive at module 4.4.

$entertainers = array('John', 'Paul', 'George', 'Ringo');

Object

  • Objects are far more complex and powerful than simple variables or even an array variable. We are only going to touch on them here and Object will be covered much more deeply later in this course.

  • Objects, while more complex, are really not all that difficult if we use a familiar analogy to distinguish between a Class and an Object.

Figure 5.2.4 - Class analogy

  • Objects are created by the PHP application using a Class.

  • Let's consider rocket-ship analogy. We are in the business of building rockets and spacecraft

    • We manufacture rockets by customer order. Hey! Can I interest you in a rocket today?

    • We have many rocket models to choose from, we can see two of these in the images above.

    • For each order a rocket is built according to a blueprint for that rocket model. The rocket blueprint has properties such as engine, fuel tanks, payload, range, stabilizers, seats and so on.

    • When the rocket is finished and ready for use we have an "instance" of a rocket built from one of the builder's blueprints.

    • The rocket will have particular processes such as ignition, lock seat-belt, jettison fuel tanks and so on.

    • In PHP programming we can think of a Class as a blueprint from which many instances of an Object are created within the PHP application.

    • The really cool (and powerful) thing about Classes is that we can create our own using whatever combinations of data and process make sense when combined together.

  • We'll leave further discussion on Objects when we begin creating custom WordPress posts later in the course.

  • We need to mention variable "scope" before moving ahead, but it is too early in the course to discuss the details. For now just know that the way your program is structured and where you choose to declare those variables in your PHP program will either limit or expand their scope.

  • Revisit the String and Integer sections and their sample code above. Notice the following:

    • $Name = "David Bowie";

    • $Birthyear = 1955;

    • In most programming languages the developer must include the data type in the variable declaration.

    • For example in C# one would declare a string like this: String thisIsMyVariable = "some value you typed here".

    • You can see that our declaration of the variable type does not include data type. PHP will look at your variable value to decide how it should be treated. This means that PHP is "loosely-typed". This has pros and cons as we will see during the course.

  • Now that we understand a little about PHP data types let's try another example. Return to your index.php file in the text editor.

  • Declare the variable $thePerformer = "David Bowie";

  • Add

    • <h3>This page is all about <?php echo $thePerformer ?> </h3>

  • Skip a line and enter

    • <h3><?php echo $thePerformer ?> has been playing guitar and singing since age 12</h3>

Figure 5.2.5

  • Return to your browser and refresh the page. You should see something very close to Figure 5.1.4a

Figure 5.2.6

  • Let's make a small change to show the power of using variables in php.

  • Go back to your index.php page in the text editor and change the value of the $thePerformer variable from "David Bowie" to "Bruno Mars" and then save the index.php file.

  • Go back to your browser and click the refresh button. Your browser display should look similar to figure 5.2.2b

  • When the page reloads we can see that although we changed $thePerformer value in one single place we were able to retrieve its value multiple times.

Figure 5.2.7

Last updated