PHP

PHP Variables

A variable is a place  where you can store things, such as  number, a date  or some text. You can put the text or number into variables so you can retrive them later or so you can manipulate them. Variables  are called variables beacuse the value that they can vary. 

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 within the variable can then be displayed by referencing it using the variable like this :

echo $first_name;

Recommended Tutorials for you


Tags

Anuj kumar

This is Anuj Kumar. I’m a professional web developer with 5+ years of 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. .

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Check Also
Close
Back to top button
Close
Close