DEV Community

Cover image for Learn Python, PHP, Ruby and Javascript in one Blog Post
Alex Merced
Alex Merced

Posted on

Learn Python, PHP, Ruby and Javascript in one Blog Post

Getting Started

Below we will have the patterns for all the main programming concepts in four different languages. To try them out head over the website, repl.it and create a free environment to practice in these languages.

We will follow the process you should always follow when learning a new language:

  1. Learn how to print values to the console
  2. Learn how to assign variables
  3. Conditionals
  4. Loops
  5. Collections
  6. Functions

After these building blocks, you'll be able to begin pushing yourself further and do challenges on websites like CodeWars.com.

Printing Values to the Console

You will generally interact with a programming language via your command line shell (Bash on mac/linux, cmd/powershell/gitBash on windows). So once you have any of these languages on your computer you'll write files and run them with a command. We can't see what our code is doing visually beyond occasionally printing values to the console to assure us the program is following our instructions.

Language File Extension run script command Pkg Mgr Dep List
Javascript .js node file.js npm package.json
Python .py python file.py pip requirements.txt
Ruby .rb ruby file.rb Gems Gemfile
PHP .php php file.php composer composer.json

This is why the first program you always create is, Hello World.

Javascript (using NodeJS)

console.log("Hello World")
Enter fullscreen mode Exit fullscreen mode

Ruby

puts "hello world"
Enter fullscreen mode Exit fullscreen mode

Python

print("Hello World")
Enter fullscreen mode Exit fullscreen mode

PHP

<?php

echo "hello world";
?>
Enter fullscreen mode Exit fullscreen mode

Variables

Programming is all about creating dynamic code. There are often values we may not know or may change overtime. Instead of hard coding these values and making our code more rigid we use variables to store and refer to the values in our code.

There are different types of data:

  1. Numbers - numerical data that can be used in math expressions
  2. Strings - a sequence of letters, text, and symbols usually within quotation marks
  3. Booleans - a value that can either represent true or false
  4. nothing - the absence of value called null, nil, undefined, etc.

We can store these values in little cubby holes called variables we can then use to refer to the value.

Javascript (using NodeJS)

let myString = "Hello World"
let myNumber = 5
let myBool = true

console.log(myString)
console.log(myNumber)
console.log(myBool)
Enter fullscreen mode Exit fullscreen mode

Ruby

my_string = "Hello World"
my_number = 5
my_boolean = true

puts my_string
puts my_number
puts my_boolean
Enter fullscreen mode Exit fullscreen mode

Python

my_string = "Hello World"
my_number = 5
my_boolean = True

print(my_string)
print(my_number)
print(my_boolean)
Enter fullscreen mode Exit fullscreen mode

PHP

<?php

$my_string = "Hello World";
$my_number = 5;
$my_boolean = true;

echo $my_string;
echo $my_number;
echo $my_boolean;
?>
Enter fullscreen mode Exit fullscreen mode

Conditionals

What makes a script useful isn't just being able to outline a sequence of actions but having those actions alter based on the state of the data involved. If statements allow us to ask, is this true? Then choose what our script does based on whether it is true or false.

Javascript (using NodeJS)

let number = 6

if (number > 5){
    console.log(true)
} else {
    console.log(false)
}

Enter fullscreen mode Exit fullscreen mode

Ruby

number = 6

if number > 5
    puts true
else
    puts false
end
Enter fullscreen mode Exit fullscreen mode

Python

number = 6

if (number > 5):
    print(True)
else:
    print(False)
Enter fullscreen mode Exit fullscreen mode

PHP

<?php
$number = 6

if ($number > 5){
    echo true;
} else {
    echo false;
}
?>
Enter fullscreen mode Exit fullscreen mode

Loops

What if you need to repeat a task several times, it would be very tedious to type it over and over again. For this situation, we have loops that will repeat a set of instructions as long as an expression is true and stop once it becomes false.

Javascript (using NodeJS)

let counter = 0

while (count < 10){
    console.log(count)
    count = count + 1
}
Enter fullscreen mode Exit fullscreen mode

Ruby

counter = 0

while counter < 10
    puts counter
    counter = counter + 1
end
Enter fullscreen mode Exit fullscreen mode

Python

counter = 0

while (counter < 10):
    print(counter)
    counter = counter + 1
Enter fullscreen mode Exit fullscreen mode

PHP

<?php
$counter = 0;

while($counter < 10){
    echo counter;
    $counter = $counter + 1
}
?>
Enter fullscreen mode Exit fullscreen mode

Collections

A Collection is a data structure in a language that can hold multiple values. They generally fall into one of two categories.

  • An ordered list of values accessibly by a numerical index starting at 0
  • A list of values access by a "key" which is usually a string/symbol
Language Using Numerical Index Using Keys
Javascript Arrays Objects
Ruby Arrays Hashes
Python List Dictionaries
PHP Arrays Associative Arrays

Javascript (using NodeJS)

let myArray = ["Alex Merced", 35]
console.log(myArray[0])
console.log(myArray[1])

let myObject = {name: "Alex Merced", age: 35}
console.log(myObject.name)
console.log(myObject.age)
Enter fullscreen mode Exit fullscreen mode

Ruby

my_array = ["Alex Merced", 35]
puts my_array[0]
puts my_array[1]

my_hash = {name: "Alex Merced", age: 35}
puts my_hash[:name]
puts my_hash[:age]
Enter fullscreen mode Exit fullscreen mode

Python

my_list = ["Alex Merced", 35]
print(my_list[0])
print(my_list[1])

my_dictionary = {"name": "Alex Merced", "age": 35}
print(my_dictionary["name"])
print(my_dictionary["age"])
Enter fullscreen mode Exit fullscreen mode

PHP

<?php
$my_array = ["Alex Merced", 35];
echo $my_array[0];
echo $my_array[1];

$my_associative = ["name" => "Alex Merced", "age" => 35];
echo $my_associative["name"];
echo $my_associative["age"];
?>
Enter fullscreen mode Exit fullscreen mode

Functions

Functions are like magical spells, they do a specified thing and be used anytime and as many times as you want. Information can be given to functions in the form of arguments, and those arguments are stored in variables called parameters. Functions are key to doing powerful things in programming.

Javascript (using NodeJS)

// x is parameter variable that will receive the first argument
function addOne(x){
    //the return value is what the function gives back when its done
    return x + 1
}

//Several invocations of the function passing different arguments
const result1 = addOne(1)
console.log(result1)

const result2 = addOne(2)
console.log(result2)
Enter fullscreen mode Exit fullscreen mode

Ruby

def addOne x
    return x + 1
end

result1 = addOne 1
puts result1

result2 = addOne 2
puts result2
Enter fullscreen mode Exit fullscreen mode

Python

def addOne(x):
    return x + 1

result1 = addOne(1)
print(result1)

result2 = addOne(2)
print(result2)
Enter fullscreen mode Exit fullscreen mode

PHP

<?php
function addOne(x){
    return x + 1;
}

$result1 = addOne(1);
echo $result1;

$result2 = addOne(2);
echo $result2;

?>
Enter fullscreen mode Exit fullscreen mode

Conclusion

Hopefully, this gives you a nice starting place in any of these languages. Here are some topics to look up and try out as you grow your ability in these languages.

  • string manipulation (concatenation, interpolation, etc.)
  • array/list manipulation (adding, removing, finding items)
  • classes and object-oriented programing

Then once you get really comfortable with any of the languages try building a web application with a web framework. These come in minimalist and opinionated flavors. Below is a table showing you the main frameworks for each language.

Language Minimalist Web Framework Opinionated Web Framework
Python Flask, FastAPI Django, Masonite
Ruby Sinatra Ruby on Rails
PHP Slim Laravel, Symphony
Javascript Express, Koa, Fastify FoalTS, NestJS, Sails

Top comments (5)

Collapse
 
darkain profile image
Vincent Milum Jr

Just a tip: for PHP, you dont need the ?> at the end. This actually shifts context from PHP to TEXT, and will pass any text after that to the console/browser. This can be especially bad for editor that send new-line characters, but you were using PHP to output binary data (such as reading an image file and sending to the browser)

Collapse
 
ayabouchiha profile image
Aya Bouchiha

Great article

Collapse
 
nfcristea profile image
Florin Cristea

It would have been nice to cover classes too 😀

Collapse
 
saranjoel profile image
SaranJoel

Good explanation .
It wolud be nice if you have elaborated on functions a little.

Collapse
 
clerijr profile image
Clerivaldo Junior

Hands down one of the best articles i read about characteristics between languages, thanks a lot!