Learn to Code via Tutorials on Repl.it!

← Back to all posts
PHP tutorial No. 2: Variables
[deleted]

PHP tutorial No. 2: Variables

Variables section in the PHP language reference

Types section in the PHP language reference

Variables are named locations in a program's memory that can store values. Values have a data type, though these do not need to be stated when variables are declared (like in some languages like C++ e.g. int age; double height).

The allowable types are:

  • String
  • Integer
  • Float - sometimes called doubles
  • Boolean - true (TRUE) or false (FALSE)
  • Array
  • Object
  • NULL
  • Resource

Rules for variable naming

  • Names should describe the value they are storing
  • Names start with a $ symbol followed by the name of the variable.
  • A variable name must start with a letter or the underscore character.
  • A name cannot start with a number.
  • It can only contain alpha-numeric characters and underscores e.g. a-z,A-Z, 0-9, and _
  • Names are case-sensitive e.g. $Banana and $banana are two different variables.

The more common data types

PHP will work out the type of a variable when a value is assigned to it. For example the following variables store an integer, float, string and Boolean respectively.

Numeric variables can be added, subtracted etc. The operators are the same as those used in C, C++, JavaScript and other languages based on C.

The following code converts a temperature from celcius to fahrenheit.

Note that statements have to have a semi-colon at the end of each line.

Below are some other examples of using variables.

Variables can be embedded in a string that is double quoted. The value of the variable is substituted in the string when it is processed by the PHP parser e.g.

If single quotes are used to 'quote' a string that contains variables, the variable names will be displayed rather than the values they hold.

The code below shows how to use a variable storing a Boolean. It also shows how to use the if/else construct. Note how it is the same as that used in the C++ and JavaScript languages:

Arrays

Single and multi-dimensional arrays

Arrays are powerful data structures that usually store multiple values that are related in some way. For example the following $animals array stores the names of animals.

Values can be accessed by using the index values of array elements. These start at 0 for the first element, so to display the second element ("pig") the following code will be used. Note the use of the concatenation character to build-up the string.

Replace pig with giraffe

Add a new animal. Note the use of the empty square brackets after the array name

Arrays can be muti-dimensional. This means arrays within other arrays. Below is an example. The arrays inside the main array hold information on pets - their: name, age (in years), and favourite food.

Note how the concatenation operator (the dot) has to be used to "stitch" the array values into the string. Unlike variables that hold the number, string and boolean data types, interpolating array values in a double quoted string will not cause the corresponding values to be substituted. So the code below will NOT work.

Associative arrays

Associative arrays are often a more useful type of an array. Values called keys are pair a value. To get or set a value the key, which is often a string, is used in the square brackets after the array name, rather than an index number. The following example shows an example and how to get and set a value. Note the person's name is used as the key and their age is the associated value.

Arrays can be more complex. The following array uses a mix of one dimensional and associative arrays to store information on people.

The values in the array can be displayed as a series of lists using a for loop, like so:

Here is a link to a program that shows the above code in action: Mixed arrays example

Previous tutorial: Introduction to PHP

Next tutorial: Decisions

Voters
arpitpandya1112
andrew_furth
pepijnvereecken
tajnimabdul
Lirhiel
rediar
EnviroS
heyitsmarcus