Comparing Ruby and PHP

Laptop with two windows of code up side by side

Ruby and PHP are both open source languages, and both are ranked in the top 20 of the TIOBE index for most popular programming languages, at spots 13 and 8, respectively, for June 2020.

If you want to learn a new programming language and are trying to decide between these two, let’s explore which may be more useful for you.

Type of language

The distinction between a scripting language and a programming language is often made based on whether a language is interpreted or compiled, but other factors differentiate also. Ruby is an interpreted language but used mainly as a programming language. PHP is also interpreted but used mainly as a scripting language.

A scripting language is used for automating tasks, such as PHP serving web pages in a browser. A programming language is used mainly for developing algorithm-based, code-oriented software. Ruby is used as the programming language in several frameworks, the most notable being Ruby on Rails.

Ruby could be used as a scripting language and PHP could be used as a programming language, but mainly they stick to their general uses.

Object orientation

Both Ruby and PHP are object-oriented languages. What differentiates Ruby is that everything in Ruby evaluates to an object—classes, variables, methods, operators, and keywords. Almost everything is considered an object—I say “almost” because some Ruby constructs are not, such as some non-object keywords.

In PHP, instances of all classes are objects. PHP even has a type called object

Both PHP and Ruby support inheritance, implying that a class extending to another class inherits all its public and protected methods or functions. But neither PHP nor Ruby supports multiple inheritance; a class may extend to only one other class at a time.

Type checking

Both Ruby and PHP are dynamically typed languages, meaning type checking is performed at runtime.

Ruby does not have the notion of static types such as int and string. Instead, variables are declared without a type, and at runtime variables are assigned a type based on the value of a variable. No static type checking is performed at compile time.

PHP is more flexible with regards to type checking and static typing. While PHP is dynamically typed and variables may be declared without a type, PHP supports 10 primitive types. Four are scalar types (boolean, integer, float, and string), four are compound types (array, object, callable, anditerable), and two are special types (null and resource). Static type checking may be used by setting declare(strict_types=1) so the type checking is performed at compile time.

Encapsulation for reusability

Both Ruby and PHP provide for encapsulation in addition to the class being a unit of encapsulation. Classes, functions, and constants may be encapsulated using modules in Ruby and namespaces in PHP, and those modules or namespaces may be imported or mixed in a class for reuse. Modules in Ruby are imported using include and namespaces in PHP are imported using use.

As with any two programming languages, other differences in syntax and constructs also exist, but these are the main differences that are most relevant to Ruby’s and PHP’s common uses.

Up Next

About the Author

TechWell Insights To Go

(* Required fields)

Get the latest stories delivered to your inbox every month.