[Comparison of PHP, Ruby, and Python description] For those who are wondering how the description method is different.

__ ◆ Purpose __ After learning Ruby, I started learning PHP with Progate, but the commands are slightly different ... I'm afraid (;;) ... so I was particularly interested in later studies. I summarized it for. I would like to become a "hmm" for beginners who are wondering how different Ruby and PHP are written ...?

__ ** We will continue to update it steadily __

__ ◆ Table of contents __ __1) Iterative processing (foreach/each/for in) 2) Comparison of associative array and hash

  1. Get the value from the associative array / hash
  2. Take the value and assign it to an empty array => Create a new array
  1. Class
  1. Assign a value outside the class
  2. __construct and initialize methods
  3. __construct and initialize methods ~ give arguments ~
  1. Receive as numbers and strings   __

1) Comparison of foreach and each ・ ・ ・ Iterative processing

【PHP】foreach.php


<?php //← Required in php (not described below)
$prices = array(1000, 650, 750, 800);

$totalPrice = 0
foreach ($prices as $price) {
  $totalPrice += $price;
}
echo 'The total amount is'.$totalPrice.'It's a yen';
//The following is the same result for array addition. Completed in one line.------
echo 'The total amount is'.array_sum($prices).'It's a yen';

//Output result=>The total amount is 3200 yen
?> 

【Ruby】each..do.rb


prices = [1000, 650, 750, 800]

totalPrice = 0
prices.each do |price|
  totalPrice += price
end
puts "The total amount is#{totalPrice}It's a yen"
#The following is the same result for array addition. Completed in one line.------
puts "The total amount is#{prices.sum}It's a yen"

#Output result=>The total amount is 3200 yen

【Python】for..in.py


prices = [1000, 650, 750, 800]
totalPrice = 0
for price in prices:
  totalPrice += price
print('The total amount is'+ str(totalPrice) + 'It's a yen')

2) Comparison of associative array and hash

1. Get the value from the associative array / hash

[PHP] Get the value from the associative array.php


$menu = array('name' => 'curry', 'price' => 900);

$name = $menu['name'];
$price = $menu['price'];
echo  $name.'Is'.$price.'It's a yen';
//It's hard to see, but ".Please pay attention to how to use
//Output result=>Curry is 900 yen

[Ruby] Get the value from the hash.rb


menu = {'name' => 'curry', 'price' => 900}

name = menu['name']
price =  menu['price']
puts "#{name}Is#{price}It's a yen"

#Output result=>Curry is 900 yen

2. Take out the value and assign it to an empty array => Create a new array

[PHP] Assign to an empty array (arry_push).php


$menus = array(
  array('name' => 'pizza', 'price' => 900),
  array('name' => 'pasta', 'price' => 1200),
);

$prices = array();
foreach($menus as $menus){
  $price = $menus['price'];
  array_push($test, $price);
}
$sum = array_sum($prices);
echo  'The total amount is'.$sum.'It's a yen'

[Ruby] Assign to an empty array.rb


menus = [{'name' => 'pizza', 'price' => 900},{'name' => 'pasta', 'price' => 1200}]

prices = []
menus.each do |menu| 
  price = menu['price']
  prices << price
end
sum = prices.sum
puts "The total amount is#{sum}It's a yen"

3) Class

1. Substitute a value outside the class

【PHP】.php


class Animal {
  public $name;
  public function hello() {
    echo  'I'.$this->name.'I have';
  }
}
$cat = new Menu();
$cat->name = 'Cat';
$cat->hello();

【Ruby】.rb


class Animal
  def initialize(name)
    @name = name
  end
  def changeName=(name) #Setter
    @name = name
  end
  def hello #Getter
    "I#{@name}I have"
  end
end

cat = Animal.new('')
cat.changeName = 'Cat'
puts cat.hello

2. __construct and initialize methods

【PHP】__construct method.php


class Human {
  public $name;
    public function __construct(){
      echo 'The method is called automatically when you create an instance with new';
    }
}
$naitou = new Human();
//Output result=>The method is called automatically when you create an instance with new

[Ruby] initialize method.rb


class Human
  def initialize
    puts "initialize was called"
  end
end
naitou = Human.new
#Output result=>initialize was called

3. __construct and initialize methods ~ give arguments ~

【PHP】__construct method~argument~.php


class Animal {
  // public $name; 
  public function __construct($name) {
    $this->name = $name;
  }
  public function hello() {
    echo 'I'.$this->name.'I have'.'<br>';
  }
}

$cat = new Animal('Cat');
$dog = new Animal('dog');

$cat ->hello();
$dog ->hello();

[Ruby] initialize method~argument~.rb


class Animal

  def initialize(name)
    @name = name
  end
  def hello
    puts "I#{@name}I have"
  end
end

cat = Animal.new('Cat')
dog = Animal.new('dog')

cat.hello
dog.hello

4. Receive as numbers and character strings → Calculate and output

Receive as numbers and strings+Calculation.php


$apple_price = 200;
echo 'Please enter the number of apples to buy';
//Get the entered value
$count =trim(fgets(STDIN));
$total_price = $apple_price * $count;
echo  'The total amount of apples to buy'.$total_price.'It's a yen';

Receive as numbers and strings+Calculation.rb


apple_price = 200
#Receive input using input and variable input_Substitute in count
puts "Please enter the number of apples to buy"
#Receive the entered number as a number
count = gets.to_i
total_price = (apple_price * count).to_s
puts "The total amount of apples to buy#{total_price}It's a yen"

Receive as numbers and strings+Calculation.py


apple_price = 200
#Receive input using input and variable input_Substitute in count
input_count = input("Please enter the number of apples to buy:")
#Substitute the entered number as a number"int"
count = int(input_count)
total_price = apple_price * count
#Output the total amount of numbers as letters"str"
print('The total amount of apples to buy' + str(total_price) + 'It's a yen')

Recommended Posts

[Comparison of PHP, Ruby, and Python description] For those who are wondering how the description method is different.
Tips for those who are wondering how to use is and == in Python
The answer of "1/2" is different between python2 and 3
The timing when the value of the default argument is evaluated is different between Ruby and Python.
Instant method grammar for Python and Ruby (studying)
Are Php / Ruby / Python that only runs when the file is called directly
Summary of the differences between PHP and Python
Specifying the range of ruby and python arrays
In Python, change the behavior of the method depending on how it is called
Comparison of Python and Ruby (Environment / Grammar / Literal)
The first step for those who are amateurs of statistics but want to implement machine learning models in Python
The first step of machine learning ~ For those who want to implement with python ~
Indent behavior of json.dumps is different between python2 and python3
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
ABC's A problem analysis for the past 15 times to send to those who are new to Python
For those who are in trouble because NFC is read infinitely when reading NFC with Python
Environment construction procedure for those who are not familiar with the python version control system
What kind of environment should people who are learning Python for the first time build?
For those who are having trouble drawing graphs in python
Summary of Hash (Dictionary) operation support for Ruby and Python
Comparison of how to use higher-order functions in Python 2 and 3
[Beginners are worried] Which is better, Ruby, PHP or Python?
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
I compared the speed of Hash with Topaz, Ruby and Python
How to change the log level of Azure SDK for Python
[Python] How to get the first and last days of the month
Verification of the theory that "Python and Swift are quite similar"
Python techniques for those who want to get rid of beginners
Summary of differences between Python and PHP (comparison table of main items)
Summary of sites and learning procedures that will be helpful for those who are trying to make games with pygame for the first time
Python a + = b and a = a + b are different
The story of Python and the story of NaN
What is the python underscore (_) for?
[Python] How to specify the window display position and size of matplotlib
How to write the correct shebang in Perl, Python and Ruby scripts
[Example of Python improvement] What is the recommended learning site for Python beginners?
[For beginners] Why are the "weights" and "bias" of neural networks necessary?
Find out the name of the method that called it from the method that is python
[Introduction to Python] What is the method of repeating with the continue statement?
I measured the speed of list comprehension, for and while with python2.7.
[YOLO v5] Object detection for people who are masked and those who are not
An engineer who has noticed the emo of cryptography is trying to implement it in Python and defeat it