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.68/5
  • 1
  • 2
  • 3
  • 4
  • 5
(112 ratings)
  • current rating is 3.42/5
  • 1
  • 2
  • 3
  • 4
  • 5
(99 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 # #
Classes Yes Yes
String concatenation operator . (dot) + (the plus sign)
Programming paradigms imperative, object-oriented, reflective Imperative, object-oriented, reflective, aspect-oriented, functional
OOP (Object Oriented Programming) Yes, single inheritance. Yes, but you don't have to.
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
Garbage Collection Yes Yes
Vectorized operations Yes Yes
String comparison syntax strcmp($string1, $string2) string1 <=> string2
String equality testing syntax $string1 == $string2 string1 == string2
Typing Weak typing Strongly typed
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)
Intended use Web applications, CLI Applications, scripting
Syntax for reversing a string strrev($string) string.reverse
Language design goals Robustness and simplicity Expressiveness, Readability
Syntax for converting string to upper case strtoupper($string) string.upcase
Examples of Use WordPress, Wikipedia Indiegogo, Hulu

Associative Arrays in PHP vs. Ruby


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.


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}


Share this comparison:

If you read this far, you should follow us:

"PHP vs Ruby." Diffen LLC, n.d. Web. 22 Feb 2019. < >