DEV Community

Liz Laffitte
Liz Laffitte

Posted on

1 1

PHP Cheatsheet for Rubyists

This is a quick cheatsheet for every Rubyist struggling to remember PHP syntax.

Variables

Ruby
Variable names can begin with an alphanumeric character or an underscore, but not a number. Local variables don't require any keywords. Variables are case sensitive (name and Name are two different variables). Conventionally, variables begin with a lowercase letter, and snakecase is used for multi-word variable names. You must assign a value to a variable when it is initialized, even if that value is 0, an empty string, or nil.

Instance variables are initialized with the @ symbol, class variables with two @@, and global variables with a $.

name = "Ginny"
Name = "Jennifer" 
p name # "Ginny"
p Name # "Jennifer"
your_name = "Ted"
_energy = nil
@@name = "Liz"
$tater = "Tot"
Enter fullscreen mode Exit fullscreen mode

PHP
PHP variables always start with a dollar sign ($) and all variable declarations (all lines in PHP actually) must end with a semicolon (;). Other than that, PHP and Ruby variables have very similar rules. Variables names can start only with an alphanumeric character or an underscore, never a number. Variables are case sensitive.

However, you can declare a PHP variable without assigning it a value. Also, multi-word variables names can be camel- or snakecase.

<?php
$tatertots;
$name = "Liz";
$hours_of_sleep = 0;
$tatertots = 3;
Enter fullscreen mode Exit fullscreen mode

Comments

Ruby
Single line comments begin with a hash.

p "Pay attention to this." #ignore this
Enter fullscreen mode Exit fullscreen mode

Multiline comments begin with =begin and end with =end.

p "Print this."
=begin
Don't print this.
Also, don't look at this.
It's not pretty enough to be part of Ruby.
=end
Enter fullscreen mode Exit fullscreen mode

It's very common to see hashes across multiple lines.

p "This is common"
#Even though
#they are
#for single lines
#technically
Enter fullscreen mode Exit fullscreen mode

PHP
Single line comments start with two forward slashes ( // ). Multi-line PHP comments start with a forward slash and a star (/) and end with a star and a forward slash (/), similar to multiline CSS comments.

<?php
echo "This will print to the screen." //But this will be ignored.
/*
This will also be ignored
*/
Enter fullscreen mode Exit fullscreen mode

Arrays

Ruby
There are two ways to create an array in Ruby:

my_array = Array.new
your_array = [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

PHP
There is a grand total of one way to create an array in PHP:

<?php
$this_array = array(1, 2, 3);
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
haelmx profile image
Jorge • Edited

Just like Ruby, with PHP yo can create an array with $this_array = [1, 2, 3];

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs