PHP for Ruby Developers
Transfer your Ruby experience to PHP 8.3: values, control flow, arrays, functions, exceptions and object design. Build a multi-file time ledger with validated parsing and deterministic reports.
Ruby and PHP side by side
The same program in Ruby and in PHP, from the lessons of this course.
Modules and first run
Ruby require_relative loads a file relative to the current source file and requires it once. PHP uses require_once with __DIR__ to anchor the path. Ruby files need no PHP opening tag; PHP CLI is not a web request, and helper modules should not print during…
Ruby1puts "hello CLI"PHP1<?php2declare(strict_types=1);3echo "hello CLI\n";Branches and precedence
Ruby and PHP share ternary punctuation, but Ruby if itself is also an expression. Ruby && and || return operands; PHP && and || return booleans. Use an explicit value-selection operation instead of porting operand-returning logic blindly.
Ruby1n = 42puts(n > 3 ? "high" : "low")PHP1<?php2declare(strict_types=1);3$n=4; echo $n>3?"high":"low"; echo "\n";Lists are PHP arrays
Ruby Array#delete_at shifts later indexes. PHP unset removes the map entry and preserves numeric keys. array_values reindexes explicitly. Ruby arrays and hashes are separate classes; PHP array serves both list-like and map-like roles.
Ruby1a = [10,20,30]2a.delete_at(1)3puts a.each_index.to_a.join(",")PHP1<?php2declare(strict_types=1);3$a=[10,20,30]; unset($a[1]); echo implode(",",array_keys($a)),"\n";Functions and return types
Ruby methods implicitly return the last evaluated expression. PHP requires an explicit return to provide a value matching a non-void return type. Ruby method arguments are dynamically typed; PHP declared scalar types participate in runtime checking.
Ruby1def surround(s)2 "(" + s + ")"3end4puts surround("note")PHP1<?php2declare(strict_types=1);3function surround(string $s):string{return "(".$s.")";} echo surround("note"),"\n";Exceptions and state
Ruby rescue handles exceptions and ensure handles cleanup. PHP uses typed catch and finally. Ruby bare rescue targets StandardError rather than every Exception; PHP catch(Exception) likewise does not cover every Throwable. Choose the expected failure boundary…
Ruby1begin2 raise "bad"3rescue RuntimeError4 print "caught"5ensure6 puts ":done"7endPHP1<?php2declare(strict_types=1);3try {throw new RuntimeException("bad");} catch(RuntimeException $e){echo "caught";} finally{echo ":done\n";}Classes and typed properties
Ruby attr_reader generates a getter method for an instance variable; @enabled is not a directly public field. PHP public typed properties expose field access with ->. An unread Ruby instance variable yields nil, whereas an uninitialized PHP typed property…
Ruby1class Flag2 attr_reader :enabled3 def initialize4 @enabled = false5 end6end7puts(Flag.new.enabled ? "on" : "off")PHP1<?php2declare(strict_types=1);3class Flag {public bool $enabled=false;} $f=new Flag(); echo $f->enabled?"on\n":"off\n";
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsValues and output
7 lessonsControl flow
5 lessonsCollections
6 lessonsFunctions and modules
6 lessonsErrors and boundaries
4 lessonsObjects and state
4 lessonsObject contracts
6 lessonsSharing and identity
4 lessonsTime ledger project
4 lessonsFinal challenges
2 lessonsFrequently asked questions
Who is PHP for Ruby Developers for?
For people who already write Ruby and want to learn PHP. Each lesson starts from Ruby you know and shows how PHP does the same thing, so you skip the beginner basics.
Do I need to know PHP already?
No. The course starts with your first PHP program. You need Ruby basics: variables, conditions, loops, functions and collections.
How many lessons does PHP for Ruby Developers have?
PHP for Ruby Developers contains 48 interactive lessons you can complete at your own pace.
What does PHP for Ruby Developers cover?
The course is organized into chapters covering: Values and output, Control flow, Collections, Functions and modules, and more.
Is PHP for Ruby Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.