Easy PHP II: Variables

In the previous introductory PHP tutorial we talked about what PHP looks like and in what kind of environment it runs. To sum up, we said that we write PHP code between tags that look like this: “<?php … ?>” and we end every command line must and in semicolon (;). We use “echo” or “print” in order for PHP to show things to us. We also said that we need to have Apache server, PHP and MySQL installed in our computer or server so that PHP can run and we concluded that using a ready-made package like XAMPP, WAMP, or MAMP to achieve that would be optimal. Today we are going to learn about PHP variables, their kinds and use.

What are Variables

Here is what a variable looks like:

// Our first variable
$cool_variable = 'This s a really cool variable';

Variables are used to hold some value or data for a certain time so that you can use it when you need it later in your code. Variables start with a dollar sign ($). A variable name must start with a letter or an underscore “_” . A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) and should not contain spaces. When a variable name is made of more than one word, it can be separated with an underscore ($cool_variable), or written in camel case ($coolVariable). Variables can hold data of different types.

PHP Variables Data Types

There are four PHP variables data types. Let’s deduce them by example:

$first_type = 'This a cool text variable'; // Just text.
$second_type = 123; // Just a number – note the lack of quotes.
$third_type = 1.23; // Decimal number – note the lack of quotes.
$fourth_type = ''; // Just empty - cool, isn't it.

As you can see in above examples, declaring any type of variable is very easy. Every type has a name and they are respectively called: 1. String, 2. Integer, 3. Float, 4. Boolean. We don’t need to tell PHP which data type is variable holding; PHP evaluates the data when you assign it to the variable and then stores it as the appropriate type. Let’s talk about them in detail.

  • String: A series of characters, otherwise called text. There is no practical limit on the length of a string or text. A string must be written between double or single quotes, otherwise PHP will produce an error.
  • Integer: A whole number (no fractions), such as –43, 0, 1, 27, or 5438. Integer numbers should not be wrapped with quotes, otherwise they will be considered as a string, not an integer i.e. text and not number.
  • Float: A number (usually not a whole number) that includes decimal places, such as 5.24 or 123.456789. This is often called a real number or a float. A float should not be wrapped with quotes too.
  • Boolean: A TRUE or FALSE value. Boolean data types represent two possible states — TRUE or FALSE. A FALSE boolean is declared in several ways – at the example above we saw only one of them. Here is the whole list:
    • The string FALSE (can be upper- or lower-case)
    • The integer 0
    • The float 0.0
    • An empty string (as in the example above)
    • The one-character string ‘0’
    • The constant NULL

    On the other hand, any other values in a Boolean variable are considered TRUE. If you echo a Boolean variable, the value FALSE displays as a blank string; the value TRUE echoes as a 1.

Assigning data to variables

The equals sign we used in the above statements is called the assignment operator, as it is used to assign values to variables. There are a ton other PHP operators that we need to know in order to be successful PHP programmers and we will talk about them in our next tutorial PHP Operators, so stay tuned.

Further Readings:

  1. Understanding PHP Data Types: http://eu.dummies.com/WileyCDA/how-to/content/understanding-php-data-types.html
  2. PHP Variables: http://www.w3schools.com/PHP/php_variables.asp
  3. What is a Variable: http://www.homeandlearn.co.uk/php/php2p1.html
  4. Learn PHP from Scratch: http://net.tutsplus.com/tutorials/php/learn-php-from-scratch-a-training-regimen/