PHP variables are containers used to store data such as text, numbers, and decimal values. They begin with a dollar sign and are dynamically typed.
Change the values below to see how PHP handles variable assignment and interpolation.
A variable in PHP is a container for storing information. It starts with a dollar sign ($), followed by the variable name.
Variables are declared and assigned using the = operator. PHP automatically detects the data type.
// Declaring variables of different data types
<?php
$txt = "Hello PHP";
$number = 25;
$price = 99.99;
echo $txt;
echo "<br>";
echo $number;
echo "<br>";
echo $price;
?>
$ sign, followed by a letter or underscore.A-z, 0-9, _).$value and $Value are different).
// Using variables inside a string with interpolation
<?php
$name = "John";
$age = 30;
echo "My name is $name and I am $age years old.";
?>
The variables $name and $age are replaced with their values directly inside the double-quoted string.
$totalPrice, $userName)."Hello $name").var_dump() to inspect variable types and values.variables.php.echo to print a sentence using those variables.http://localhost/variables.php.