
Hello again. This time, I will share some ready-to-use PHP function examples that are not easily found with a search. I assume you are familiar with PHP, so I won’t be adding any explanations.
Let's get started together.
(If you have kids at home, they will be watching TV while you stay next to them. You end up watching the cartoon they are watching and that’s how you end up rambling like this! :) )
The most commonly used function for me is the error handling function. The reason is that when my code gets messy, this function provides the file and line number correctly, so I can find the issue and come up with a solution calmly.
PHP ERROR HANDLING FUNCTION
function Hata_Yakala($HataNo, $HataAciklama, $HataDosya, $HataSatir) {
echo '<b>ERROR:</b> [$HataNo] $HataAciklama <b>File:</b> $HataDosya <b>Line:</b> $HataSatir';
}
set_error_handler ( 'Hata_Yakala' );
FILE SIZE CALCULATION/READABLE FUNCTION
function boyut($bayt) {
if ($bayt < 1024) {
return '$bytes byte';
} else {
$kb = $bayt / 1024;
if ($kb < 1024) {
return sprintf ( '%01.2f', $kb ) . ' KB';
} else {
$mb = $kb / 1024;
if ($mb < 1024) {
return sprintf ( '%01.2f', $mb ) . ' MB';
} else {
$gb = $mb / 1024;
return sprintf ( '%01.2f', $gb ) . ' GB';
}
}
}
}
Usage: echo boyut(1024); will display the size in KB, MB, GB for the given byte value.
FUNCTION TO CALCULATE AND DISPLAY TIME DIFFERENCE
function cevir($zaman) {
$simdi = time ();
//$zaman=strtotime($zaman); //You can activate it by removing the // if you send the date as '2018-09-12 00:12:34'.
$fark = $simdi - $zaman;
$sn = $fark;
$dk = round ( $fark / 60 );
$saat = round ( $fark / (60 * 60) );
$gun = round ( $fark / (60 * 60 * 24) );
$hafta = round ( $fark / (60 * 60 * 24 * 7) );
$ay = round ( $fark / (60 * 60 * 24 * 30) );
$yil = round ( $fark / (60 * 60 * 24 * 30 * 12) );
if ($sn < 60) {
return $sn . ' seconds ago';
} elseif ($dk < 60) {
return $dk . ' minutes ago';
} elseif ($saat < 24) {
return $saat . ' hours ago';
} elseif ($gun < 7) {
return $gun . ' days ago';
} elseif ($hafta < 4) {
return $hafta . ' weeks ago';
} elseif ($ay < 12) {
return $ay . ' months ago';
} else {
return $yil . ' years ago';
}
}
This function is a great way to show the duration, like "5 minutes ago". You should pass the $zaman variable as a timestamp, or you can activate the commented line to send it as '2018-09-12 00:12:34'.
SEO URL FUNCTION
function seoyap($text) {
$text = mb_strtolower ( $text, 'UTF-8' );
$trharf = array ('ç','Ç','ı','İ','ş','Ş','ğ','Ğ','ö','Ö','ü','Ü' );
$enharf = array ('c','c','i','i','s','s','g','g','o','o','u','u' );
$text = str_replace ( $trharf, $enharf, $text );
$regex = '#[^-a-zA-Z0-9_ ]#';
$text = preg_replace ( $regex, '', $text );
$text = trim ( $text );
return preg_replace ( '#[-_ ]+#', '-', $text );
}
This function takes the given text (for example, if you send 'Türkçe Harfleri Dene', it will convert it to 'turkce-harfleri-dene'), converts it to lowercase, replaces Turkish characters, removes spaces and special characters, and returns it in a clean format for SEO-friendly URLs.
FUNCTION TO CONVERT DATE FORMAT (YEAR/MONTH/DAY TO DAY-MONTH-YEAR)
function tarihcevir($tarih) {
if ($tarih) {
$ytarih = explode ( '/', $tarih );
$yenitarih = $ytarih ['2'] . '-' . $ytarih ['1'] . '-' . $ytarih ['0'];
return $yenitarih;
} else {
return;
}
}
This is a very simple function. If you send the date as 2018/09/13 like this: echo tarihcevir('2018/09/13');, it will return the date as 13-09-2018.
I wrote this function because it came in handy in places where I needed it.
To be continued....
Related Articles
