PHP104

From mi-linux
Jump to navigationJump to search

Main Page >> Web Application Development >> Workbook >> PHP Variables

PHP Variables & Constants - Basics

A variable is a place in the computer’s memory where you can store some data. The stored value can change, and is often as the result of some calculation or processing having taken place.

A variable in PHP is identified by the first character being a dollar sign, ‘$’. Variables do not have to be declared before using them, and they have no value until assigned one. The process of assigning a value to the variable also usually defines what type of variable it is, e.g. text or numeric.

This process of implicitly defining a variable means that if the value is changed, it can be implicitly re-defined. The following line of code shows a variable called $num, and assigns it a value of 17. This implicitly defines it as an integer (whole number).

<?php
  $num = 17;
?>

To re-define this variable, so it contains text (a string of characters), we use the following code:

<?php
  $num = "University of Wolverhampton";
?>

Note, that this would not be a meaningful name for this variable. Perhaps a more meaningful name might be $university. The process of re-defining variables can both be flexible and dangerous, if not used with some care.

Look at the following example which uses both a string and an integer variable.

<?php
  $students = 212;
  $module = "5cs023";
  echo "The class $module has $students enrolled on it";
?>

This produces:

The class 5cs023has 212 enrolled on it

NOTE: - a common error is to use an undefined variable in an expression.

Types of Variable

There are four type of scalar (can contain a single value at any given time) variable supported by PHP. These are:

  • Boolean
  • Float
  • Integer
  • String

PHP also supports compound types (array or object). These can contain a number of scalar values, such as the days of the week, the scores of students in an exam etc.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. The underscore is used instead of a space (space in variables names is not permitted in PHP).

For example, if you chose the variable name '$my age' it would not be permitted. The variable '$my_age' would be.

Boolean

These are very simple variables, in that they can only hold the values true or false. They are often used to determine what action should be taken - according to their value.

Integer

A whole number, sometimes known as a counting number. Note, these can be positive or negative, e.g. -37, 5, 4732, -162, 0, 56

<?php
  // This is an integer
  $distance = 42;
?>

Float

A number which has a fractional part. Note this may be a decimal, or represented in exponential notation (scientific format).

<?php
  // This is a float
  $distance = 42.0;
  // This is a float equal to 1120
  $num = 1.12e3;
  //This is a float equal to 0.02
  $num = 2e-2;
?>

String

We have already seen numerous examples of string variables on previous pages. A string can contain a single character, or numerous characters.

<?php
  $initial = "A";
  $surname = "Bergeret";
  echo $initial." ".$surname;
?>

Constants

If variables are values which may 'vary' during the life of a script, then a constant is a value which remains 'constant' (does not vary) during the life of that script. Therefore, it is assigned a value which is used throughout the script. Examples would be the number of hours in a day, the number of students on a course, the rate of VAT etc.

<?php
  define ("PI", 3.142);
  // output the value of twice PI
  echo (2 * PI);
?>

This produces:

6.284

NOTE: - Conventions are for constants to be UPPERCASE

Note the syntax of the definition of PI, and the fact that unlike variables, constants do not have a '$' as their first character.

Constants are much underused by new programmers. If used to define the number of lines used on a web page, it can be altered once, without having to make numerous changes throughout the code.

Ready to move on?

PHP105 - Expressions, Operators, and Variable Assignments