String Literals

String Literals

For string literals, which are not to be confused with string variables, both a single quote (') and a double quote ( ") can be used in both PHP and Node.js. In Node.js, both kinds of quotes are interchangeable with each other and perform in exactly the same way. But, in PHP, the single quote (') means a literal string with no substitutions while a double quote ( ") will substitute variables and delimited special characters. Here’s a PHP example using both kinds of quotes:

$name = "Bill" ; print 'My name is $name.\n' ; print "\n" ; // so the next line will display on a new line print "My name is $name .\n" ;

The output of the preceding PHP code shows the same string for single quotes ('), and for the double quotes ( "), substitutes the string Bill for the $name variable and converts the \n text into an new line rather than printing a literal backslash-n:

String Literals | 109

My name is $name.\n My name is Bill.

By comparison, in Node.js, both single quotes (') and double quotes ( ") will never sub‐ stitute variables and will always substitute delimited special characters. The following Node.js example corresponds to the preceding PHP example. For the time being, simply accept and ignore the var keyword (it will be covered in the next chapter, which explains variables and scoping):

var name = "Bill" ; console . log ( 'My name is name.\n' ); console . log ( "My name is name.\n" );

The output of the Node.js code shows two strings, both without variable substitution and both with the special newline character \n, removed and replaced by an actual new line:

My name is name. My name is name.

Lots of existing PHP code has single quotes (') and double quotes ( ") arbitrarily used for string literals with little rhyme or reason, only dependent on the whim of the developer at the time. A more rigorous approach for both PHP and Node.js is to always use single quotes (') unless either a literal double quote ( ") is needed or the substitution semantics of a double quote ( ") is needed. The following example, applicable to both PHP and Node.js, shows why preferring single quotes (') by default is a good approach:

'The man said, "Throw the ball to me."'

The string literal above produces what is expected in both PHP and Node.js. To fix up PHP or Node.js code, the following find-and-replace action can be taken. In

PHP and Node.js, it can be performed on the entire file:

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

Each occurrence should be manually examined to see if any single quotes (') are used inside the string literal, needing to be converted to double quotes ( ") or needing to be delimited by becoming a backslash-single quote (\'). Each occurrence will also need to

be examined to ensure that it is not inside a comment and that the code will perform as expected.

With some tweaking of both the PHP code and the Node.js code, the visual diff feature or program should be able to match all the string literals in both codebases fairly closely.

With string literals reconciled between PHP and Node.js, we can turn to the matter of the plus (+) operator and string concatenation in both languages. In Node.js, the plus

110 | Chapter 6: Syntax

(+) operator is used both to add two numbers together and to concatenate strings. In PHP, the plus (+) operator is only used to add two numbers, and in a rather obscure and unwise case, to add two PHP arrays in a special way. Instead, in PHP, the dot (.) operator is used to concatenate strings.

For example, the following PHP code adds two numbers and concatenates two strings:

$total = $trees + $plants ; $html = 'The total number of trees and plants is ' . $total . '.'

The dot (.) operator is used in the second statement while the plus (+) operator is used in the first statement.

However, in Node.js, the plus (+) operator is used for both statements:

total = trees + plants ; html = 'The total number of trees and plants is ' + total + '.'

To make string concatenation work in Node.js, the PHP dot (.) operator must be con‐ verted to a plus (+) operator. An incremental find-and-replace action can be executed to make this happen:

Operation: "Find/Replace" in Eclipse PDT Find: . Replace: + Options: Selected lines Action: Find, then Replace/Find

For each occurrence, it must be verified that the dot (.) is being used to do a string concatenation and is not being used in one of the following other ways:

• In a comment for any purpose, including to end a sentence • In a string literal • As part of a floating-point number

While the dot (.) operator is used in PHP to do string concatenation, the dot (.) operator is used in Node.js to access a property of a Node.js object. In the following Node.js example, the number “14” will be printed to the console because the dot (.) operator accesses the length property of the s object. The length property of a string object contains its length in characters:

var s = 'Copyright 2013' ; console . log ( s . length );

Since both the dot (.) operator and the plus (+) operator are used in both PHP and Node.js but serve different important purposes in some instances, careful track must be kept of whether or not the hybrid PHP/Node.js code in the page() function is using the dot (.) operator in the PHP way or the Node.js way. When the dot (.) operator is

String Literals | 111 String Literals | 111

// dot concatenation converted: yes

Earlier, it was mentioned briefly that the plus (+) operator can be used in PHP to add two PHP arrays together in a special way. The plus (+) operator in PHP can be used to create an array that is the union of two PHP arrays. In Node.js, there is no corresponding operator. Rather than attempting to convert a plus (+) operator used in this way, it is recommended that the PHP code be refactored to remove the plus (+) operator for this case and replace it with a call to a PHP function, perhaps an array_union() function, that does the same operation. Then, the array_union() function can be converted from PHP to Node.js just one time rather than each and every time that the PHP code uses the plus (+) operators with arrays directly.

Syntax Differences

A minor difference between PHP and Node.js is one facet of how it handles if…else statements.

In both PHP and Node.js, the standalone if and else statements are used in exactly the same way and work the same way. The difference is the else if statement, an optional statement that occurs after the beginning if statement but before the else statement, if an else statement actually exists.

In PHP, the else if statement uses the elseif keyword. Here’s a PHP example using the elseif keyword:

if ( $a == $b ) { print 'a is equal to b' ;

} elseif ( $a > $b ) { // elseif, not else if

print 'a is greater than b' ; } else { print 'a is less than b' ; }

In Node.js, there is not a special keyword for the else if statement. The else and if keywords are separated by whitespace, and when they are arranged in this way, they perform the same function as the elseif keyword does in PHP. Here’s an example using the Node.js else if keywords:

if ( a == b ) { console . log ( 'a is equal to b' );

} else if ( a > b ) { // else if, not elseif

112 | Chapter 6: Syntax 112 | Chapter 6: Syntax

Since the elseif keyword is a PHP keyword and has no other use, it is safe to do a blind, global find-and-replace action to replace all elseif text with else if text in any .njs file that contains a page() function. The find-and-replace action can even be repeated with no ill effects:

Operation: "Find/Replace" in Eclipse PDT Find: elseif Replace: else if Options: Case sensitive Action: Replace All

Both string concatenation and the else if statement can be handled mechanically but the PHP foreach statement requires more insight when each occurrence is examined. The PHP foreach statement has a rough equivalent in the Node.js for…in statement.

In PHP, the values in an array can be any kind of object. However, keys can only be an integer or a string. In PHP terminology, a PHP array with integer keys is called an “indexed array.” A PHP array with string keys is called an “associative array.” Beyond terminology, PHP does not differentiate much between an indexed array and an asso‐ ciative array.

In Node.js, a PHP-style indexed array is just called an array. A PHP-style associative array is called an “object” in Node.js terminology.

The PHP foreach statement can take two forms. The first form returns only the values of the array. Here’s a PHP example showing a

foreach statement, which shows the values of the $a array:

foreach ( $a as $value ) { print $value ; }

The second form returns both the keys and the values of the array. The following PHP example shows a foreach statement using the second form:

foreach ( $a as $key => $value ) {

print $key . ' contains ' . $value ; print $key . ' contains ' . $a [ $key ]; // $a[$key] is the same as $value

In this Node.js code, you should note that even though the value is returned in its own variable, the value is also obtained from the array itself by using the square brackets ( [ and ] ) operator on the array.

Syntax Differences | 113

In Node.js, the for…in statement (which roughly corresponds to the PHP foreach statement) only has one form. It looks similar to the PHP foreach statement, except that instead of returning values, it returns keys. In Node.js, it is more common to use the terminology “properties” or “property names” instead of “keys.” Here’s a Node.js for…in statement:

for ( var key in a ) {

console . log ( key + ' contains ' + a [ key ]);

To convert PHP foreach statements to Node.js for…in statements, it is best to address PHP indexed arrays separately from PHP associative arrays.

For indexed arrays, it is recommended that PHP foreach statements be refactored as regular for statements so, when they are converted to Node.js, they are converted into regular for statements and not into for…in statements. For the following PHP example, assume that the $colors array is an indexed array that contains three values: red, blue, and green:

foreach ( $colors as $value ) {

print $value . "\n" ; }

Here’s output of this code:

red blue green

To refactor the preceding foreach statement, the PHP count() API function is used to determine the number of indexes in the $colors array variable. The $key variable iter‐ ates over the keys, not the values, so the value needs to be extracted from the $colors array variable each time through the loop:

for ( $key = 0 ; $key < count ( $colors ); ++ $key ) {

print $colors [ $key ] . "\n" ; }

This refactoring is wordy and probably executes a little slower than the foreach version. However, it can be converted from PHP to Node.js in a straightforward way:

for ( var key = 0 ; key < colors . length ; ++ key ) {

console . log ( colors [ key ] + "\n" ); }

By refactoring these foreach statements into regular for statements, no find-and- replace action is needed to convert from PHP to Node.js. In both PHP and Node.js, the keywords and punctuation for for statements are identical to the C language and to

114 | Chapter 6: Syntax 114 | Chapter 6: Syntax

If the PHP foreach version was converted to Node.js to use a for…in statement, it would look like this code:

for ( var key in colors ) {

console . log ( colors [ key ] + "\n" ); }

In many cases, this for…in statement would work. But in Node.js, properties can be arbitrarily added to any object, including arrays. The regular for statement is explicit about looping through the numbered indexes but the for…in statement only assumes that there are no other properties except the indexes. It is safer to be explicit; that is, it is better to be safe than sorry.

For associative arrays in PHP, it is recommended that PHP foreach statements be re‐ factored to always use the form where both a key and value are provided. Let us assume that the following PHP code has an associative colors array variable where 0 is the value associated with the red key, 128 is the value associated with the blue key, and 255 is the value associated with the green key:

foreach ( $colors as $value ) {

print $value . "\n" ; }

The preceding PHP code may rely on the keys being inserted in a certain order: red, then blue, and, finally, green. Here’s output of the PHP foreach statement:

The PHP code above should be refactored to add the $key variable to the foreach statement:

foreach ( $colors as $key => $value ) {

print $value . "\n" ; }

The find-and-replace action will be run on the .php file, not the .njs file. This action refactors PHP and is not a conversion from PHP to Node.js:

Operation: "Find/Replace" in Eclipse PDT Find: as Replace: as $key => Options: None Action: Find, then Replace/Find

Syntax Differences | 115

At each occurrence, the foreach statement will be examined to see if it already uses the => syntax or not. If it already uses it, it will be skipped. If it does not use it, it will be added, possibly replacing the $key variable with a more appropriately named variable.

Refactoring the PHP code to add a $key variable does nothing for the PHP, but when converted to Node.js, the Node.js code can create the value variable from the array variable and the current key, as shown here:

for ( var key in colors ) { var value = colors [ key ]; console . log ( value + "\n" ); }

To convert a PHP foreach statement into a Node.js for…in statement, the following find-and-replace action will be used with two manual steps executed at each occurrence:

Operation: "Find/Replace" in Eclipse PDT Find: foreach ( Replace: for ( Options: None Action: Find, then Replace/Find

At each occurrence, the PHP $colors as $key => $value will need to be manually converted to the Node.js var key in colors. It is up to the developer doing the con‐ version to execute this first step, although a clever developer might come up with a regular expression that would accomplish the same thing.

The second manual step needed at each occurrence will be to add an extra statement at the beginning of the Node.js for…in statement similar to var value = colors[key];. The name of the newly defined variable and the name of the array are dictated by the names given in the original PHP foreach statement.

With the $key variable added to the PHP code, the visual diff feature or program will more easily match the $key variable when comparing the PHP and Node.js codebases, and by doing so, it is easier to visually confirm that both codebases are correct. If you really want to make the visual diff feature or program match up nicely, the Node.js statement that assigns the value variable can be converted back to the corresponding PHP foreach statement:

foreach ( $colors as $key => $value ) { $value = $colors [ $key ]; // redundant but makes "visual diff" very happy print $value . "\n" ; }

The preceding PHP improvement is not optional, but will make the PHP and Node.js codebases more similar and easier to compare. The improvement can be made by exe‐ cuting the following find-and-replace action:

116 | Chapter 6: Syntax

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

At each occurrence, the action is never executed. Instead, an extra statement of the form $value = $colors[$key]; is inserted at the beginning of the loop, changing the variable names to those used in the foreach statement.