[Basic grammar] Differences between Ruby / Python / PHP

Introduction

A summary of personal learning! I learned the basic grammar of 3 languages, so I summarized it!

1. Output

Ruby


   puts "Hello, world"

Python


   print("Hello, world")

PHP


   echo "Hello, world";

2. Variables

Ruby


   variable = "variable"

Python


   variable = "variable"

PHP


  $variable = "variable";

3. Output of property value

Ruby


   # attr_accessor :Define getters by name etc.
   puts person.name

Python


   # def __init__Define getters with etc.
   print(person.name)

PHP


  echo $person->name();

4. If statement

Ruby


   if number % 15 == 0
     puts "FizzBuzz" 
   elsif number % 3 == 0
     puts "Fizz"
   elsif number % 5 == 0
     puts "Buzz"
   else 
     puts number
   end  

Python


   if number % 15 == 0:
     print("FizzBuzz") 
   elif number % 3 == 0:
     print("Fizz")
   elif number % 5 == 0:
     print("Buzz")
   else:
     print(number)  

PHP


  if (number % 15 == 0){
     echo "FizzBuzz";
   } elseif (number % 3 == 0){
     echo "Fizz";
   } elseif (number % 5 == 0){
     echo "Buzz";
   } else {
     echo $number;
   }

5. Array / hash edition

Ruby


   #Array
   array = ["Red", "Blue", "yellow"]
   #hash
   hash = {"Osaka": "Osaka", "Aichi prefecture": "Nagoya", "Tokyo": "Tokyo?"}

Python


   #list
   lists = ["Red", "Blue", "yellow"]
   #dictionary
   dict = {"Osaka": "Osaka", "Aichi prefecture": "Nagoya", "Tokyo": "Tokyo?"}

PHP


    //Array
   $array = ["Red", "Blue", "yellow"];
   //Associative array
   $associative_array = [
           "Osaka" => "Osaka",
           "Aichi prefecture" => "Nagoya",
           "Tokyo" => "Tokyo?"
   ];

6. Repeat syntax

6-1. Loop processing

Ruby


   i = 1
   while i <= 100 do
     #processing
     i += 1
   end

Python


   i = 1
   while i <= 100:
    #processing
    i += 1

   for i in range(1, 101):
    #processing

PHP


  for ($i = 1;$i <= 100;$i++){
  //processing
  }

6-2. Iterative processing (?)

Ruby


   array.each do |value|
     #processing
   end

   hash.each {|key, value|
    #processing
   }

Python


  for value in lists:
    #processing

  for key in dict:
     #processing

PHP


  foreach($array as $value){
    //processing
  }

  foreach($associativeArray as $key => $value){
    //processing
  }

7. Function definition

Ruby


  def hello
   #processing
  end   

Python


   def hello():
     #processing
     return #Return value

PHP


  function hello() {
    //processing
    return //Return value
  }

8. Classes and instances

Ruby


   class Person
     @@number = 0

     def self.classmethod
      #processing
     end
     
     def initialize(name)
        @name = name
     end

     person = Person.new("Yamada")
   end   

Python


  class Person:
     number = 0

     @classmethod
     def classmethod(cls):
       #processing

     def __init__(self, name):
       self.name = name
     end
     
     person = Person("Yamada")
   end   

PHP


  class Person {
    private static $number;
    private $name;
 
    public function static function classmethod(){
      echo self::$number;
    }

    public function __construct($name){
      $this->name = $name;
    }
    $person = new Person();
  }

9. Getters and setters

Ruby


   def setName(name)
     @name = name
   end
 
   def getName
    @name
   end
   #Or
   attr_accessor :name

Python


   def __init__(self, name):
        self._name = name

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, name):
        self.__name = name

PHP


  public function setName($name) {
    $this->name = $name;
  }

  public function getName() {
    return $this->name;
  }

10. Finally

It feels like there is a personality in how (end / indent / {}) one block is specified! Thank you for reading! : relaxed:

Recommended Posts

[Basic grammar] Differences between Ruby / Python / PHP
Differences between Ruby and Python (basic syntax)
Python3 basic grammar
Differences between Ruby and Python in scope
Python basic grammar (miscellaneous)
Python basic grammar note (4)
Python basic grammar note (3)
Python basic grammar memo
Basic Python 3 grammar (some Python iterations)
Differences in string processing between Python, Ruby, JS, PHP (combination and variable expansion)
Java VS PHP VS Python VS Ruby
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
About Perl, Python, PHP, Ruby
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
Five languages basic grammar comparison (C #, Java, Python, Ruby, Kotlin)
Difference between Ruby and Python split
Java and Python basic grammar comparison
Dynamic proxy with python, ruby, PHP
Differences between Python, stftime and strptime
Basic grammar of Python3 system (dictionary)
Differences between Python, read (), readline (), readlines ()
Summary of differences between Python and PHP (comparison table of main items)
Differences in authenticity between Python and JavaScript
Realize PHP / Python generator with Golang / Ruby
PHP / Python / Ruby sample hitting Path API
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
[Python] I personally summarized the basic grammar.
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
Differences in multithreading between Python and Jython
Basic grammar of Python3 system (included notation)
RF Python Basic_01
Instant method grammar for Python and Ruby (studying)
python grammar check
I tried using mecab with python2.7, ruby2.3, php7
Adjust font differences between Qt for Python OS
VBA user tried using Python / R: basic grammar
Python grammar notes
RF Python Basic_02
Grouping combination in Python / Ruby / PHP / Golang (Go)
Comparison of Python and Ruby (Environment / Grammar / Literal)
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
[Ruby vs Python] Benchmark comparison between Rails and Flask
Ruby expert learned the basic grammar of Go language
[For beginners] Learn basic Python grammar for free in 5 hours!
Handle prime numbers in Python / Ruby / PHP / Golang (Go)
Interprocess communication between Ruby and Python (POSIX message queue)
python, php, ruby How to convert decimal numbers to n-ary numbers
Let's write Python, Ruby, PHP, Java, JavaScript side respectively
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
How to handle JSON in Ruby, Python, JavaScript, PHP
[Go] Basic grammar ① Definition
Python basic course (12 functions)
Python I'm also basic