Friday, December 15

Basics of Php Variables

Google+ Pinterest LinkedIn Tumblr +

Basics of PHP – Part 3

This is part 3 of my series, Basics of PHP. In this part of the series, I give you the basics of PHP variables. PHP has variables, similar to mathematical variables. In the strict sense, they do not behave like mathematical variables. In this part of the series, we look at the meaning of PHP variables and some of the type of values that can be assigned to them. The variable name is the name, you, the programmer give.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at That is, contact me for the slightest problem you have about what you are reading.

Consider the following statement;

    $myStr = “This is the third part”;

In the above statement, the variable is, $myStr, meaning, my String. The value “This is the third part” is in quotes. It is a string. It is assigned to the variable, $myStr, using what is called the assignment operator, “=”.

A variable begins with a dollar sign followed by the name of the variable. A valid variable name begins with a letter or underscore, followed by any number of letters, numbers, or underscores.

Consider now the following two consecutive statements:

    $myStr = “This is the third part”;
    echo $myStr;

The first statement assigns the value, “This is the third part” to the variable, $myStr. The second statement sends the value assigned to the variable to the browser. The second statement has the, echo construct. What goes next to the construct, echo, is called an argument to echo. So $myStr is an argument to echo. There are many situations in computing where you would use the variable instead of the value. Try the following code:


        $myStr = “This is the third part”;
        echo $myStr;

Numbers as Values
You can use numbers as values. You can assign a number to a variable. Consider the following two statements:

    $myNum = 56.48;
    echo $myNum;

The number, 56.48 is assigned to the variable, myNum. When you are assigning a number, you do not have to put the number in quotes. The second line sends the number to the browser. Here, the argument to the echo construct is, myNum. Try the following code:


    $myNum = 56.48;
    echo $myNum;


Assigning a Variable to another Variable
You can assign a variable to another variable. Consider the following two statements:

    $str1 = “test”;
    $str2 = str1;

The first statement assigns the string value, “test” to the variable, $str1. The second statement assigns the variable, $str1 to $str2. The value of $str2 is “test” copied from str1.

Changing the Value for a Variable
You can assign a value to a variable and then change it after that. Consider the following statements:

    $myStr = “test”;
    $myStr = “good”;

The first statement assigns the value, “test” to the variable, $myStr. The second statement assigns a new value to the same variable. The final value of, $myStr is “good”.

In the above section, the value, “test” is first assigned to $myStr and then “good” is assigned to the same variable. This means the variable $myStr is not constant. If you want to prevent a variable from being assigned a new value, you should precede the statement with a reserved word, which is, const. A reserved word is a word you do not use arbitrarily in code. The following code illustrates this. Try it and notice that the value of the variable, $myStr is not changed (the code may not work with your version of PHP).


    const $myStr = “beautiful”;
             $myStr = “handsome”;
    echo $myStr;


Boolean Variable
In some situations, you can have one of two possible values. The value can either be, true or false. Any variable that deals with these two values is called a Boolean variable. So, you can have something like:

    $myVar = true;


    $myVar = false;

You do not need quotation makes around true or false.

Rule for Naming a Variable
A variable name must start with a letter or underscore, ‘_’. Within the name and at its end, you can have a letter, number or underscore. You precede all that with a $ sign.

Case Sensitivity in PHP
PHP is said to be case sensitive. This means that for variable names, $myVar is not the same as $MyVar or $myvar or $MYVAR, etc.

Creating a Variable
Before you can use a variable, you have to create it. To create a variable, you begin with the $ sign and then the name of the variable; just as we have been doing above. You do not have to assign a value to a variable when you create it (but end it with a semicolon). You can do the assignment later. The following code illustrates this:

        $myVar = “you”;
        echo $myVar;

When to use Quotation Marks for Values
If your value is a number or a Boolean value, you do not have to use quotation marks. If your value is a string, you must use quotations marks.

A string is a value in quotes. The quotes may be single (‘) or double (“).

An integer is a whole number. It could be a negative number, zero or a positive number.

Floating Point Number
A floating-point number is a number with a decimal point. There are several ways of writing it.

Boolean Value
A Boolean value is either true or false. Now, 0 is equivalent to false. Any other number is equivalent to true. Note: an empty string (“”) is equivalent to false.

When used as a value, a string, integer, floating-point number or Boolean value is called a literal.

We have seen a lot. Let us take a break here and continue in the next part of the series.


To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (use menu if available):

Getting started with PHP
PHP Basic Syntax
Basics of PHP Variables
PHP Conditional Statements
Boolean Logic for PHP
Boolean Logic and PHP Conditions
PHP Comparison and Arithmetic Operators
PHP Loop Statements
PHP Function Basics
PHP Array
Some PHP Predefined Functions and Arrays
PHP Variable Scope Basics
PHP Object Basics
PHP Reference
PHP Error Basics
PHP String and Date Basics
PHP Form Simple Validation with embedded Error Messages from Server


About Author

Leave A Reply