104 | Chapter 5: HTTP Responses 104 | Chapter 5: HTTP Responses

104 | Chapter 5: HTTP Responses 104 | Chapter 5: HTTP Responses

===

104 | Chapter 5: HTTP Responses 104 | Chapter 5: HTTP Responses

be properly sent by the end() function and control can be returned to the Node.js server. But that’s not enough. Both PHP and Node.js are programming languages. Although

the PHP syntax has some similarities to the Node.js syntax, any PHP to Node.js con‐ version will need to convert from one syntax to the other.

In the next chapter, the differences between the PHP syntax and the Node.js syntax will

be explained and conversion recipes will be provided to convert between them.

A PHP Example Page | 105

CHAPTER 6

Syntax

By now, the Node.js server is running and routing HTTP requests to the correct handler function, the page() function, in the correct Node.js local module for that page request. The local modules contain a simple framework based on the exports.serve() function that is correct Node.js code but the page() function itself mostly contains PHP code with a few Node.js modifications mixed in. When a client requests a page, the Node.js server probably stops with a stack trace.

The good news is that the Node.js server infrastructure is done. From here on out, all our conversion efforts are focused on making transformations to the hybrid PHP/ Node.js code that constitutes the page itself.

Initially, the hybrid code will be mostly PHP with a little bit of Node.js, but as the con‐ version techniques from this chapter and the remaining chapters are applied, the balance in hybrid code will shift from being mostly PHP to being a balance of PHP and Node.js, then being mostly Node.js with a little bit of PHP, and finally, being fully working pure Node.js code.

In this chapter, we will deal with syntax, the “standard parts” of a language. The syntax of a language consists of the keywords and symbols used to create different statements. For example, in Node.js and every other computer language ever invented, the plus (+) operator is the syntax used to add two numbers together; in the code fragment 4+5, the plus (+) operator is a piece of syntax. In contrast, the semantics of a language is the knowledge that the plus (+) operator means “add these two numbers together”; in the 4+5 example, the semantics indicate that the answer is 9. For our purposes, we will not quibble over syntax versus semantics. This chapter will show how to convert some of the PHP syntax to Node.js syntax while keeping an eye on semantic differences so that the converted code will function the same way as the original code did.

Fortunately, PHP and Node.js have a lot of syntax and semantics in common. We already know from Chapter 3 that both PHP and Node.js have common roots in the C language, and even where they depart from the C language, they have often departed in the same way.

Both PHP and Node.js use the if and else keywords. In both languages, if and else statements have the same syntax and the same semantics. Both PHP and Node.js have the switch, case, and break keywords, and they have the same punctuation and func‐ tion in the same way in each language. They both have for statements, and in both languages, the for statements have an initialization clause, a conditional clause, and an increment clause, separated by semicolons (;). They both have while and do…while statements that operate the same. These control statements are the same for many other languages, including the C language.

Statements in both languages end in semicolons (;). Statement blocks in both languages use curly brackets ( { and } ). It is the same in the C language. Here is a PHP example of an if statement with a statement block (which, of course, uses curly brackets):

if ( $var1 == 1 ) { $var1 += 10 ; $var1 += 12 ;

The Node.js example is almost the same, except that variables in PHP are required to start with a dollar sign ($), whereas in Node.js, they do not:

if ( var1 == 1 ) { var1 += 10 ; var1 += 12 ;

PHP and Node.js use the same set operators. To access a value at a particular index (also known as a “key”), the square brackets notation ( [ and ] ) can be applied to PHP arrays, Node.js arrays, and Node.js objects. Parentheses ( ( and ) ) are used to define precedence. Plus (+) for addition, minus (-) for subtraction, asterisk (*) for multiplication, slash (/) for division, and percent sign (%) for remainder work the same way in both languages. The double plus (++) increment and double minus (--) decrement operators work the same in both languages.

Both languages use the same conditional operators: greater than (>), less than (<), greater than or equal (>=), and less than or equal (<=) are the same. The equals (==) and the does not equal (!=) operators work the same. Even the triple equals (===) and triple does not equal (!==) work the same. In both languages, the triple versions compare both the type and the value, while the regular equals (==) and does not equal (!=) compare the values, possibly coercing the values to be the same type in order to do the comparison.

108 | Chapter 6: Syntax

Both PHP and Node.js support the inline conditional, using the question mark (?) and colon (:), in the same way.

Functions in both languages use the function keyword. However, the C language does not have the function keyword. Fortunately, despite the fact that they do not share the same syntax as the C language, both PHP uses the same syntax and the same semantics for functions as Node.js does. All three languages use the return statement in the same way for the same purpose. Here’s a simple PHP function with a return statement:

function add ( $a , $b ) {

return $a + $b ; }

The corresponding Node.js function is the same, except for the dollar sign ($) discrep‐ ancy mentioned earlier:

function add ( a , b ) { return a + b ; }

Function calling is as similar as function definition. In both languages, functions are called by name, arguments are enclosed in parentheses ( ( and ) ) and separated from each other by commas (,). The following example function call works in both PHP and Node.js:

add ( 4 , 5 );

All these similarities between PHP and Node.js are shown to demonstrate just how similar the two languages are in terms of syntax and semantics. Converting from PHP to Node.js is not as enormous a job as might be initially imagined. Now, let us turn to the relatively few syntactic or semantic differences between the two languages.