An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
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"
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;
Comments
Ruby
Single line comments begin with a hash.
p "Pay attention to this." #ignore this
Multiline comments begin with =begin
and end with =end
.
p "Print this."=beginDon'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
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.
<?phpecho "This will print to the screen." //But this will be ignored./*This will also be ignored*/
Arrays
Ruby
There are two ways to create an array in Ruby:
my_array = Array.newyour_array = [1, 2, 3]
PHP
There is a grand total of one way to create an array in PHP:
<?php$this_array = array(1, 2, 3);
Original Link: https://dev.to/lizlaffitte/php-cheatsheet-for-rubyists-198p
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To