You are currently viewing PHP Variables

PHP Variables

In order to know PHP variables, we need to understand the variables in programming.

What is a variable?

A variable is a way of storing information in your computer. This information can vary. We can also define a variable as a container or a storage space that holds information in the memory which can be reused throughout the program.

In mathematics, variables are named as letters like x and y. but in programming, programmers named them with more meaningful, descriptive names. We can assign a variable to an element of a program like user inputs, a number, the name of a database, or a calculated value.

Declaring PHP variables

In PHP programming, the name of the variable should be started with $ sign.

<?php
          $domain = "domesticatedbrain.com";
          echo "$domain";
?>

Rules for creating a PHP variable

The name of the variable should be started with the $ sign.

Example – $domain

The name of the variable can start with a letter or an underscore sign and cannot start with a number.

The name of the variable can only contain alphanumeric characters and underscores.

The name of the variable is case sensitive. Therefore, the variable $domain and the variable $DOMAIN are different to each other.

<?php
       $domain = "domesticatedbrain.com";
       echo "$DOMAIN";
?>

The above PHP program will give an error message like. 

(Notice: Undefined variable: DOMAIN in C:\xampp\htdocs\dmbtest\variable.php on line 4)

To assign a value to a variable we use = sign and should use quotes around the text values.

$domain = "domesticatedbrain.com";

We do not need to declare the data type as the PHP automatically converts the variable into a required data type.

Leave a Reply