What is Variables in PHP
Variables are used to store data, like text strings, numbers or arrays.
Important things to know about variables in PHP:
• In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.
• After declaring a variable it can be reused throughout the code.
• The assignment operator (=) used to assign value to a variable.
In PHP variable can be declared as: $var_name = value;
In the above example we have created two variables where first one has assigned with a string value and the second has assigned with a number.
Naming Conventions for PHP Variables
These are the following rules for naming a PHP variable:
All variables in PHP start with a $ sign, followed by the name of the variable.
A variable name must start with a letter or the underscore character “_”.
A variable name cannot start with a number.
A variable name in PHP can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).
A variable name cannot contain spaces.
What is Constants in PHP
A constant is an identifier (name) for a simple value. A constant value cannot change during the execution of the script (except for magic constants). Constants are useful for storing data that doesn’t change while the script is running. Common examples of such data include configuration settings (such as database usernames and passwords).
Constants are defined using define() function, which accepts two arguments: the name of the constant, and its value. Here is an example of defining and using a constant in a script:
Naming Conventions for PHP Constants
Name of constants must follow the same rules as variable names, which means a valid constant name must starts with a letter or underscore, followed by any number of letters, numbers, or underscores, with one exception: the $ prefix is not required for constant names.