-
include
Posted on April 29th, 2009 No commentsThe include() statement includes and evaluates a specified file. The difference between include() and require() is that require() results in a Fatal Error upon failure, whereas include() does not, it only produces a Warning.
include() is not a function in PHP, it is a language construct.
PHP
include('../config.php');
Ruby
include('../config.rb');
-
require
Posted on April 26th, 2009 No commentsThe require() statement includes and evaluates a specified file. The difference between require() and include() is that require() results in a Fatal Error upon failure, whereas include() does not, it only produces a Warning.
require() is not a function in PHP, it is a language construct.
PHP
require ('../config.php');
Ruby
require ('../config.rb');
-
printf
Posted on April 23rd, 2009 1 commentOutput a formatted string.
PHP
$format = 'There are %d monkeys in the %s'; printf($format, 5, 'tree'); // => There are 5 monkeys in the tree
Ruby
my_string = 'There are %d monkeys in the %s'; printf(my_string, 5, 'tree'); # => There are 5 monkeys in the tree
-
print
Posted on April 20th, 2009 No commentsOutput a string.
Strictly speaking, print() is not a function in PHP, it is a language construct (similar to echo() ).
PHP
print 'Hello World!'; // => Hello World!
Ruby
print 'Hello World!' # => Hello World!
-
echo
Posted on April 17th, 2009 2 commentsOutput one or more strings.
Strictly speaking, echo() is not a function in PHP, it is a language construct.
PHP
echo 'Hello World!'; // => Hello World!
Ruby
print 'Hello World!' # => Hello World!
-
sort
Posted on April 14th, 2009 No commentsSorts an array with elements arranged from lowest to highest.
PHP
$a = array('a', 'b', 'e', 'g', 'c', 'd'); sort($a); print_r($a); /* Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => g ) */
Ruby
a = [ "d", "a", "e", "c", "b" ]; puts a.sort; # => ["a", "b", "c", "d", "e"]
-
count
Posted on April 11th, 2009 No commentsReturns the number of elements in an array.
PHP
$a = array('first' => 1, 'second' => 2); echo count($a); // => 2
Ruby
my_array = [1, 2]; puts my_array.length; # => 2
-
array_key_exists
Posted on April 8th, 2009 No commentsReturns TRUE if the given argument is set in the array. The argument passed in can be any value possible for an array index.
PHP
$a = array('first' => 1, 'second' => 2); var_dump( array_key_exists('first', $a) ); // => true
Ruby
animals = {:sheep => 1, :cow => 2}; puts animals.include?(:cow); # => true
-
array_sum
Posted on April 5th, 2009 2 commentsReturns the sum of values in an array.
PHP
$a = array(2, 4, 6, 8); echo array_sum($a); // => 20
Ruby
a_var = [2, 4, 6, 8]; puts a_var.inject {|sum,x| sum ? sum + x : x }; # => 20
In the code above, the return value from the ruby code will be nil if the array used is empty. This is not exactly the same behaviour as PHP, since the array_sum() function will always return a number. We can force ruby to always return a float (or an int) too, even if the array is empty, by adding a bit of type casting to the final result:
Ruby
a_var = []; puts a_var.inject {|sum,x| sum ? sum + x : x }.to_f; # => 0.0
-
pi
Posted on April 2nd, 2009 No commentsReturns the value of PI to 14 (default) decimal places. This precision can be altered in the php.ini file.
PHP
echo pi(); // => 3.1415926535898
Ruby
puts Math::PI; # => 3.14159265358979
In the code above, PI is actually a constant of the Module Math. PHP also has a constant which stores the approximate value of PI - M_PI.
PHP
echo M_PI(); // => 3.1415926535898


