Variables in PHP

This reference shows how to work with variables in PHP.


Edited: 2020-07-24 19:18

In variables begins with a dollar sign ($), and must be followed either by an underscore (_) or a letter (A-Z).

Variables refer to a specific memory location, so that information stored can be easily accessed and manipulated later.

In object oriented PHP, where variables are found inside classes, they are described as properties of the object.

This is a variable in PHP:

$my_variable_name = 'some value';

Use of Variables

You should use variables when you want to save information temporarily for later use.

The below example outputs "My name is Santa" in two different ways, both uses the information we stored in the variable $MyName.

Here is an example:

$my_name = 'Santa';

echo 'My name is ' . $my_name; // Using String Concatenation

echo "My name is $my_name"; // Using Double Quotes

Storing something in a variable, will allow you to use the variable throughout the script, if you want to store the information for longer then that, you may want to consider using a database, or session variables.

Data Types

The data types of your Variables will automatically be determined doing execution of your script, but you should still think about how you assign values to your variables.

For example, you might want to declare numeric values as integer rather than as a string. To do that, you should leave the value unquoted:

$just_a_number = 30;

Types can be checked with the type checking functions.

Below are some data types available in PHP:

  1. Integer
  2. String
  3. Array
  4. Boolean
  5. Object

The Numeric types are:

  1. Integer
  2. Double

The Integer Type contain number values, while the Double Type contain floating point values, see below:

$number = 100; // Integer
$floating_number = 99.5; // Double

The String Data Type

Strings are used for everything from binary image data to simple text pieces.

In PHP, strings are written using either double quotes or single quotes, and can contain both letters and numbers.

$my_name = 'Santa'; // Only letters
$santa = 'Santa the 1th'; // A combination of letters and numbers

The Array Data Type

Is used to easily store many values, in a single variable. This can be done the following ways:

$user_info = array();
$user_info[0] = 'Santa';
$user_info[1] = 'The 1th';
$user_info[2] = 'Active:Admin';

Associative arrays uses a string as the key:

$user_info['Name'] = 'Santa';
$user_info['UserCount'] = 'The 1th';
$user_info['Status'] = 'Active:Admin';

Arrays can also be defined with square brackets:

$some_array = [
  'one' => 'some value',
  'two' => 'another value'

Or, in older versions of PHP:

$some_array = array(
  'one' => 'some value',
  'two' => 'another value'

String Concatenation

Another aspect of strings is refereed to as String Concatenation; concatenation allows you to connect multiple strings and variables using the "." dot operator.

Below is an example of this:

$my_name = 'Santa';
$my_string = 'My name is: ' . $my_name;

  // With multiple variables
$my_name = 'Santa';
$user_status = 'Active:Admin';

$my_string = 'My name is: ';

echo $my_string . $my_name . "\n" . 'UserStatus: ' . $user_status;

Get the syntax right, and you can enter in just about anything, just like you would in your own language.

The "\n" is just a simple linebreak; in order for PHP to understand it as a linebreak, we will need to use double quotes.

If we use Single Quotes, it will just appear as the literal characters "\n" instead of the desired linebreak; Of course, we may want to use PHP_EOL for linebreaks to maximize cross-platform compatibility.

Tell us what you think:

  1. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  2. How to create a router in PHP to handle different request types, paths, and request parameters.
  3. Tutorial on how to use proxy servers with cURL and PHP
  4. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.
  5. How to effectively use variables within strings to insert bits of data where needed.

More in: PHP Tutorials