Simple Variables

Simple Variables

The var statement sets a “scope,” that is, an area of source code where the variable name refers to this newly defined variable. Toward the end of this chapter, variable scopes will

be covered in more detail, but for now, it can simply be noted that a var statement has

a scope. In PHP, variables are not declared. When they are first used, the PHP engine identifies

them by the dollar sign ($) in their name at the time of their first use. To convert a variable from PHP to Node.js, the dollar sign ($) is removed and the var

keyword added at its first use. This find-and-replace action removes the dollar sign ($) and allows you to identify where to add the var keyword:

Operation: "Find/Replace" in Eclipse PDT Find: $ Replace: Options: None Action: Find, then Replace/Find

At each occurrence, determine if this is the first time that variable is used. If it is, add the var keyword before the first use or at the beginning of the appropriate statement block. Statement blocks are defined with curly brackets ( { and } ).

With the dollar sign ($) removed and var keyword added, the basic variable declaration and variable reference will now work in Node.js.

In PHP, a variable can be one of nine basic types. The basic type of a PHP variable can

be determined by calling the PHP gettype() API function. The following list shows the nine strings that can be returned from a PHP gettype() API function call:

NULL boolean string integer double

126 | Chapter 7: Variables 126 | Chapter 7: Variables

In Node.js, a variable can be one of seven basic types. The basic type of a Node.js variable can be determined by using the Node.js typeof operator. The following list shows the seven strings that can be returned from the Node.js typeof operator:

null boolean string number object function undefined

The first three basic types in both lists—null, boolean, and string—are arguably the most important, and are essentially the same. The PHP integer and double basic types correspond very closely to the Node.js number basic type. Similarly, the PHP object and array basic types very closely correspond to the Node.js object basic type that encom‐ passes both Node.js arrays and Node.js objects. The remaining basic types are somewhat obscure and largely irrelevant although the PHP unknown type basic type is a rough equivalent to the Node.js undefined basic type.

Nulls in PHP and in Node.js are the same, except that PHP is case insensitive while Node.js is case sensitive. There is only one value, null, which is of this type in both languages. To make PHP code compatible with Node.js, the following find-and-replace action will replace all uppercase and mixed case occurrences of the NULL text that might occur in PHP code with the lowercase null text, which is compatible with both PHP and Node.js:

Operation: "Find/Replace" in Eclipse PDT Find: NULL Replace: null Options: None Action: Replace All

Booleans in PHP and Node.js have two possible values, true and false. Again, PHP is case insensitive but Node.js is case sensitive. Since lowercase boolean values are accepted in both PHP and Node.js, all PHP and Node.js code should be refactored or converted to use lowercase true and lowercase false versions:

Operation: "Find/Replace" in Eclipse PDT Find: TRUE Replace: true Options: None Action: Replace All

Operation: "Find/Replace" in Eclipse PDT

Simple Variables | 127

Find: FALSE Replace: false Options: None Action: Replace All

Strings are equivalent and interchangeable between PHP and Node.js. The differences between string literals, including single quotes (') versus double quotes ( "), was described and converted in the previous chapter.

Integers and floating-point numbers, also called “doubles” in PHP, are of different types in PHP, but in Node.js, they are both considered numbers. Specific integer numbers, such as 10 or 4322, and floating-point numbers, such as 4.22 and 1033.48, are repre‐ sented exactly the same in both languages and need no conversion between PHP and Node.js. Even specific numbers in scientific notation, such as the number, 14.8e3, are represented exactly the same in both PHP and Node.js. No find-and-replace actions are needed to handle differences between numbers in PHP and Node.js.

Arrays and objects are considered separate types in PHP, but in Node.js, an array is a type of object. In the previous chapter, some aspects of PHP arrays were explained to support the conversion to PHP foreach statements to Node.js for…in statements. The remaining aspects will now be explained as well as how to convert these remaining aspects from PHP to Node.js.