PHPGurukul

PHP Variables

A variable is a named container in a PHO script in which a data value can be stored.

The stored value can be referenced using the variable’s name and changed as the script proceeds. The script author can choose any name for a variable providing it adheres to these three naming conventions:

  • Name must begin with a $ dollar sign, for example, $name
  • Name can comprise letters, numbers, and  underscore characters, but not spaces – for  example, $subtotal_1
  • The first character after the $ dollar sign must be a letter or an underscore character –  it cannot be a number.

Note that variable names in PHP are case-sensitive so $name, $Name, and $NAME are three separate individual variables.

PHP variables are loosely typed meaning they can contain data of any type, unlike  “strongly typed” variables in some languages where the data type must be specified when the variables are created. So, PHP variables may happily contain an integer, or a floating-point number, or a string of text characters, or a Boolean value of True or FALSE, or an object, or a NULL empty value.

Do not confuse the purpose of double and single quotes. Remember that PHP only makes variables substitutions for mixed strings enclosed with double quotes.

 A variable is created in PHP script simply by stating its name. The variable can then be assigned an initial value (initialized) by using the = assignment operator to state its value. This statement, and all others in PHP,  must end with a semi-colon like this:

$first_name="John";

The value contained with in the variable can then be displayed by referencing it using the variable like this :

echo $first_name;

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.