How to know if a file exists on the web or not, in PHP?

I have listed a few methods in PHP to check the existence of a file on the web. <?php $fileURL = “http://www.some-domain.com/image.jpg”; echo “Checking the existence of $fileURL<br>”; ?> Method 1: <?php $a = microtime(true); //start time in microseconds of 1st method echo date(“d/m/y : H:i:s”, time()), ” “,$a, “<br>”; get_http_response_code($fileURL); //fast but not fool […]

How to remove last comma from a string in PHP?

To remove/trim the last/trailing or first/heading comma from a string, one should first confirm that the last character of the string is comma itself and nothing else. This would make the code foolproof and prevent accidental removal of characters other than comma. A few methods are described here and listed in the order of increasing […]

How to send mail when Googlebot crawls a webpage of your website

How to send an email, as soon as Googlebot crawls a webpage of your website, in PHP? Many of us have always wished to get some kind of intimation as soon as Google (Googlebot to be specific) crawls our websites. Isn’t it? Don’t know if there are any tools available for the same but what […]

How to compare the execution time of PHP code blocks

How to compare the execution time of PHP code blocks? Sometimes we want to test our code for efficiency and execution time by comparing it with other code. Here’s how you can compare two code blocks in PHP and decide which one is faster (helps in optimization of code). $str = ‘1,2,3,4,’; //we will trim […]

Common mistakes and points to remember in MySQL

Some common mistakes we often commit while coding in PHP to access MySQL DB (Database). 1. For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. For other type of SQL statements like INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE […]

Parse XML or HTML

Here’s a simple XML parser, with DOM Object, which can fetch values digging deep but in a few lines of code. It uses namespace functinality(XPATH). The XML document must have defined namespaces. <?php $xml = <<<EOT <?xml version=”1.0″ encoding=”UTF-8″?> <entry xmlns=”http://www.w3.org/2005/Atom” xmlns:other=”http://other.w3.org/other” > <id>uYG7-sPwjFg</id> <published>2009-05-17T18:29:31.000Z</published> </entry> EOT; $doc = new DOMDocument; $doc->loadXML($xml); $xpath = DOMXPath($doc); […]