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 $name and $Name are 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 $A with the value “Hello How are You” and a float $Num with the value 3.12, and then outputs their values to the browser.
Scroll to Top