PHP and Ruby are server-side scripting languages - most commonly used to develop web pages.

Comparison chart

PHP versus Ruby comparison chart
Edit this comparison chartPHPRuby
  • current rating is 3.72/5
  • 1
  • 2
  • 3
  • 4
  • 5
(108 ratings)
  • current rating is 3.42/5
  • 1
  • 2
  • 3
  • 4
  • 5
(89 ratings)
Block comments delimiter /* and */ =begin and =end; also any text after __END__
Statement terminators Semicolon terminated Newline terminated
Arrays dynamically sized Yes Yes
Inline comments delimiter // and # #
String concatenation operator . (dot) + (the plus sign)
Programming paradigms imperative, object-oriented, reflective imperative, object-oriented, reflective, aspect-oriented
Multi-dimensional arrays supported Yes Yes
Array element access $array[i] array[i]
Type checking Dynamic Dynamic
Syntax for string length strlen($string) string.size or string.length
Intended use Web applications, CLI Applications, scripting
Language design goals Robustness and simplicity Expressiveness, Readability
Vectorized operations Yes Yes
Classes Yes Yes
String comparison syntax strcmp($string1, $string2) string1 <=> string2
String equality testing syntax $string1 == $string2 string1 == string2
Typing Weak typing Strongly typed
OOP (Object Oriented Programming) Yes, single inheritance. Yes, but you don't have to.
String search syntax strpos($string, $substring[, startpos]) returns FALSE if search item ($substring) not found string.index(substring[,startpos]) returns nil if search item (substring) not found
String formatting syntax sprintf(formatstring, items) sprintf(formatstring, items)
Syntax for joining an array of strings implode(separator, array_of_strings) array_of_strings.join(separator)
Syntax for substring substr(string, startpos, numChars) string[startpos, numChars]
Syntax for search and replace within string str_replace($find, $replace, $string) string.gsub(find, replace)
Syntax for reversing a string strrev($string) string.reverse
Syntax for converting string to upper case strtoupper($string) string.upcase
Garbage Collection Yes Yes
Examples of Use WordPress, Wikipedia Indiegogo, Hulu

Associative Arrays in PHP vs. Ruby

PHP

PHP's built-in array type is in reality an associative array. Even when using numerical indexes, PHP internally stores it as an associative array. This is why one in PHP can have non-consecutive numerically indexed arrays. The keys have to be scalar values (string, floating point number or integer), while values can be of arbitrary types, including other arrays and objects. The arrays are heterogeneous; a single array can have keys of different types. PHP's associative arrays can be used to represent trees, lists, stacks, queues and other common data structures not built into PHP.

In PHP, an associative array can be formed in one of two ways:

$phonebook = array ();
$phonebook['Sally Smart'] = '555-9999'; 
$phonebook['John Doe'] = '555-1212'; 
$phonebook['J. Random Hacker'] = '555-1337'; 

// or 

$phonebook = array (
  'Sally Smart' => '555-9999',
  'John Doe' => '555-1212',
  'J. Random Hacker' => '555-1337',
);

// or 
$phonebook['contacts']['Sally Smart']['number'] = '555-9999'; 
$phonebook['contacts']['John Doe']['number'] = '555-1212'; 
$phonebook['contacts']['J. Random Hacker']['number'] = '555-1337';

You can also loop through an associative array in PHP as follows:

foreach ($phonebook as $name => $number) {
  echo "Number for $name: $number\n";
}
// For the last array example it is used like this 
foreach($phonebook['contacts'] as $name => $num) {
  echo "Name:{$name}"; 
  echo "Number:{$num['number']}"; 
}

PHP has an extensive set of functions to operate on arrays.

Ruby

In Ruby an associate array is called a Hash and is used as follows:

phonebook = {
  'Sally Smart' => '555-9999', 'John Doe' => '555-1212', 'J. Random Hacker' => '553-1337'
}

phonebook['John Doe'] produces '555-1212'

To iterate over the hash, use something like the following:

phonebook.each {|key, value| puts key + " => " + value}

Additionally, each key may be shown individually:

phonebook.each_key {|key| puts key}

Each value may also be shown:

phonebook.each_value {|value| puts value}

References

Share this comparison:

If you read this far, you should follow us:

"PHP vs Ruby." Diffen.com. Diffen LLC, n.d. Web. 23 Sep 2016. < >