PHP PHP Basics Cheatsheet - everything you need to know

PHP PHP Basics Cheatsheet

How to output (echo/print) content in PHP

There are several ways to output data/content. I might have forgotten about one of them

1) A php file with any content apart from <?php . Without the PHP opening tag, everything else will be echoed out.

2) echo 'some text'; - a simple echo

3) print ('some text'); - similar to echo

4) <?='some text':?> or <?=$a_variable:?>

5) and via functions such as print_r(12345) or var_dump($something)

What is the difference between echo and print in PHP?

There isn't much difference. The only two differences are that echo does not return any value, and can take multiple arguments (echo($a,$b,$b). print() returns a value of 1 and can only take one parameter. Years ago echo() used to be a bit faster, now it would be so insignificant that it is not worth even thinking about which is faster..

View More Details (and 11 discussions about this topic) Here...

What are the predefined variables in PHP?

There are a handful of predefined variables in PHP. They include:

$GLOBALS — References all variables available in global scope

$_SERVER — Server and execution environment information

$_GET — HTTP GET variables

$_POST — HTTP POST variables

$_FILES — HTTP File Upload variables

$_REQUEST — HTTP Request variables

$_SESSION — Session variables

$_ENV — Environment variables

$_COOKIE — HTTP Cookies

$php_errormsg — The previous error message


$http_response_header — HTTP response headers

$argc — The number of arguments passed to script

$argv — Array of arguments passed to script

The most commonly used ones, in my opinion, are $_GET, $_POST, $_SERVER, $_FILES, $_SESSION and $_COOKIE (so, the majority of them!)

View More Details (and 7 discussions about this topic) Here...

PHP Date commonly used formats

The date() function in PHP is very simple. It takes two arguments (the second is optional) - the $format, and the optional $timestamp - date('y-m-d',time()- 31536000) = 17-10-12

(If no timestamp is provided then it uses the current date/time. The timestamp is in seconds, since January 1 1970 00:00:00 GMT. Current timestamp right now: 1539357386

The most difficult part of using date() in PHP is remembering its format.

The following characters are recognized in the format parameter string
format characterDescriptionExample returned values
dDay of the month, 2 digits with leading zeros01 to 31
DA textual representation of a day, three lettersMon through Sun
jDay of the month without leading zeros1 to 31
l (lowercase 'L')A full textual representation of the day of the weekSunday through Saturday
NISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)1 (for Monday) through 7 (for Sunday)
SEnglish ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
wNumeric representation of the day of the week0 (for Sunday) through 6 (for Saturday)
zThe day of the year (starting from 0)0 through 365
WISO-8601 week number of year, weeks starting on MondayExample: 42 (the 42nd week in the year)
FA full textual representation of a month, such as January or MarchJanuary through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, three lettersJan through Dec
nNumeric representation of a month, without leading zeros1 through 12
tNumber of days in the given month28 through 31
LWhether it's a leap year1 if it is a leap year, 0 otherwise.
oISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)Examples: 1999 or 2003
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a yearExamples: 99 or 03
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
BSwatch Internet time000 through 999
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds, with leading zeros00 through 59
u Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds.Example: 654321
v Milliseconds (added in PHP 7.0.0). Same note applies as for u.Example: 654
eTimezone identifier (added in PHP 5.1.0)Examples: UTC, GMT, Atlantic/Azores
I (capital i)Whether or not the date is in daylight saving time1 if Daylight Saving Time, 0 otherwise.
ODifference to Greenwich time (GMT) in hoursExample: +0200
PDifference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)Example: +02:00
TTimezone abbreviationExamples: EST, MDT ...
ZTimezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.-43200 through 50400
Full Date/Time------
cISO 8601 date (added in PHP 5)2004-02-12T15:19:21+00:00
r» RFC 2822 formatted dateExample: Thu, 21 Dec 2000 16:01:07 +0200
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)See also time()
View More Details (and 8 discussions about this topic) Here...

What is the difference between empty(), isset(), != null, is_null() in PHP

All four of these are ways to basically check if a variable (or return value) is empty or null. But they all have slight differences in how they approach it.

A lot of people assume that empty() and isset() are opposites, but if you look at the table at the bottom of this page you will see they aren't quite opposite. (e.g. an empty array ([]) will return true for both empty([]) and isset([]) They both also return true for values such as "0", 0, 0.0.


This will check if the variable is empty. The official docs list it as "A variable is considered empty if it does not exist or if its value equals FALSE.".

This means that empty() will return true if:

  1. if $var does not exist
  2. if $var = false
  3. if $var = null
  4. if $var = [] (empty array)
  5. if $var = "" (empty string)
  6. if $var = 0
  7. if $var = "0" (0 as a string)
  8. if $var = 0.0 (0 as a float)


isset() is a mix between checking if something is set (i.e. the variable exists, or in the case of arrays if the array key exists)) and checking if it is not null

It will return true for most situations, unless the variable does not exist, or if the variable = null

If you supply multiple arguments, it will only return true if all are set.

  1. $a  = $b = $c = true;
  2. $null1 = $null2 = null;
  4. var_dump( [
  5.     "a_b_c" => isset($a,$b,$c),
  6.     "null1_null2" => isset($null1,$null2),
  7.     "a_b_null1" => isset($a,$b,$null1),
  8. ]);
"; //var_dump( [ // // "a_b_c" => isset($a,$b,$c), // "null1_null2" => isset($null1,$null2), // "a_b_null1" => isset($a,$b,$null1), // //]); // echo "";// $a = ) ?>
array(3) {
    ["a b c"]=> bool(true) // all were set and have values
    ["null1 null2"]=> bool(false) // both were null
    ["a b null1"]=> bool(false) // one was null

$val == null

This will return true for all situations where $val has a value. There are some weird situations that you must be aware of though.

It will return false for $val = "0"; var_dump($val == null); . I don't know why.

$val === null and is_null($val)

The only time that either of these statements will return true is if $val actually is null (i.e. $val=null;).

Testempty($val)isset($val)$val == null$val === nullis_null($val)
false true true true false false
true false true false false false
null true false true true true
empty array true true true false false
array with 1 item false true false false false
empty string ("") true true true false false
space (" ") false true false false false
0 true true true false false
"0" (as string) true true false false false
0.0 (as float) true true true false false
View More Details (and 7 discussions about this topic) Here...