Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 27, 2021 03:11 am GMT

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."=beginDon'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.

<?phpecho "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.newyour_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

Original Link: https://dev.to/lizlaffitte/php-cheatsheet-for-rubyists-198p

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To