• Home
  • PHP Variables and Constants

PHP Variables and Constants

In PHP, variables are used to store data. A variable is a storage location in the computer’s memory that is used to store data. The data that is stored in a variable can be of any data type, such as a string, an integer, or an array.

To create a variable in PHP, you need to use the $ symbol followed by the name of the variable. For example:

$name = "John";
$age = 30;
$salary = 5000;

In the above example, three variables are created with the names $name, $age, and $salary. The $name variable is assigned the value “John”, the $age variable is assigned the value 30, and the $salary variable is assigned the value 5000.

In PHP, there are also constants, which are variables that cannot be changed or modified once they are set. To create a constant in PHP, you need to use the define() function. For example:

define("PI", 3.14);
define("MIN_AGE", 18);

In the above example, two constants are created with the names PI and MIN_AGE. The PI constant is assigned the value 3.14, and the MIN_AGE constant is assigned the value 18.

Once a constant is set, it cannot be modified or changed. If you try to change the value of a constant, you will get an error.

Constants are often used in PHP to store values that are used frequently throughout the code, such as the value of PI or the minimum age for a certain activity.