This is a quick cheatsheet for every Rubyist struggling to remember PHP syntax.
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"
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;
Single line comments begin with a hash.
p "Pay attention to this." #ignore this
Multiline comments begin with
=begin and end with
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
It's very common to see hashes across multiple lines.
p "This is common" #Even though #they are #for single lines #technically
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 */
There are two ways to create an array in Ruby:
my_array = Array.new your_array = [1, 2, 3]
There is a grand total of one way to create an array in PHP:
<?php $this_array = array(1, 2, 3);