Code Snippets

This is a collection of really useful and handy PHP code snippets. You can share your code snippet through "Post a snippet" page if you think it may be useful for others. We keep adding new snippets all the time !

Calculate percentages in PHPConnect to MySQL databaseConvert URL in textCopyright year updateCount lines in a fileDatabase connection using PDODetect browser with PHPDraw a table using PHPFile last modifiedFormat currencyGenerate random string with PHPGet file extensionHTTP Redirection in PHPJava Script Right Click Disable List files in a directory Measure page load time
Measure page load time
Measure how much time it takes for your web pages to load using just a few lines of PHP code
<?php 
//Create a variable for start time - it needs to be the very first line of your web page
$time_start = microtime(true);
?>
your web page code goes here
<?php
//Create a variable for end time - this needs to be at the end of the web page when everything else is loaded
$time_end = microtime(true);

$time = $time_end - $time_start;

echo 'Page took '.$time.' seconds to execute';
?>
Random HTML colorRemove duplicate arrayremove last characterRemove special charactersSave text in a fileSeconds to StringSend email with PHP mail()SEO friendly linksShow random imageValidate email addressValidate IP address
  • 1