Difference between $ and $$

This is one of the most common question asked in interviews, whether you are a beginner or an experienced developer you must know the difference between $ and $$.





Before explaining difference between $ and $$, let's know what is $?

In PHP $ is used to declare a variable. The variable names must be start with the $ sign.

Example

$a = 10;
echo $a;

$$ is a reference variable while $ is a normal variable. In other words we can say that $ is a normal variable that can hold normal values like integers, strings, floats etc. But $$ variables can hold a value that points another variable.

Let's clear this with an example

$cats = "dogs";
$$cats = "Tommy";
echo $cats;
echo $$cats;
echo $dogs;

Output

Dogs
Tommy
Tommy



Let's make it more simple to understand
${$cats} == $dogs

Now I think we have understood the concept of $ and $$ and also the differences.

If this tutorial is helpful for you please support us by following our Facebook page.







Post a Comment

0 Comments