Home arrow PHP arrow Page 2 - 10 PHP Functions I Bet You Didn't Know About!
PHP

10 PHP Functions I Bet You Didn't Know About!


PHP has hundreds of functions. In this article Mitchell looks at 10 uncommon yet useful PHP functions and shows you examples of how to use each of them to save time when coding.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 27
August 09, 2002
TABLE OF CONTENTS:
  1. · 10 PHP Functions I Bet You Didn't Know About!
  2. · Functions 1 to 5
  3. · Functions 6 to 10
  4. · Conclusion

print this article
SEARCH DEVARTICLES

10 PHP Functions I Bet You Didn't Know About! - Functions 1 to 5
(Page 2 of 4 )

function_exists
If you've ever been busy coding and then had the PHP interpreter spit a "myFunc() function already defined" error at you, then the function_exists function might be just what you're after. Its signature looks like this:

bool function_exists ( string function_name)

Function_exists can check if a function has already been defined, and is great for checking if various PHP extensions are installed on the server.

Let's look at a typical scenario in which you define a function in one file and then try to define the same function in another file, wondering why PHP is spitting an error telling you that function already exists:

firstfile.php

<?php

function someFunc()
{
echo "This is some text";
}
?>


secondfile.php

<?php

include("firstfile.php");

function someFunc()
{
echo "creating this function will cause an error";
}
?>


Now, the remedy for this type of function declerations:

thirdfile.php

<?php

include("firstfile.php");

if(!function_exists("someFunc"))
{
// Function doesn't exist, let's create it
function someFunc()
{
echo "creating this function will cause an error";
}
}
?>


As you can see, the thirdfile.php script checks if the someFunc() function already exists, and if it does it simply skips defining the function. If you're unsure whether or not your web host has a certain extension installed, you can check so like this:

<?php

define("EXTENSION_FUNC", "mssql_connect");

if(!function_exists(EXTENSION_FUNC))
{
die("ERROR: Function " . EXTENSION_FUNC " . doesn't exist!");
}
else
{
// Function exists, continue ...
}
?php>


unlink
I've had many a PHP newbie asking why PHP doesn't provide a function called fdelete or something similar to delete a file. PHP does provide a delete file function, and it's called unlink(). It's very similar to the Unix C unlink() function, and accepts the name of a file to delete:

int unlink ( string filename)

Using the unlink function is very much a one line job, as shown below:

<?php

// Delete the file /tmp/test1/blah.bin
if(@unlink("/tmp/test1/blah.bin"))
{
// File deleted successfully
}
else
{
// An error occurred
}
?>


Note that the unlink function can only be used to delete files. If you want to delete a directory then you will need to use the rmdir() function.

mysql_list_dbs
If you're hosting more than one site per web host, then there's a good chance that you're using more than one database. The mysql_list_dbs function returns a list of available databases on your MySQL server and has the following signature:

resource mysql_list_dbs ( [resource link_identifier])

It returns a resource, which we can use in a loop to see the names of each database that we have access to on our MySQL server:

<?php

// Connect to the database
$svrConn = @mysql_connect("localhost", "root", "")
or die("Couldn't connect to database server");

$myDatabases = mysql_list_dbs($svrConn);
$i = 0;

while($row = mysql_fetch_object($myDatabases))
{
echo ++$i . ". " . $row->Database . "<br>";
}


The code shown above would produce some output like this:
  1. dbFred
  2. sales_database
  3. test
  4. mysql
  5. johnDatabase
This function can be extremely useful when you want to perform some sort of script-based back up of your databases, or if you're in need of some similar information about each database, such as how many tables they contain, etc.

getrusage
At an operating system level it's easy to see the number of page faults, swaps, flushes etc for your computer, but how do you accomplish it through PHP? The getrusage function can be used to return an associative array containg details of system level calls. Its signature looks like this:

array getrusage ( [int who])

This function only works on Linux. Here's some sample code:

<?php

$usage_stats = getrusage();

// Print the entire contents of the $usage_stats array
print_r($usage_stats);

// Show some individual parts of the $usage_stats array
echo "Number of page faults: " . $usage_stats["ru_majflt"] . "<br>";
echo "User time used in seconds: " . $usage_stats["ru_utime.tv_sec"] . "<br>";

?>


parse_url
This would have to be one of the most useful functions that I've ever stumbled across. It's called parse_url, and it accepts a string representation of a URL:

array parse_url ( string url)

It returns an associative array containing the various parts of the URL, such as the protocol, host, port, path, query string, etc. This function could come in handy when you require a user to enter a valid URL for something like a competition, feedback form, etc.

The parse_url function is simple enough, and can be used like this:

<?php

$myDomain = parse_url("http://www.devarticles.com/?param1=blah#bottom");

echo "Domain: " . $myDomain["host"] . "<br>";
echo "Query String: " . $myDomain["query"] . "<br>";
echo "Anchor: " . $myDomain["fragment"] . "<br>";

?>

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials