php.w3clubs.com

PHP tutorials and articles

 

Variables

The PHP variables start with a dollar sign and then a name. A variable name can contain letters, numbers or an underscore (but cannot start with a number). So all these are valid variable names:

  • $a
  • $another_b
  • $RandOMCaPitalization
  • $a1b2see3
  • $_something_really_long123

These are invalid:

  • $2a
  • $5
  • $%another
  • $a@b

Variable names are case sensitive, so $a and $A are two different variables.

What are they good for?

Variables are used to contain values. Values are for example 1, 2, 3.14 and "ninja". The values contained in variables can, well, vary. Enough theory, let's see some examples.

// the variable called $break will now
// contain the string "<br />"
$break = '<br />'; 

// the variable called $temp will contain the number 1
$temp = 1; 

// let's print the value contained in the variable $temp
echo $temp; // echoes 1

// let's print the value contained in the variable $break
// which is the HTML tag for line break
echo $break; 

// assign a new value to the variable $temp
$temp = "Ninja"; 

// the value contained in $temp
// will now be copied as value contained in $break
$break = $temp; 

//  printing both $break and $temp with a space between them
echo $break, ' ', $temp; // the result is "Ninja Ninja"

Types of data (values)

What types of values can a variable hold? PHP supports eight types of data (and some pseudo-types). For now let's just look at 5 of them, the rest will follow:

  • integers, like 1, 2, 1001, 0, -1, -356
  • strings like "a", "abc123", "today is Tuesday"
  • booleans - they can only have two values - TRUE and FALSE, which are case insensitive
  • doubles (also knows as "floats", "floating-point numbers" or "real" numbers), such as 9.99, -1.2345 or 0.01
  • null - a special type of data with only one possible value - NULL. It means that a variable exists but doesn't have a value.

Defining a variable

In PHP you define a variable by simply giving it a value.

// up until here $a doesn't exist
$a = 1;
// from now on, $a is defined and initialized

It's possible to use variables without initializing them, but it's not a recommended practice and will throw a notice if you have error reporting set to E_ALL. Consider this script:

<?php
error_reporting(E_ALL);
echo $a; // this will return a Notice
$a = 1; // now we initialized $a
echo '<br />', $a; // printing, no errors here
?>

Declaring the type

Let's say you want to define a string variable, meaning a variable that will hold strings. How do you declare the type of the variable? Easy, you don't. PHP will guess and assign the correct type, based on the value you give.

$a = 1; // $a is of integer type
$a = '1'; // string, because of the quotes
$a = true; // boolean
$a = "true"; // string
$a = 1.1; // float

Type conversion

Let's say you have a string variable and you want to convert it to a number. Most of the time you don't need to worry about converting types, because PHP will do the conversion when needed, based on the context in which you use the variable.

$a = "1"; // string
$b = 2; // integer
$c = $a + $b; // $c is an integer with value 3

In the example above, the value in $a was converted to an integer before being added to $b, so the result is an integer.

$a = "1"; // string
$b = 2; // integer
$c = $a . $b; // $c is a string "12"

Here the string concatenation operator was used, so both values were treated as strings.

Explicit conversion

If you want to be sure a variable is of specific type, you can put the type you desire in front of the variable and in brackets:

$a = 2; // $a is an integer
$b = (string)$a; // $b is the string "2"
$c = (boolean)$a; // $c is TRUE, anything
                        // that is not 0, "", false or null is TRUE
$d = (integer)$b; // $d is the integer 2
$e = (double)$a; // $e is a double

The type-casting operators have synonyms that can also be used:

  • int, integer
  • bool, boolean
  • real, double, float

If you want to know the type of a variable, use gettype(), like:

$a = 1;
echo gettype($a); // prints "integer"

Variable variables

This is rarely needed, but in some cases can be handy. You can have a variable that contains the name of another variable.

$a = 1;
$b = "a";
echo $$b; // prints 1

« to the PHP tutorial table of contents

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress, theme by ♣w3clubs.com, based on Kubrik and Sancta Simplicitas