PHP Versus JavaScript Variables

A comparison of PHP vs. JavaScript variables. When programming or learning two different languages such as PHP and JavaScript, which are often used together, it is helpful to compare aspects of the two languages. This helps with remembering the similarities and differences between the languages to avoid confusion. PHP versus JavaScript variables are compared in this article.

PHP Versus JavaScript Variables Comparison

Both PHP and JavaScript are loosely typed languages because they do not require variable types to be defined. A new variable can be assigned any type of value such as integer, floating point, string, etc. Variable names in both languages are case sensitive.

PHP Versus JavaScript Variables
PHP Versus JavaScript Variables

PHP variable names must start with a dollar sign ($). JavaScript variables can start with a dollar sign, but most programmers don’t use a dollar sign in variable names by convention. It is good programming practice to precede JavaScript variable names with the var keyword.

PHP and JavaScript variable names can consist of alphanumeric characters (A to Z, a to z and 0 to 9) as well as the underscore (_). Variable names may not start with a number, a letter or underscore must be the first character in a variable name.

More details on variables in both PHP and JavaScript follow in the next two sections.

PHP Variables

PHP variables do not need to be declared before using them. However, it is considered  better programming practice if variables are first declared.

PHP variables do not need to be initialized before using them, however it is also considered good programming practice to first initialize a variable.

Naming PHP Variables

Variable names are case sensitive. Valid variable names can be composed of letters A to Z, a to z, numbers 0 to 9  and the underscore _, but must keep to the following rules.

  • Variable names must start with a dollar sign $.
  • At least one alphabet letter or an underscore must follow the dollar sign.
  • Numbers 0 to 9 can be used in the variable name, but a number may not immediately follow the dollar sign.

PHP Variables Reference Manual

See the PHP language reference on variables in the PHP manual for more information on PHP variables.

PHP Data Types

The basic types in PHP that can be assigned to a variable are integer numbers, floating point numbers, booleans, strings, arrays and objects. For a complete list of all PHP data types and an explanation of each, see PHP types in the PHP manual.

JavaScript Variables

JavaScript variables do not need to be declared or initialized before using them. It is good programming practice to both declare and initialize variables.

A variable in a function that is not declared with the var keyword becomes a global variable in JavaScript. For this reason it is best to declare all variables using var. See the JavaScript var reference for more information on using var.

Naming JavaScript Variables

In JavaScript variable names are case sensitive. Names can consist of letters A to Z and a to z, numbers 0 to 9, the dollar sign $ and the underscore _, but must keep to the following rules.

  • Variable names must start with a letter, underscore or dollar sign.
  • Variable names may not start with a number (0 to 9).
  • It is best practice not to use the dollar sign in variable names as the dollar sign is used in various JavaScript libraries.

JavaScript Data Types

Some basic JavaScript data types that can be assigned to variables are integer numbers, floating point numbers, booleans, strings, arrays and objects. See the JavaScript data types reference for more information on JavaScript data types.

Leave a Reply

Your email address will not be published. Required fields are marked *