PHP $ and $$ Variables- Tutorialpath

Hello to all of you, thanks for landing on the TutorialPath.com, which is “Your Path to Learn Everything” in an easy way and you can learn almost every sort of important elements about online things. As you land here, so we can assume, you are a newbie for PHP $ and $$ Variables language, so you don’t need to worry about it.

Welcome to all our readers on our website at www.tutorialpath.com. As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which help you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world. As you know technology is one of the trending part in this world. So, here again we came up with a latest and new tech information which is related to the process of creating Basic PHP $ and $$ Variables for Beginners. With the help of this article you will get to know the complete process of how to create Basic PHP $ and $$ Variables for Beginners in a step by step manner. So, simply have a look to this article and grab all the useful information which is going to be very helpful for you.

 

What is Variable in PHP

Variables are used to store data, like string of text, numbers, etc. Variable values can change over the course of a script. Here’re some important things to know about variables:

  • In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.
  • After declaring a variable it can be reused throughout the code.
  • The assignment operator (=) used to assign value to a variable.

In PHP variable can be declared as: $var_name = value;

 

Difference Between $var and $$var in PHP

$$var uses the value of the variable whose name is the value of $var.

The $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc.

The $$var (double dollar) is a reference variable that stores the value of the $variable inside it.

To understand the difference better, let’s see some examples:

Example:

Output:

In the above example we have created two variables where first one has assigned with a string value and the second has assigned with a number. Later we’ve displayed the variables values in the browser using the echo statement. The PHP echo statement is often used to output data to the browser. We will learn more about this in upcoming chapter.

we have assigned a value to the variable x as abc. Value of reference variable $$x is assigned as 200.

Now we have printed the values $x, $$x and $abc.

Example:

Output:

Naming Conventions for PHP Variables

These are the following rules for naming a PHP variable:

  • All variables in PHP start with a $ sign, followed by the name of the variable.
  • A variable name must start with a letter or the underscore character _.
  • A variable name cannot start with a number.
  • A variable name in PHP can only contain alpha-numeric characters and    underscores (A-z0-9, and _).
  • A variable name cannot contain spaces.

Leave a Reply

Your email address will not be published.