What are the variables in PHP
- Variables are containers that store data (like text, numbers, etc.) within your PHP code.
- They act like temporary storage locations for information that your script needs to use.
How to Declare a Variable:
- All PHP variables start with a dollar sign (
$). - Example:
$name,$age,$price,$product_id.
Naming Variables:
Variables are Case-sensitive which means
$nameand$Nameare considered different variables.Variables can contain letters (a-z, A-Z), numbers (0-9), underscores (_), and dollar signs ($).
A variable must start with a letter, underscore, or dollar sign.
A variable cannot start with a number.
Example:
<?php
$A = "Hello How are You";
echo $A;
$Num = 3.12;
echo $Num;
?>
In this example:
- This PHP code declares two variables, a string
$Awith the value “Hello How are You” and a float$Numwith the value 3.12, and then outputs their values to the browser.