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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

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];

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay