__ ◆ 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
【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')
[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
[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"
【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
【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
【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
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')