PHP reference guide

This post will be update all the time.
I will use this post as a reference guide.
I will always show an example of working code.
This will look a bit messy at first but I will organise it over time.

Remove characters

Last 3 characters of a variable

$var = "ABCDEFGH";
echo substr($var,-3); // FGH

First 3 characters of a variable

$var = "ABCDEFGH";
echo substr($var,0,3); // ABC

Remove 3 first characters and 2 last characters of a variable

$var = "ABCDEFGH";
echo substr($var,3,-2); // DEF

Round

Round a number

echo round(3.6);         // 4

Round a number to 1 decimal

echo round(3.633, 1);      // 3.6

Round a number to 2 decimals

echo round(1.95583, 2);  // 1.96

Round a number to the thousand

echo round(1241757, -3); // 1242000

Passing value in the url

Passing 1 value inside the url
url = http://www.yoursite.com/yourfile.php?date=2015-09-27

$the_date       = $_GET[date];

Passing 2 values inside the url
url = http://www.yoursite.com/yourfile.php?date=2015-09-27&user=22

$the_date       = $_GET[date];  // 2015-09-27
$the_user 		= $_GET[user];  // 22

strtotime

Get the day of the week

$the_date = "2015-09-27";
$day = date("D",strtotime($the_date));		// Sun

Advertise with us!