Apostolos Dountsis

Web Development & Desktop Application Solutions
  • rss
  • Home
  • Projects
    • Easy Peasy Image Gallery
    • Social Bookmarks
      • Social Bookmarks Release Notes
      • Social Bookmarks Supported Sites
    • Move Comments
  • Downloads
  • Freelance Services
  • Photo Gallery
    • U2 – Vertigo Tour (Rome, Italy)
    • Brighton
    • Amsterdam
    • Athens 2004
    • Holiday Snapshots
  • Portfolio
  • About
  • Contact

PHP General Purpose Functions

PHP is a simple and yet very powerful high-level computer language that it is designed for creating dynamic pages and applications on the web. It is very well documented on the official web site and as it becomes obvious from browsing the Functions Reference on the online manual, PHP has a great API.

However, everytime I embark on a new project either for a client or for personal satisfaction, I end up in need of a set of general-purpose fuctions that are not included in PHP. I tend to put these functions in a file named common.php that I usually include in the base class of my project.

pre_print_r()

This function displays information about a variable in a way that’s readable by humans. If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.

function pre_print_r($a)
{
print('< pre >');
print_r($a);
print('< /pre >');
}

println()

Performs a standard print() on the browser but the generated HTML source code is nicely formatted.

function println($str)
{
print($str."\n");
}

is_even()

Checks if the parameter is an even number. This is useful when you want to present data in a tabular format and you want to display every alternative row with a different background color.
function is_even($number)
{
if ($number % 2 == 0 )
{
// The number is even
return true;
}
else
{
// The number is odd
return false;
}
}

redirect()

This is a wrapper around header(). If no parameter is specified then it performs on redirect on the current page. This function is extremely useful when an insert query is executed as a result of a form submission. If the user decides to refresh the page then the query won’t be performed twice.

function redirect($location='')
{
if(empty($location))
{
header("location: {$_SERVER['PHP_SELF']}");
}
else
{
header("location: $location");
}
exit();
}

send_mail()

This is a wrapper function around mail(). It generates the email headers based on the function parameters.

function send_mail($from_name, $from_email, $to_name, $to_email, $subject, $message)
{
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: "".$from_name."" <".$from_email.">\n";
if(mail(""".$to_name."" <".$to_email.">", trim($subject), trim($message), $headers))
{
return true;
}
else
{
return false;
}
}

is_extant()

Checks whether the variable is empty, but allow for it to be zero.
function is_extant($var)
{
if (isset($var))
{
if( empty($var) && ($var !== 0) && ($var !== '0') )
return false;
else
return true;
}
else
return false;
}

gd_loaded()

GD is an extremely popular graphic library that is usually compiled into the PHP but it is not available out of the box. This function checks whether the GD has is available in the PHP installation or not.

function gd_loaded()
{
if (!extension_loaded('gd'))
{
if (!dl('gd.so'))
{
return false;
}
}
return true;
}

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...
Bookmark This Page

Add to Buzz Add to Del.icio.us Add to DotNetKicks Add to Facebook Add to Google Bookmarks Add to Mister Wong Add to Tip'd Add to Twitter Add to Yahoo My Web
Hide Sites
Categories
PHP
Tags
PHP
Comments rss
Comments rss
Trackback
Trackback

« Album Galleries Setting up Eclipse for PHP »

5 Responses to “PHP General Purpose Functions”

  1. bob says:
    June 9, 2006 at 2:13

    very useful.

    Thanx!

  2. Ronson says:
    July 5, 2006 at 11:50

    PHP is not a high level programming language. For it to be a high level programming language it must be used with a desktop enviroment and PHP is clearly not. Although some may argue it’s a programming language. It is not simply because its a web language.

    Further more PHP does not create web applications, it only allows dynamic pages to be created and user interaction for script kiddies.

    James.

  3. apostolos says:
    July 13, 2006 at 10:48

    Hi James,

    The term High Level Language refers to the higher level of abstraction from machine language. Rather than dealing with registers, memory addresses and call stacks, high-level languages deal with variables, arrays and complex arithmetic or boolean expressions. In addition, they have no opcodes that can directly compile the language into machine code, unlike low-level languages like Assembly language. Other features such as string handling routines, object-oriented language features and file input/output may also be present. In general, high-level languages make complex programming simpler, while low-level languages tend to produce more efficient code. In a high-level language, complex elements can be broken up into simpler, though still fairly complex, elements for which the language provides abstractions, keeping programmers from having to “reinvent the wheel”. Therefore, PHP is a high level language and it is completely unrelated to whether or not you use it to develop desktop applications.

    PHP is a language that is used primary for web development but this does not mean that this is the only use that it can have. You can develop PHP programs that run on a console and also desktop applications with a complete GUI. For more information, check out the official site and this tutorial for a quick introduction. So, you are in the wrong, believing that it is simply a web language.

    Finally, you can create web applications using PHP. You may want to have a serious look into the language and especially with the release of PHP5, sky is the limit. Remember that in any computer lanuage you can possibly do more than you think that you can.

    Apostolos

  4. Matt says:
    January 24, 2007 at 2:01

    Thanks a lot for the functions. I used the is_extant() function.

  5. More PHP functions | Apostolos Dountsis says:
    July 6, 2008 at 21:25

    [...] have updated the list of general purpose functions with additional functions. I hope that you find them [...]

Leave a Reply

Click here to cancel reply.

About Dountsis.com

Dountsis.com is the site of the web developer, technology enthusiast, and gadgets addict, Apostolos Dountsis. The articles on this site focus on web development, software and freelance.

Topics

  • EPIG (2)
  • Firefox (6)
  • Funny (1)
  • Programming (3)
  • Site Updates (7)
  • Tutorials (4)
    • .NET (1)
    • PHP (3)
  • Windows (5)
  • WordPress (25)

PayPal Button

If you feel that the open-source projects that I develop and deliver through the site are worth a penny then you could press the button below to send it to me.

Keywords

.NET adblock Bad Sectors C# chkdsk Del.icio.us Eclipse Extension Firefox Hard Disk Microsoft Move Comments PEAR PHP Social Bookmarks Spam User Control Windows Windows XP WinForm WordPress

What I'm Doing...

  • Nite nite 1 week ago
  • RT @JulieMaggot: Τζουλια ΕΕΕΛΑΑΑΑΑ!, Θα κανουμε Σαμπανιαααααααααααα!! #julia_porn 1 week ago
  • My back! Ouch ouch 3 weeks ago
  • More updates...

Recent Articles

  • Social Bookmarks 4.1.2 Point Release
  • How to use User Controls to create MDI WinForm Applications
  • Social Bookmarks Sites Cleanup
  • Social Bookmarks 4.1.1
  • WordPress Plugins Activation Order
  • WordPress Plugin Development

Spam Blocked

65,105 spam comments
blocked by
Akismet
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox