PHP: Variable

Variables are identifiers for some data storage in the programming. We can save some data in memory using a variable, and the variable works as a named identifier of that data storage memory address.

PHP: Variable Naming
PHP: Variable Naming

PHP variable naming starts with a dollar sign($) and then there is an identifier(variable name). The identifier/name of the variable should follow certain rules.

WARNING

Variable names in PHP are case-sensitive. So $myVar, $MyVar, and $mYVAR represent 3 different variables.

Function names are not case-sensitive in PHP.

Variable Naming Rules

An identifier/variable name should-

  • A non-empty sequence of characters.
  • Start with a dollar sign($).
  • Can only contain letters(a-z, A-Z, non-English Unicode characters), numbers(0-9), and underscore(_).
  • Can not be a reserved keyword(mentioned below).

Variable Naming Convention

We have the following conversion for naming a variable in PHP-

  • Should be meaningful and related to the usage of the variable.
  • After the dollar sign($) the first character should be lowercase.
  • If the variable name contains multiple words then the first letter of each word should be capitalized (except the very first character of the variable name). This is called camel case.
  • Do not use underscore(_) in variable names (except the constant naming or there is a specific requirement for underscore).

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.