Variable

Welcome! A variable is a mean to store values such as strings or integers so we can easily reuse those values in our code. For example, we can store a string value such as "I Love PHP" or an integer value such as 100 into a variable. When ever we need to use these values, we simply call that variable instead writing out those values over and over again in our code. Here is an example.

<?php
$str="I love PHP"; //here is one variable with a string value
echo $str; // here we print the $str value onto the browser

$num = 100; //another variable with a integer value
echo $num; // here we echo the $num variable to print its value

echo $str; //here we reuse the $str variable to print its value again in our code
echo $num; //here we reuse the $num variable to print its value again in our code
?>

The following code should print "I Love PHP100I Love PHP100". Try it. Copy the code in an empty file, save the file as php and run it on your browser.

Variables in PHP

Every language has its own semantics of defining variables. In PHP, we define a variable starting with a $ sign, then write the variable name and finally assign a value to it. Here is an example below.

$variable_name = value;

You must add the $ sign when declaring variables, otherwise it will not work. It is advisable that when you define a variable, you must initilaize it by assign it a value.

Working With PHP Variables

Few Naming Rules

There are few things we need to note when working with PHP variables.

  • A variable name should only start with underscore "_" or a letter.
  • A variable can only contain letters, numbers or underscore. In another words, you can't use other funky characters like <^# in your variables names.

Declaring a PHP Variable

Let say we want to store values using variables in PHP. Here's how we do it.

  • First, think of a logical name for a variable. For example, if you we want to store the text of your company logo in a variable, the logical name for that variable would be something like $logo_text. Remember, you can name your variables whatever you like. But you must choose logical names so it is easier to remember what they are.
  • Assign that variable a value.
  • Remember to put the $ sign in front of the variable name.

Let's look at some example...

PHP String Variable

Example - PHP variable with a string value

<?php $logo_text = "PHP-Learn-It! Learn PHP by examples"; echo $logo_text; ?>

In the above code we define a variable name $logo_text, assign it a string value "PHP-Learn-It! Learn PHP by examples" and print its value onto the page.

 

PHP Integer Variable

Example - PHP variable with an integer value

?php $number = 100; echo $number; ?> In the above code we define a variable name $number, assign it an integer value of 100 and print its value onto the page.

PHP Float or Decimal Variable
Example - PHP variable with a float or decimal value

<?php $decimal_number = 100.01; printf("%.2f", $decimal_number); ?>

In the above code we define a variable name $decimal_number, assign it a decimal value of 100.01 and print its value onto the page.

Summary

Some key points to remember when declaring variables in PHP

  • Remember to always put $ sign in front of variable names. That tells PHP that we're working with a variable.
  • In PHP a variable name must start with a letter or an underscore followed by combination of letters, number or underscores.
  • PHP variables are case sensitive, that means they could be either lower case or upper case or combination of both.
  • You don't have to worry about declaring the type of the variable. For example strings and integers are declared the same way.
  • It's good practice to initialize a variable i.e. assign it a value. Uninitialized variables in PHP have a value of either false, empty string or empty array.