Chapter 7. Variables

Now that PHP syntax has been converted to Node.js in the previous chapter, we can turn our attention to variables, which are a little more interesting and complicated to convert.

In PHP, a variable name always starts with a dollar sign ($). For example, $a and $colors are PHP variable names, but a and colors are not. In Node.js, a variable name may start with a dollar sign ($), but does not have to. So, in Node.js, $a, $colors, a, and colors are all valid Node.js variable names.

However, in Node.js, even though it is perfectly legal, it is a widely accepted common practice to avoid variable names that begin with a dollar sign ($). It is so widely accepted that Node.js code that uses variable names that start with a dollar sign ($) looks very, very strange to Node.js developers. To accommodate this practice, it is recommended that PHP variable names be converted to Node.js variable names by removing the dollar sign ($). For example, the PHP variables $a and $colors should be converted to the Node.js variables, a and colors.

The allowed characters in a PHP variable name is a subset of the allowed characters in a Node.js variable name. It is recommended that variable names, excluding the initial dollar sign ($) for PHP variables, begin with an alphabetic character of either case, that is, a to z or A to Z, and have the remaining characters be zero or more characters that are alphabetic characters of any case (a to z or A to Z), underscores ( _ ), or digits (0 to 9). ...

Get Node.js for PHP Developers now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.