DEV Community

Tina Morlock
Tina Morlock

Posted on

PHP Cheat Sheet for Beginners

Note: I am primarily using this as a study tool as I'm upskilling my foundational knowledge. With this in mind, this document is ongoing, and I'll likely be updating it weekly.

Opening and closing tags:

<?php ?>
Enter fullscreen mode Exit fullscreen mode

Note: omitting the closing tag at the end of a file can be beneficial so you don't have any unwanted white space at the end of the file

Printing:

echo 'This prints to the screen.';
<?= 'This works too.'; ?>
Enter fullscreen mode Exit fullscreen mode

Commenting:

One line:

# This is a one-line comment in PHP
Enter fullscreen mode Exit fullscreen mode

Multi-line:

/* This is a multi-
line comment in PHP */
Enter fullscreen mode Exit fullscreen mode

Checking variable type:

var_dump($var_name);
Enter fullscreen mode Exit fullscreen mode

Comparing Floats:
Note: Comparing floats in PHP is tricky because of precision. The following is a workaround for that limitation.

<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;

if (abs($a - $b) < $epsilon) {
    echo "true";
}
?>
Enter fullscreen mode Exit fullscreen mode

String Definitions:

'this is a string'

"this is a string"

<<<END
   this
        is also
               a string
END

<<<'EOD'
This is an example of
another type of string
you can use in PHP.
EOD

Enter fullscreen mode Exit fullscreen mode

String Interpolation:

<?php
$juice = "apple";

echo "He drank some $juice juice." . PHP_EOL;

?>
Enter fullscreen mode Exit fullscreen mode

or

echo 'this line prints a {$var}';
Enter fullscreen mode Exit fullscreen mode

String concatenation uses a . rather than a + in PHP.


I used PHP's documentation as the source of my information here. Some of the code will be similar or identical.

Top comments (2)

Collapse
 
suvreal profile image
Suvreal

Thank you for sharing! Allow me please share some piece of mind of mine.

Opening and closing tags must not be used as pair. In PHP classes or files you can just use opening and never use closing. PHP will actually load file properly and it is actually considered as best practice.

Remember, single line comment can always in PHP be: // commented.

For checking variable type we can always use echo gettype($variable);. It is more straightforward and actually relevant for our purposes. Writing out variable to get a type is just for debugging purposes.

For comparing floats it is a good take from PHP manual (real source): php.net/manual/en/language.types.f....
Image description

For string interpolation use sprintf('%s %s %s', 'I', 'am', 'batman');. Sprintf types properties according to desired type (%s - string, %d - integer, ETC read docs). It is more organized and provides better formatting options.

Collapse
 
xwero profile image
david duymelinck • Edited

A more common way for a single line comment is to use two forward slashes. In newer PHP versions a hashtag is used to identify attributes, #[Attribute].

Good luck learning PHP!