Difference between $var and $$var in PHP

By Shahnail Khan Last updated : December 1, 2023

PHP $var

This is a standard variable that you can use to store different types of data, such as strings (text), integers (whole numbers), floats (decimal numbers), and more. It acts as a container for holding various values.

PHP $$var

Here, the double dollar sign ($$) indicates a reference variable. Instead of storing a direct value, it captures and holds the value contained in the variable represented by $var. Essentially, $$var refers to the value stored in $var, allowing you to indirectly access or refer to the original value stored in $var. It serves as a way to reference another variable's value.

PHP $var and $$var Examples

Let's take some examples to get a better idea of $var and $$var.

Example 1

<!DOCTYPE html>
<html>
<body>

<?php
// Example: Student Information System
$studentName = "Rohan"; // String variable storing the student's name
$studentID = "ABC2345"; // String variable storing the student's ID
$Class = 12; // Integer variable storing the student's class
$mathGrade = 92; // Integer variable storing the math grade
$englishGrade = 85; // Integer variable storing the English grade
$programmingGrade = 94; // Integer variable storing the programming grade

// Calculate average grade using regular arithmetic operations
$averageGrade = ($mathGrade + $englishGrade + $programmingGrade) / 3; // Regular variables used in calculations

// Displaying Student Information
echo "<h1>";
echo "Student Name: $studentName<br>"; // Display the student's name
echo "Student ID: $studentID<br>"; // Display the student's ID
echo "Class: $Class <br>";

// Displaying Grades

echo "</h1> <h2>Grades -<br>";
echo "Math: $mathGrade<br>"; // Display the math grade
echo "English: $englishGrade <br>"; // Display the English grade
echo "Programming: $programmingGrade<br>"; // Display the programming grade

// Display Average Grade
echo "\nAverage Grade: $averageGrade"; // Display the calculated average grade

?>

</body>
</html>

Output:

The output of the above example is:

PHP Variable Example 1

In this example,

  • Normal variables ($var) are used to store various pieces of information about a student, such as their name, ID, class, and grades in different subjects.
  • Arithmetic operations are performed using the $ variable to calculate the average grade.
  • The echo statements are used to display the values, providing information about the student and their grades.

Example 2

<!DOCTYPE html>
<html>
<body>

<?php
// Example: Dynamic Variable Names
$color = "blue";
$$color = "black";
echo "<h1>";
// Using $$var to access the dynamically named variable
echo "Second color: $blue <br>";

// Modifying the value of the dynamically named variable
$$color = "orange";
echo "New Color:\n" . $$color;
?>

</body>
</html>

Output:

The output of the above example is:

PHP Variable Example 2

In this example,

  • $color is a normal variable storing the initial value of color.
  • $$color is a double variable storing the string "black."
  • The first echo statement accessed the initial value of color ($$blue) and then displayed the second color name.

Example 3

<!DOCTYPE html>
<html>
<body>

<?php
// Example: Variable Variables
$firstName = "Shahnail";
$lastName = "Khan";
$variableToAccess = "lastName";

// Using $$var to access the dynamically named variable
echo "<h1>";
echo "First Name: $firstName <br>";
echo "Last Name: $lastName <br>";
echo " Full Name: $firstName ${$variableToAccess}\n";
?>

</body>
</html>

Output:

The output of the above example is:

PHP Variable Example 3

In this example,

  • $firstName and $lastName are $ variables storing the first and last names.
  • $variableToAccess is a normal variable storing the string "lastName," which will be used as the dynamic variable ($$) name.
  • The echo statement displays the full name by accessing the dynamically named variable using $$var.

Difference between $var and $$var

The main difference between the $var and $$var is that the $var is just a variable whereas the $$var is a reference variable. The variable ($var) is used to store the variable's value whereas the variable $$var is used to store the variable's reference.

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.