Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive ($age
and $AGE
are two different variables). Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. Variable name cannot start with a number
Creating or Declaring PHP Variables
It is not necessary to initialize variables in PHP however it is a very good practice.
<?php $txt="I love PHP Programming"; $x=5; $y=10; ?>
Output Variable
PHP echo
statement is often used to output data to the screen.
<?php echo "I love PHP Programming"; $x=5; $y=10; echo $x+$y; echo "My Rating is ".$x; ?>
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.