Variables, Constants and Simple Arrays

Dealing with data always involves the use of constants and variables. This is the primary topic of this tutorial. The concept is really easy to understand and by the end of this tutorial, you should gain the following knowledge:

  • Differentiate a variable, a constant and a literal.
  • Learn the different data types supported by PHP.
  • Learn how to create and use variables and constants in PHP Script
  • Create and use simple PHP Arrays
  • Know the difference between PHP String in Single Quotes and in Double Quotes

In programming parlance, a variable is simply a named memory location used for storing value that may change over the course of time. A constant is a named memory placeholder whose value cannot be changed programmatically once it is assigned a value. In PHP, the actual value (also called a literal) of a variable or a constant is determined by its data type, which may be one of the following:

  • Boolean -- value can be either TRUE or FALSE.
  • String -- a series of characters
  • Numeric -- may be an interger or a floating point number
  • Array -- a named collection of scalar
  • Object -- an instance of a class (we will have more of this in more advanced topics)
  • resource -- a special value associated with an external resource like a database connection, opened file, etc.
  • NULL -- represents a no-value data; that is a variable that has no value has the value of the literal NULL.

The first three data types are considered scalar data types. You create an array by collecting similar scalar types. And object is an instance of an Abstract Data Type called Class created by combining scalar types.
The idea of constants and variables is best learned using an example, so consider the code snippet shown below.

Listing 3.1

PHP:
  1. <?php
  2. $name = 'paul';
  3. $current_age = 12;
  4. define('COUNTRY', 'Philippines');
  5. $message = 'I am '. $name . ' aged ' . $current_age . ' and living in the ' . COUNTRY;
  6. print ($message);
  7. ?>

The first and the second lines tell us to create two variables ( $name and $current_age ) and assign them with their respective values. In PHP, a variable is always prefixed with the dollar sign followed by the actual name of the variable. The name can formed by combining alphanumeri characters and/or underscore, but the first character must not be numeric. PHP variables are also implicitly declared at run-time without the need of prior declaration as you would in other programming languages like Java or C. PHP is a loosely typed language and a variable can be assigned a value of any datatype programmatically.These two statements are also called assignment statements in that they put values to the variables.

The third line shows us how a constant is created using the define() function. The first parameter becomes the name of the constant and the second parameter takes on the literal value of the constant.

The fourth line demonstrates how the values of variables and constants are used. The expression on the right side of the assignment statement simply concatenates the values of the variables $name, $current_age, and the constant COUNTRY with some string literals. In PHP, Strings are concatenated using the dot ( . ) operator.

We'll have more about PHP Strings by the end of this section.

The fifth line should be obvious. It simply displays on the browser the value of the concatenated string.

Simple PHP Arrays

An array is a collection of similar items (also called elements) under one name. PHP Arrays are of two kinds: Numerically Indexed Array and Associative Array. Whereas a Numercially Index array references an element by using a number, an Associative Array uses a much more descriptive text. Listing 3.2 demonstrates how to create these two kinds of arrays how to use them.
Listing 3.2

PHP:
  1. <?php
  2. // Create a numerically indexed array of three elements
  3. $friends[] = 'Jose';
  4. $friends[] = 'Anna';
  5. $friends[] = 'Erin';
  6. // Create an associative array of four elements
  7. $countries['ph'] = 'Philippines';
  8. $countries['sg'] = 'Singapore';
  9. $countries['ch'] = 'China';
  10. $countries['jp'] = 'Japan';
  11.  
  12. print ($friends[0] . ' is from ' . $countries['ph'] );
  13. print(' ');
  14. print ($friends[2] . ' is from ' . $countries['jp'] );
  15. ?>

When do use a numerically indexed array and when do you use an associative array? Actually it depends on your needs. Experience and common sense will tell you when one is more appropriate to use over the other.

Basic PHP Strings

There are two common ways of creating PHP Strings:

1. Using single quotes -- everything that is enclosed in single quotes is treated as is.
2. Using double quotes -- literal characters within the double quotes are treated as is; the variables will be parsed and substituted with their literal values. This one comes handy and makes for a easy-to-read code if you want to combine strings with variables.

Take a look at this example.

Listing 3.3

PHP:
  1. <?php
  2. $student = 'Allan';
  3. $message = "$student is the fastest C programmer in the campus.";
  4. echo $message;
  5. ?>

Easy right? For more information, check the PHP Manual for PHP Strings.

References:

This series of articles is intended as a
Self-Paced Introductory tutorial on PHP
.

Leave a Reply

Get notified of occasional articles posted on this blog.

Enter your email address:

Delivered by FeedBurner