Variables in PHP are used to store data values. In PHP, a variable starts with the dollar sign ($) followed by the name of the variable. Here’s an overview of PHP variables:
$variable
is different from $Variable
).$name = "John"; // Assigning a string value to a variable
$age = 30; // Assigning an integer value to a variable
$height = 5.9; // Assigning a floating-point value to a variable
$isStudent = true; // Assigning a boolean value to a variable
echo $name; // Output: John
echo $age; // Output: 30
echo $height; // Output: 5.9
echo $isStudent; // Output: 1 (true value in PHP is displayed as 1)
$globalVariable = "I am a global variable";
function testScope() {
$localVariable = "I am a local variable";
echo $globalVariable; // Error: $globalVariable is not accessible inside the function
echo $localVariable; // Output: I am a local variable
}
testScope();
echo $globalVariable; // Output: I am a global variable