🔁 The Same Program in Three Languages
The fastest way to feel the difference between Node.js, Python, and PHP is to watch them solve the same tiny problems side by side. In this Rosetta-stone lesson you'll see variables, strings, conditionals, loops, functions, and list transforms — three ways each — and start recognizing that the ideas transfer even when the punctuation doesn't.
🎯 Learning Objectives
By the end of this lesson, you will be able to:
- Declare variables and basic types in Node.js, Python, and PHP
- Format strings using each language's interpolation style
- Write conditionals, loops, and functions in all three
- Transform a list/array using each language's idiomatic approach
- Read a syntax cheat sheet to translate a snippet from one language to another
Estimated Time: 30–40 minutes • Difficulty: Beginner–Intermediate
Hands-on: Port a small "grades" program across all three languages.
In This Lesson
Why a Rosetta Stone?
The famous Rosetta Stone carried the same decree in three scripts, which is exactly what let scholars decode Egyptian hieroglyphs. We'll use the same trick: by holding the problem constant and varying only the language, the shared structure jumps out and the surface differences stop being scary.
💡 Read the columns, then the rows. First read one language top-to-bottom to see it as a whole. Then compare the same task across all three. You'll notice the concepts (a variable, a loop, a function) are identical — only the spelling changes.
📖 A note on running these
Node runs with node file.js, Python with python file.py, and PHP with php file.php from the command line. You don't need a server for any snippet in this lesson — they all print to the terminal.
Variables & Types
All three languages are dynamically typed — you don't declare a type up front; the value carries its type. The visible differences are the keyword (or sigil) and how a statement ends.
Node.js
const name = "Ada"; // string, cannot be reassigned
let age = 36; // number, can change
let isMember = true; // boolean
const scores = [90, 85]; // array
// Statements end with a semicolon (optional but conventional)
Python
name = "Ada" # string
age = 36 # int
is_member = True # bool (note the capital T)
scores = [90, 85] # list
# No keyword, no semicolon; snake_case is the convention
PHP
<?php
$name = "Ada"; // every variable starts with $
$age = 36; // int
$isMember = true; // bool
$scores = [90, 85]; // array
// Semicolons are required
💡 Spot the difference
Node uses const/let; Python uses a bare name; PHP prefixes every variable with $. Python and PHP booleans differ in casing (True vs true). Semicolons are conventional in Node, absent in Python, and required in PHP.
String Formatting & Printing
Building a message out of variables — "string interpolation" — is something you'll do constantly. Each language has a clean, modern way to do it.
Node.js — template literals
const name = "Ada";
const age = 36;
console.log(`${name} is ${age} years old.`);
// Uses backticks and ${ } placeholders
Python — f-strings
name = "Ada"
age = 36
print(f"{name} is {age} years old.")
# Prefix the string with f and use { } placeholders
PHP — double-quoted interpolation
<?php
$name = "Ada";
$age = 36;
echo "$name is $age years old.\n";
// Double-quoted strings expand $variables directly
All three print:
Ada is 36 years old.
Printing itself differs: Node uses console.log(), Python uses print(), and PHP uses echo (and you add \n yourself, since echo doesn't add a newline).
Conditionals
Same logic, three flavors of braces and colons. Watch how Python replaces braces with indentation.
Node.js
const score = 82;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else {
console.log("C or below");
}
Python
score = 82
if score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("C or below")
PHP
<?php
$score = 82;
if ($score >= 90) {
echo "A\n";
} elseif ($score >= 80) {
echo "B\n";
} else {
echo "C or below\n";
}
⚠️ Three words for "else if"
Node writes else if (two words), Python writes elif, and PHP writes elseif (one word). A small trap when translating between them.
Loops
Let's sum the numbers 1 through 5. Each language offers a range-style loop, though the exact mechanics differ.
Node.js
let total = 0;
for (let i = 1; i <= 5; i++) {
total += i;
}
console.log(total); // 15
Python
total = 0
for i in range(1, 6): # 1..5 (stop is exclusive)
total += i
print(total) # 15
PHP
<?php
$total = 0;
for ($i = 1; $i <= 5; $i++) {
$total += $i;
}
echo $total . "\n"; // 15
💡 One gotcha to remember
Python's range(1, 6) stops before 6, so it produces 1–5. Node and PHP use the classic C-style three-part for loop with an explicit <= 5 condition. PHP also joins strings with . (a dot), not +.
Functions
A reusable function that greets someone shows off each language's declaration keyword and return syntax.
Node.js
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Ada"));
// Arrow-function equivalent:
const greet2 = (name) => `Hello, ${name}!`;
Python
def greet(name):
return f"Hello, {name}!"
print(greet("Ada"))
PHP
<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("Ada") . "\n";
All three print:
Hello, Ada!
The keyword differs — function (Node), def (Python), function (PHP) — but the shape is the same: name it, take parameters, return a value.
Transforming a List
A very common real task: take a list of numbers and produce a new list with each one doubled. This is where idiomatic style diverges the most.
Node.js — .map()
const nums = [1, 2, 3, 4];
const doubled = nums.map((n) => n * 2);
console.log(doubled); // [ 2, 4, 6, 8 ]
Python — list comprehension
nums = [1, 2, 3, 4]
doubled = [n * 2 for n in nums]
print(doubled) # [2, 4, 6, 8]
PHP — array_map()
<?php
$nums = [1, 2, 3, 4];
$doubled = array_map(fn($n) => $n * 2, $nums);
print_r($doubled); // Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )
Node reaches for a .map() method on the array, Python uses a compact list comprehension, and PHP calls the standalone array_map() function with an arrow function (fn). Same result, three cultures.
Syntax Cheat Sheet
Keep this table handy — it's a quick translator when you jump between languages.
| Task | Node.js | Python | PHP |
|---|---|---|---|
| Declare a variable | let x = 1; |
x = 1 |
$x = 1; |
| String interpolation | `Hi ${x}` |
f"Hi {x}" |
"Hi $x" |
console.log(x) |
print(x) |
echo $x; |
|
| Else-if keyword | else if |
elif |
elseif |
| Boolean true | true |
True |
true |
| Define a function | function f() {} |
def f(): |
function f() {} |
| Block delimiter | curly braces { } |
indentation + : |
curly braces { } |
| String concatenation | a + b |
a + b |
a . b |
| Map over a list | arr.map(fn) |
[fn(x) for x in arr] |
array_map(fn, arr) |
✅ The big pattern
Notice how concepts line up perfectly across columns even when the syntax doesn't. Once you know a concept in one language, learning it in the next is a matter of swapping punctuation — not relearning how to think.
Hands-on Exercise
🏋️ Port a "Grades" Program
Objective: Write the same small program in all three languages to lock in the syntax differences.
The program should:
- Start with a list of scores:
[92, 74, 88, 60, 100]. - Compute the average.
- Print a formatted line:
Average: 82.8. - Loop the list and print
PASSfor each score ≥ 70, otherwiseFAIL.
Write it in Node.js, then translate it to Python, then to PHP — using the cheat sheet, not by looking anything else up.
💡 Hint
For the average: sum the list and divide by its length. In Node use arr.reduce((a, b) => a + b, 0) / arr.length; in Python use sum(arr) / len(arr); in PHP use array_sum($arr) / count($arr).
✅ Solution (Python version)
scores = [92, 74, 88, 60, 100]
average = sum(scores) / len(scores)
print(f"Average: {average}")
for s in scores:
print("PASS" if s >= 70 else "FAIL")
The Node and PHP versions follow the same shape — swap in console.log/echo, the correct loop syntax, and the sum/length helpers from the hint.
🎯 Quick Quiz
Question 1: Which language replaces curly braces with indentation to mark a block of code?
Question 2: In PHP, how do you join two strings together?
Question 3: Python's range(1, 6) produces which numbers?
Summary & Quiz
🎉 Key Takeaways
- All three languages are dynamically typed; the visible differences are keywords, sigils, and block style.
- Interpolation: Node uses
`${...}`, Python usesf"{...}", PHP expands$varsin double quotes. - Blocks: Node and PHP use braces; Python uses indentation and a colon.
- List transforms: Node
.map(), Python comprehensions, PHParray_map()— same idea, three idioms. - Learning a concept once means you mostly just re-spell it in the next language.
📚 Further Reading
🚀 What's Next?
You've seen how the three languages look. Next we go under the hood: how each one actually runs on the server — Node's event loop, Python's WSGI/ASGI and the GIL, and PHP's process-per-request model — and what that means for performance.
🎉 Nice work!
Three languages no longer look like three alien worlds. Let's see how they execute.