Apostolos Dountsis

Web Development & Desktop Application Solutions
  • rss
  • Home
  • Projects
    • Easy Peasy Image Gallery
    • Social Bookmarks Plugin for WordPress
      • Social Bookmarks Release Notes
    • Move Comments
  • Downloads
  • Freelance Services
  • Photo Gallery
    • U2 - Vertigo Tour (Rome, Italy)
    • Brighton
    • Amsterdam
    • Athens 2004
  • 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 begin a new project either for a client or for personal experimentation, 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 (No Ratings Yet)
Loading ... Loading ...
Bookmark This Page

Add to BlinkBits Add to BlinkList Add to Bloglines Add to Blogmarks Add to Blogmemes Add to Blue Dot Add to Co.mments Add to Del.icio.us Add to De.lirio.us Add to Diigo Add to digg Add to DZone Add to Facebook Add to Fark Add to Feed Me Links Add to Fleck Add to FriendSite
Add to FURL Add to Google Bookmarks Add to Kaboodle Add to Ma.gnolia Add to Mister Wong Add to Netscape Add to Netvouz Add to Newsvine Add to PlugIM Add to PopCurrent Add to reddit Add to Slashdot Add to Stumble Upon Add to Shoutwire Add to SphereIt Add to Taggly Add to Yahoo My Web
Hide Sites
Categories
PHP
Tags
PHP
Comments rss
Comments rss
Trackback
Trackback

« Album Galleries Eclipse for PHP »

5 responses

very useful. Thanx!

bobNo Gravatar | June 9, 2006

very useful.

Thanx!

PHP is not a high level programming language. For it

RonsonNo Gravatar | July 5, 2006

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.

Hi James, The term High Level Language refers to the higher

apostolosNo Gravatar | July 13, 2006

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

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

MattNo Gravatar | January 24, 2007

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

[...] have updated the list of general purpose functions with

More PHP functions | Apostolos Dountsis | July 6, 2008

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

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


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

  • Firefox (6)
  • Funny (1)
  • PHP (7)
  • Programming (3)
  • Site Updates (7)
  • Windows (5)
  • WordPress (17)

RSS Feeds

Subscribe to the Articles RSS feed Subscribe to the Comments RSS feed

Keywords

adblock Bad Sectors chkdsk Del.icio.us Extension Firefox Hard Disk Microsoft PEAR PHP Spam Windows Windows XP WordPress

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.

Recent Articles

  • Spam Comments
  • Del.icio.us Firefox 3 Extension
  • Site Revamp & WordPress 2.5
  • Social Bookmarks 4 Point Release
  • Firefox 3 Beta 4
  • Setup a Development Environment for Your WordPress Site

Spam Blocked

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

© 2006 - 2008 Apostolos Dountsis - All Rights Reserved