PHP Alternative Syntax

PHP Alternative Syntax

One final conversion issue remains: the PHP alternative syntax for branching and loop‐ ing. The use of the PHP alternative syntax varies a lot: in some PHP, it will be nearly everywhere, and in other PHP code, it will never be used. Node.js does not have an alternative syntax, so if the PHP alternative syntax is used, it needs to be converted into the normal syntax for Node.js.

This is the normal syntax for an if…elseif…else statement:

if ( $a == $b ) { print 'a is equal to b' ; } elseif ( $a > $b ) { print 'a is greater than b' ; } else { print 'a is less than b' ; }

The alternative syntax for the same if…elseif…else statement removes all the right curly brackets ( } ) except for the last one and replaces the left curly bracket ( { ) with a colon (:). The last right curly bracket ( } ) is replaced with the endif keyword followed by a semicolon. Here’s the alternative syntax of this example:

if ( $a == $b ) : print 'a is equal to b' ; elseif ( $a > $b ) : print 'a is greater than b' ; else : print 'a is less than b' ; endif ;

The alternative syntax can either be refactored into the normal syntax in the PHP code or only converted in the Node.js code (that is, the hybrid PHP/Node.js code in the page() function of the .njs file).

PHP Alternative Syntax | 117

For both refactoring and converting, changing the if statement alternative syntax into the normal syntax takes four find-and-replace actions. In both cases—refactoring and converting—the find-and-replace actions are the same. To convert the if statement alternative syntax into the normal syntax, we’ll need to execute four find-and-replace actions, starting with this one:

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

At each occurrence, the replacement is done only if it is an if statement or an elseif statement using the alternative syntax. The elseif statement may still be using the elseif keyword, or if it has already been converted to Node.js, will use the Node.js else if statement containing whitespace.

An inline conditional is not alternative syntax. An inline conditional is an if and else statement in an abbreviated syntax using a question mark (?) and a colon (:) as part of

a variable assignment. The example below shows:

$msg = ( $a == $b ) ? 'a is equal to b' : 'a is not equal to b' ;

A right parenthesis ( ) ) might be inserted as part of the “equals” value and match the find-and-replace action. So here’s an example of that happening:

$msg = ( $a == $b ) ? ( 'a is equal' + ' to b' ) : 'a is not equal to b' ;

In both PHP and Node.js, the inline conditional syntax is the same and no conversion is needed. The find-and-replace action should not replace this instance.

After the find-and-replace action is executed, the preceding PHP example will be modi‐ fied as follows:

if ( $a == $b ) { print 'a is equal to b' ; elseif ( $a > $b ) { print 'a is greater than b' ; else :

print 'a is less than b' ;

endif ;

The next find-and-replace action will add a right curly bracket ( } ) to the elseif statement. If the PHP elseif statement has already been converted to the Node.js else if statement, the else if text can be substituted for both the Find and the Replace fields:

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

118 | Chapter 6: Syntax

At each occurrence, it must be confirmed that a right curly bracket ( } ) does not already exist. After the find-and-replace action is executed, the elseif statement will have a right curly bracket ( } ) before it:

if ( $a == $b ) { print 'a is equal to b' ; } elseif ( $a > $b ) { print 'a is greater than b' ; else : print 'a is less than b' ; endif ;

The third find-and-replace action can globally replace the else: text, which can only occur as part of the if statement alternative syntax with the } else { text. Since the alternative syntax is the only way that the else: text can occur, the find-and-replace action can be executed all at once, without visiting each occurrence:

Operation: "Find/Replace" in Eclipse PDT Find: else: Replace: } else { Options: None Action: Replace All

After the third find-and-replace action, the partially converted if statement will appear similar to this example:

if ( $a == $b ) { print 'a is equal to b' ; } elseif ( $a > $b ) { print 'a is greater than b' ; } else {

print 'a is less than b' ;

endif ;

The final find-and-replace action replaces the endif; text with a right curly bracket ( } ). Like the third find-and-replace action, this action can be executed all at once because the endif; text is unique to the alternative syntax:

Operation: "Find/Replace" in Eclipse PDT Find: endif; Replace: } Options: None Action: Replace All

After this final action is taken, the PHP code is now in the if statement normal syntax. The alternative syntax has been removed:

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

PHP Alternative Syntax | 119 PHP Alternative Syntax | 119

Besides the if statement alternative syntax, the for, foreach, while, and switch state‐ ments also have alternative syntaxes. The rest of this chapter will briefly describe these other alternative syntaxes and how to convert them into normal syntax. These descrip‐ tions are provided for reference. If you are reading this book cover to cover, you may safely skip the remainder of this chapter, as these alternative syntaxes are obvious and unsurprising.

PHP also has a for statement alternative syntax. Here’s a PHP example with the for statement normal syntax:

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

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

Here’s the alternative syntax for this same example:

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

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

To refactor the PHP code itself or convert the PHP code to Node.js code, the following find-and-replace action will convert the opening line from the alternative syntax to the normal syntax:

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

At each occurrence, the replacement is done only if it is a for statement using the al‐ ternative syntax.

A second find-and-replace action completes the conversion from alternative syntax to normal syntax by converting the last statement into normal syntax:

Operation: "Find/Replace" in Eclipse PDT Find: endfor; Replace: } Options: None Action: Replace All

The for statement is now in the normal syntax. PHP has a foreach statement alternative syntax. The following is a PHP example with

the foreach statement normal syntax:

120 | Chapter 6: Syntax 120 | Chapter 6: Syntax

print $value . "\n" ; }

Here’s the alternative syntax for this same example:

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

print $value . "\n" ; endforeach ;

To refactor the PHP code itself or convert the PHP code to Node.js code, the following find-and-replace action will convert the opening line from the alternative syntax to the normal syntax:

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

At each occurrence, the replacement is done only if it is a foreach statement using the alternative syntax.

A second find-and-replace action completes the conversion from alternative syntax to normal syntax by converting the last statement into normal syntax:

Operation: "Find/Replace" in Eclipse PDT Find: endforeach; Replace: } Options: None Action: Replace All

The foreach statement is now in the normal syntax. PHP has a while statement alternative syntax. Here’s a PHP example with the while

statement normal syntax:

$key = 0 ;

while ( $key < count ( $colors )) {

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

And this is the alternative syntax for the same example:

$key = 0 ; while ( $key < count ( $colors )) : print $colors [ $key ] . "\n" ; ++ $key ; endwhile ;

To refactor the PHP code itself or convert the PHP code to Node.js code, the following find-and-replace action will convert the opening line from the alternative syntax to the normal syntax:

PHP Alternative Syntax | 121

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

At each occurrence, the replacement is done only if it is a while statement using the alternative syntax.

A second find-and-replace action completes the conversion from alternative syntax to normal syntax by converting the last statement into normal syntax:

Operation: "Find/Replace" in Eclipse PDT Find: endwhile; Replace: } Options: None Action: Replace All

The while statement is now in the normal syntax. PHP has a switch statement alternative syntax. The following is a PHP example with

the switch statement normal syntax:

switch ( $color ) { case 'red' : print 'The red makes a redish hue.' ; break ; case 'green' : print 'The green makes a pale hue.' ; break ; case 'blue' : print 'The blue makes a nice tint.' ; break ; }

And here’s the alternative syntax for this same example:

switch ( $color ) : case 'red' :

print 'The red makes a redish hue.' ; break ; case 'green' : print 'The green makes a pale hue.' ; break ; case 'blue' : print 'The blue makes a nice tint.' ; break ; endswitch ;

To refactor the PHP code itself or convert the PHP code to Node.js code, the following find-and-replace action will convert the opening line from the alternative syntax to the normal syntax:

122 | Chapter 6: Syntax

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

At each occurrence, the replacement is done only if it is a switch statement using the alternative syntax.

A second find-and-replace action completes the conversion from alternative syntax to normal syntax by converting the last statement into normal syntax:

Operation: "Find/Replace" in Eclipse PDT Find: endswitch; Replace: } Options: None Action: Replace All

The switch statement is now in the normal syntax. Lucky for us, the differences between the PHP syntax and the Node.js syntax are rela‐

tively minor and easily addressed. Converting between PHP variables and Node.js vari‐ ables is more of a challenge. In the next chapter, PHP variables, like $a, and PHP array variables, like $colors, will be converted from PHP to Node.js, which will make many of our if statements, for statements, and other conditionals and loops into pure Node.js code.

PHP Alternative Syntax | 123

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). The following text shows a regular expression that describes this variable naming scheme:

[a-zA-Z][a-zA-Z_0-9]*

Although Unicode characters in both languages and dollar signs ($) in Node.js may be used in creating variable names, it is not recommended. It is encouraged that PHP and Node.js codebases limit or refactor themselves to use the more narrow variable naming specification above. Find-and-replace actions can be invented to make both PHP and Node.js codebases conform.

Besides the dollar sign ($) difference, in Node.js, every variable must be declared using

a var statement. For example, the following code declares the a variable and the colors variable:

var a = 4 ; var colors ;