PHP Variables

Creating (Declaring) PHP Variables

In PHP, a variable is a container that can store a value, such as a string, number, or array. To declare a variable in PHP, you use the dollar sign $ followed by the variable name. Here’s an example:

$name = "John";

In this example, we’re declaring a variable called $name and setting its value to the string “Mohan” .

Note that PHP is a dynamically typed language, which means that you don’t need to specify the type of the variable when declaring it. The type of the variable is determined automatically based on the value that you assign to it. For example, if you assign a string value to a variable, the variable becomes a string type; if you assign a number value to a variable, the variable becomes a numeric type.

You can also declare variables without assigning a value to them. In that case, the variable will have a default value of NULL . Here’s an example:

$age;

In this example, we’re declaring a variable called $age without assigning a value to it. The variable will have a default value of NULL.

Once you’ve declared a variable, you can use it in your PHP code by simply referring to its name with the dollar sign prefix. For example:

echo "Hello, " . $name . "!";
Scroll to Top