Programming Languages war Python, PHP, Javascript (Nodejs) "Define Variables, functions and loops"
Hey my friends, a new post about this war. I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!
We're going to figure out how define variable functions and loops. I'm going to do a little script with all examples and a little more. =D
Variables
It's a space of memory where you could save information what you need manipulate. These three language don't need to define the type of variable in front the name. It's important to say that exist boolean, numbers, strings, array.
Python
To define a variable, you just need give it a name
greeting = "hi"
name = "dev"
print(f"{greeting}, {name}")
greeting = "hey"
print(f"{greeting}, {name}")
PHP
To define a Variable, you need to use the character "$" in front the name of the variable
<?php
$greeting = "hi";
$name = "dev";
echo "$greeting, $name";
$greeting = "hey";
echo "$greeting, $name";
?>
Javascript
To Define a Variable, you need to use var, let or const, "var" you can use in any part of your code, and you can change at any moment, "let" you can use only where it was defined, and you can change it, "const" you can use only where it was defined, and you can't change it.
var greeting = "hi"
let name = "dev"
const world = "Hello, world"
console.log(`${greeting}, ${name}`)
greeting = "hey"
console.log(`${greeting}, ${name}`)
console.log(world)
Functions
The functions are ways to enclosure some lines of code for reuse in another moment.
Python
You need to use "def" in front the name of function, and between parentheses, you could add any parameters what you want, here it's important colon and space in start code into functions. When you leave to use the space in front the code, it's the end of the function. To run the function, you only write the name and between parentheses send all parameters that you defined before.
def greeting(name):
print(f"Hey, {name}")
greeting("dev")
PHP
You need to use "function" in front the name of the function, and between parentheses, you could add any parameters what you want, here it's important the braces for defining the start and finished. To run the function, you only write the name and between parentheses send all parameters that you defined before.
<?php
function greeting($name) {
echo "Hey, $name";
}
greeting("dev");
?>
Javascript
You need to use "function" in front the name of the function, and between parentheses, you could add any parameters what you want, here it's important the braces for defining the start and finished. To run the function, you only write the name and between parentheses send all parameters that you defined before.
function greeting(name) {
console.log(`Hey, ${name}`)
}
greeting("dev")
If you don't define any parameter, you have to run the function with parentheses but empty ex: nameFunction()
Loops
Loops are ways that you can execute some instructions as many times as you want. In this case I'm going to talk about "FOR", you will see that it's like the before cases, in python is important the space in front the code, PHP and Javascript are the same with braces.
Python
for index in range(6):
print(index)
PHP
<?php
for ($index=0; $index < 6; $index++) {
echo $index;
}
?>
Javascript
for (let index = 0; index < 6; index++) {
console.log(index)
}
Final example
Now we're going to make an example with the whole content in this post in the three languages and we're going to see maybe who is better. We're going to greet all cast of One Piece. ;)
Python
def greeting(name):
print(f"Hey, {name}")
cast = ["Luffy", "Nami", "Zoro", "Sanji", "Usopp", "Chopper", "Robin", "Franky", "Brook", "Jinbe"]
for name in cast:
greeting(name)
PHP
<?php
function greeting($name) {
echo "Hey, $name";
}
$cast = ["Luffy", "Nami", "Zoro", "Sanji", "Usopp", "Chopper", "Robin", "Franky", "Brook", "Jinbe"];
for ($index = 0; $index < count($cast); $index++) {
greeting($cast[$index]);
echo "\n";
}
?>
Javascript
function greeting(name) {
console.log(`Hey, ${name}`)
}
const cast = ["Luffy", "Nami", "Zoro", "Sanji", "Usopp", "Chopper", "Robin", "Franky", "Brook", "Jinbe"]
for (let index = 0; index < cast.length; index++) {
greeting(cast[index])
}
I created a greeting function, next I define a variable of arrays, then I create a loop of array of greeting the crew.
Conclusion
Structure: they are very similar, change a little for the braces
Lines: Python: 9, PHP: 13, Javascript: 9
Time to execution: For this test I add another "for" with 60000 iterations, and I ran 3 times. Python: 5.937s, PHP: 5.843s, Javascript: 2.214s
Then this time Javascript was faster than PHP or Phyton.
Would you like that I write about the type of variables and his methods?
I hope you enjoy my post and remember that I am just a Dev like you!