Common mistakes and points to remember in MySQL

Rating 3.00 out of 5

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 . . . → Read More: Common mistakes and points to remember in MySQL

How to compare the execution time of PHP code blocks

Rating 3.67 out of 5

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,’; . . . → Read More: How to compare the execution time of PHP code blocks

How to optimize PHP code

Rating 4.25 out of 5

While writing PHP code, you should keep following points in mind, to make your code run efficiently and quickly.
Most the optimization depends on the tuning of the webserver, compiling PHP with correct configuration options and cache usage. But, still, some what optimization can be achieved, if you,
1) “Loops” (for, foreach, while) are . . . → Read More: How to optimize PHP code

Parse XML or HTML

Rating 2.50 out of 5

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);
$xpath->registerNamespace(‘atom’, “http://www.w3.org/2005/Atom”);

$xpath_str = ‘//atom:entry/atom:published/text()’;

$entries = $xpath->evaluate($xpath_str);

print $entries->item(0)->nodeValue .”\n”;

?>

You . . . → Read More: Parse XML or HTML

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

Rating 4.00 out of 5

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 . . . → Read More: How to send mail when Googlebot crawls a webpage of your website

How to parse PHP in html pages

Rating 3.00 out of 5

How to parse PHP in web pages having extension as .html / .htm?

This can be done using .htaccess file in the directory where you want all your .html files to be treated as php while still showing .html in the address bar.

Create a simple text file.  Put the following code in that . . . → Read More: How to parse PHP in html pages

Set time out while downloading files in PHP

Rating 4.00 out of 5

How to set a timeout while downloading a file from web in PHP?

Time out can be implemented in various ways in PHP e.g. by setting proper HTPP headers,  cURL options, PHP ini options, stream context options etc.

Method 1, using cURL timeout,

<?php
$url = ‘http://www.example.com’;
$text = content_from_curl($url);

function content_from_curl($url)
{
$content = false; . . . → Read More: Set time out while downloading files in PHP

How to remove last comma from a string in PHP?

Rating 3.50 out of 5

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 . . . → Read More: How to remove last comma from a string in PHP?

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

Rating 3.50 out of 5

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 proof
echo “<br>”;
$b = microtime(true);                                   //start time . . . → Read More: How to know if a file exists on the web or not, in PHP?

How to create a random string in PHP

Rating 3.00 out of 5

A random string with length specified as a parameter ($sLength) and having allowable characters in another parameter ($a).

<?php
$s = “”; //random string
$sLength = 35; //random string length (you can put any number, you like your string to be in length)

//allowable chars in the wanted random string (add other chars but avoid chars . . . → Read More: How to create a random string in PHP

Usefull PHP array functions

Rating 4.00 out of 5

How to check if a value exists in array or not in PHP?

PHP function in_array() checks if a value exists in an array in a case-sensitive manner and returns boolean result.
e.g.,

<?php
$os = array(“Mac”, “NT”, “Unix”, “Linux”);
if (in_array(“Unix”, $os)) {
echo “Got Unix”;
}
?>

How to check if a key/index exists in array or not . . . → Read More: Usefull PHP array functions