PHP - Variables

Variables in a program are used to store some values or data that can be used later in a program. The variables are also like containers that store character values, numeric values, memory addresses, and strings. PHP has its own way of declaring and storing variables. There are a few rules, that need to be followed and facts that need to be kept in mind while dealing with variables in PHP: Any variables declared in PHP must begin with a dollar sign ($), followed by the variable name. A variable can have long descriptive names (like $factorial, $even_nos) or short names (like $n or $f or $x) A variable name can only contain alphanumeric characters and underscores (i.e., ‘a-z’, ‘A-Z’, ‘0-9, and ‘_’) in their name. Even it cannot start with a number. A constant is used as a variable for a simple value that cannot be changed. It is also case-sensitive. Assignment of variables is done with the assignment operator, “equal to (=)”. The variable names are on the left of equal and the expression or values are to the right of the assignment operator ‘=’. One must keep in mind that variable names in PHP names must start with a letter or underscore and no numbers. PHP is a loosely typed language, and we do not require to declare the data types of variables, rather PHP assumes it automatically by analyzing the values. The same happens while conversion. No variables are declared before they are used. It automatically converts types from one type to another whenever required. PHP variables are case-sensitive, i.e., $sum and $SUM are treated differently. Data types used by PHP to declare or construct variables: Integers Doubles NULL Strings Booleans Arrays Objects Resources