http://net.tutsplus.com/tutorials/php/15-wonderfully-creative-uses-for-php/
15 Wonderfully Creative Uses for PHP
May 4th in PHP by Joel Reyes If you are familiar with the basics of PHP, then you're probably wondering how you can use it to make your website more appealing. The possibilities are endless, and you can write your own PHP scripts or implement widely available scripts from around the web. Let’s get started with 15 creative uses for PHP for your website!
Author: Joel Reyes
Joel Reyes-Has been writing and developing web sites for several years now, this has lead him to be the creative mind behind Looney Designer a design resource and portfolio site that revolves around web and graphic design.
1. E-Commerce
E-commerce is one of the major uses for PHP. From a small business level to an enterprise level, businesses are always looking to create additional streams of revenue online. If you know how to integrate existing e-commerce solutions or build your own from scratch, this gives you a distinct advantage with your clients.
Advanced Coders
If you want to build your own shopping cart application, you can either code the entire application from scratch or implement a PHP framework. If you are an intermediate to advanced PHP coder, I personally recommend using a framework such as CodeIgniter or CakePHP. CakePHP has a bakery section with readily available source code for e-commerce applications. For instance, you can integrate Paypal with your site using ready-made scripts. CodeIgniter has a user guide and a few tutorials to get you up and running quickly. Both of these frameworks have extensive documentation on how to create web applications from the ground up; which one you use is really a matter of personal preference.
Beginners
If you are new to PHP, or you just know some basic PHP programming, I would suggest using an existing e-commerce solution. Some of the available options are:
While I understand that each of these solutions has its own quirks, it might be easier to spend a few days with the documentation or a tutorial than to learn PHP from the ground up. It really depends on how much time you are willing to invest in tackling e-commerce.
2. Project Management Tools
For both freelancers and web development firms alike, project management is an important aspect of your business. Your clients need a resource to be able to check the progress of the work and provide feedback. Ideally, with a good project management system in place, your clients will be thoroughly pleased with the end result.
There are several excellent web-based project management tools on the market today. If you can afford to use a subscription service, I would highly recommend Basecamp. Although Basecamp was written in Ruby on Rails, it is an excellent advertisement for effective, streamlined project management solutions. For those of us who do not necessarily need a subscription-based product, you can build your own!
Building your own project management tool from the ground up will require a bit of in-depth PHP and some Javascript knowledge. The most important aspects of the application are security, time-tracking, collaborative to-do lists, file sharing, a message board, and a live preview of the website - which could simply be a link to an index.html page. Of course, you can add to this list or remove features as you like. The takeaway point here is that you can learn a lot about PHP by creating this application and your customers will be happy to see their project take shape. Showing you how to get started on your own project management tool is beyond the scope of this article, but I hope I have given you a good basis to start generating ideas!
3. Graphical User Interface
For those of you who are up to the challenge, you can extend your PHP installation to create desktop applications. This one is a challenge because it requires some extensive knowledge of PHP and it might also be easier to create desktop applications in other programming languages. If PHP is your favorite programming language, then you can use some of these PHP extensions to get you started creating GUI applications.
- PHP GTK - This extension is a popular open source that implements the GIMP toolkit
- ZZEE PHP GUI - A paid solution that allows you to turn your PHP scripts into Windows applications
The main advantage of creating your own PHP GUI's is that you will learn a great deal about the language itself!
4. Building an Online Community
Whether your website is about business, entertainment, or products and services, internet users need to feel connected to the product or message. For example, if you develop web applications, a forum where your customers can discuss issues might be a good idea. As a user, if I have a question and I need support right away, a hotline or an e-mail form is often insufficient. With an online community, your visitors can help solve each other's product-related issues, and even answer technical questions. You still have to provide some level of after-sales support, but a community can effectively decrease your workload and provide useful feedback.
You can build your own PHP-driven online community, or choose from widely available scripts that you can implement into your website. Again, if you plan to build your own forum from scratch, I do recommend the use of a PHP Framework. CodeIgniter, for example, has classes and helpers to take care of the most routine tasks you can think of. Beyond that, you can use several different forum building tools. Some popular ones include:
5. Developing Facebook Applications
You can integrate Facebook with your website using PHP. If you have developed Facebook applications using another language or you would like to get started with PHP, the Facebook developer's wiki can help you to get started. The developer's wiki explains the Facebook PHP client library and provides detailed instructions on how to install and use the files included in the library. This is certainly worth a look if you are interested in programming for the Facebook platform. For Facebook users interested in the back-end of the platform, this would be a natural step.
6. Generating PDF Files
The PDF format is Adobe's proprietary file type for document exchange. Using a library called PDFLib, you can generate PDF files with PHP. This library is already included with PHP5; to access it, you need to uncomment the appropriate lines in your PHP configuration file. An example of why creating PDF files might come in useful is, if you were building an online invoicing application and you wanted to output an HTML-generated invoice in PDF format. You can then send the invoice via e-mail or print a copy of it to your client.
7. Parsing XML Files
PHP allows you to parse XML files. Parsing XML is an important feature of PHP 5 because not all browsers can output the contents of an XML file; so you can create a parser in PHP to facilitate this process. Using XML is important for RSS feeds, and also for data storage and rendering data on different devices - for example, cell phones use an implementation of XML called WML (Wireless Markup Language). Working with XML files in PHP is similar to handling the opening, closing, and reading of a file. The steps involved are creating an XML parser, setting functions to handle your opening and closing XML tags, opening the file for reading, reading the file incrementally and then closing it.
8. Mailing Lists
You can write your own script to send e-mail newsletters to your client, or use a ready-made script. PHP mailing lists are an excellent way to keep your clients informed about your services and products, holidays, vacations, and general announcements. Anything your clients need to know can be included in your automated newsletter. The PHP online documentation explains PHP mailing functions in more detail. There are also scripts you can download and install on your website:
9. Image Processing and Generation
Using the GD library with PHP, you can do more than just output HTML to the browser! You can output images in different file types including jpeg, png, and gif. This feature of PHP is useful because it allows you to create thumbnail pictures, add watermarks, resize and crop images, and even create a photo gallery!
10. Create Graphs and Charts
Do you need visual representations of numbers on your site? PHP can create graphs and charts too! Using Image_Graph, you can create up to fourteen different types of charts including pie charts, bar graphs, impulse, dot/scatter, step, candlestick, box & whisker, radar. This is incredibly useful for e-commerce websites or websites where you need to present graphical data in a concise manner. the Image_Graph website has more detail on how you can get started!
11. Content Management Systems
One of the most popular uses of PHP is creating or using Content Management System. A good CMS allows your clients to update their website and add content without any in-depth knowledge of HTML and CSS. A good Content Management System should be user friendly, extensible, produce clean URL's, and be search engine friendly among other things. There are several online resources you can use to assist you with coding your own CMS from scratch, or you can use one of the widely available free or commercial solutions listed below:
12. Create a PHP Photo Gallery
By simply using PHP's file handling functions, you can create your own photo gallery! you begin by placing your photos in a single directory, you then use PHP's exif function to get header information about the photo and output a thumbnail version of it. The process is as straightforward as it sounds and its also a great way to present your photos!
13. Create Dynamic Website Templates
Using PHP, you can make it easier to add pages and elements to your websites dynamically. You begin by creating the HTML page and splitting it into the header, main content, and footer sections. Add the .php extension to your subsequent pages and use server-side Includes for the header and footer for each new page. You can also have dynamic sidebars and top navigation sections. As a matter of fact, the more "templated" your site is, the easier it is to update the content.
14. Create Wordpress Plugins
If you have done any work with Wordpress, you will know that it is a highly flexible blogging system that you can use to do just about anything from e-commerce to content management. With that being said, if you know some PHP and you delve into the Wordpress Codex, you have everything you need to begin plugin development. If you need novel ideas for creating plugins, they even have a section where users post their plugin ideas.
Watch a screencast that teaches how to build your first WordPress plugin.
15. Creating Flash
The use of Flash in websites is a contentious issue to say the least! But there is nothing wrong with having Flash elements in certain places on your website; PHP can help with this! To these files you would use the Ming library to create Flash files in .swf format. With this library, you can generate movies, text, and even animations in Flash!
You've just learned fifteen creative uses of PHP which you can use to improve your website or just have fun with - enjoy them all!
September 30th, 2007 at 3:33 am
Very interesting! Thanks for sharing that!
September 30th, 2007 at 6:24 am
Dustin, thanks for a great tip!
It seems that you forgot to make a poll in handleReadyState definition in your updated code, as it would be called only once which is incorrect.
September 30th, 2007 at 7:13 am
I really liked your technique and so wanted to check if the popular JavaScript libraries are also using it. I just looked at jQuery1.1.4 and it just had a one-liner.
Any problem with this?
September 30th, 2007 at 8:30 am
I noticed that problem could occur with the example you gave, but never really thought it through properly to come up with an alternative. Really nice solution :)
September 30th, 2007 at 12:34 pm
@Mourner: I removed the poll because that was an outdated piece of code that assumed IE6 leaked memory. Since then IE has fixed that.
@Bhabishya: Yes, there is… that piece of logic is run upon every request. And not only that, it doesn’t take advantage of IE’s newer methods for XMLHTTP such as MSXML2 and XMLHTTP.3.0.
@Graham: Yeah. It doesn’t seem to bother a lot of people. But working at Google has tweaked my mind to always think about speed and optimization techniques, hence this blog post :
September 30th, 2007 at 2:18 pm
yes, thats true
changing workplaces often changes the thinking to
ps: nice to read something new on your blog :)
September 30th, 2007 at 4:26 pm
[...] Making Ajax Elegantly Fast (tags: ajax javascript fast web development webdev) This entry was written by delicious and posted on 1 Ottobre 2007 at 01:25 and filed under Del.icio.us. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL. [...]
September 30th, 2007 at 11:55 pm
Hey Dustin, thanks for the informative article. I’m left with three questions though that I hope you’ll be able to shed some light on.
First, not that it’s a big deal, but what’s the point of the ‘return http;’ line in your original getXHR function? Since you are redefining the function anyway, it doesn’t seem like it would serve a purpose. However, since that wasn’t in your original writeup, I’m assuming you must have added it for a reason.
Second, why don’t you test for XMLHttpRequest using a !! flag instead of a try / catch block? While the ‘catch’ block for the way you’re doing it would only get hit once in IE6, that’s once more than it needs to be hit it seems. The performance gain would probably be just a few milliseconds or so, and it’s maybe not even worth discussing, but I figured I’d ask anyway.
Finally, is there a reason you chose to use Michaux’s pattern when you rewrote this instead of your original pattern based on function closures (not sure what else to call it really). For example, is there a reason you did this
instead of this
? As some who wrote a book on the subject of JavaScript patterns, I’m especially curious to hear more about this one from you.
I’m honestly not trying to knock your example, nit-pick at things, or say anything you did was wrong. I’m just really curious to learn more about why you did what you did.
October 1st, 2007 at 1:00 am
@Andrew:
1) http is returned so you always have the object incase you want to abort or grab other properties. In other words… you can do this…
2) and 3) Actually, I don’t know why I didn’t use my usual closure technique (which is always my favorite). We can take it a step further and use a ternary operator to decide which function is assigned to getXHR…
October 1st, 2007 at 3:19 am
Something I do not understand.
1. You remove the poll in the handleReadyState function, and you say it may cause the ie to leak memory, but without the poll the asyncRequest can not get any response, have you test it by yourself this function?
2. Code problem:
it is:
3. I do not like your lazy-like getXHR function implementation, below is my implementation:
I believe it is much more reasonable and readable, and maybe this optimization is only for the stupid internet explorer 6.
4. You add checking the status 200 in the handleReadyState function, but what about the local file system, and the file protocol, maybe “o && o.readyState == 4 && (o.status == 200 || o.status == 0)” would be better.
Last: this blog is not quite convenient, First time I do not input the e-mail address and it tell me an error, and I returned to this page, and the the textarea has been cleared……. Sorry for posting again, I find last post has some problem please delete it, and this blog lack of the preview feature.
October 1st, 2007 at 3:24 am
And this is my getXHR function:
Sorry for posting again, the HTML entities………….
October 1st, 2007 at 4:29 am
I use often this technique,
but be careful because if you use closures AND you get a Dom node in the factory function you leech some memory on IE.
In your case the problem don’t exists.
October 1st, 2007 at 8:12 am
For clarification, MSXML2.XMLHTTP.3.0, MSXML2.XMLHTTP, and Microsoft.XMLHTTP all map to the same object internally: “MSXML2.XMLHTTP.3.0″. (that is, on any currently supported Internet Explorer platform from Windows 2000 and up) Checking for each version as you are is unnecessary. The one version you should add is “Msxml2.XMLHTTP.6.0″ which is the version that ships with IE7 and would be used if somebody turned off the native XHR object (not a likely scenario but seems like the best way to handle it).
October 1st, 2007 at 9:38 am
@Snook:
xmlhttp 6.0 can be installed as stand-alone so even an IE6 machine could use it.
there is a urban legend out there that say that one of the 2 {MSXML2.XMLHTTP, and Microsoft.XMLHTTP} map to the LAST msxml installed on a machine, i don’t know if this is true.
For what I know microsoft.* is the OLD namespace and MSXML2 the new one.
October 1st, 2007 at 10:37 am
[...] http://www.dustindiaz.com/faster-ajax/ [...]
October 1st, 2007 at 1:06 pm
@Dustin > @Andrew
I think both implementations work but for readability I prefer Dustins version…
And about the namespaces, the MS XML Team has this to say:
http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
Which apparently results in using only: Msxml2.DOMDocument.6.0 and Msxml2.DOMDocument.3.0
October 1st, 2007 at 1:08 pm
crap… those are the DOMdocument objects… delete the comment Dustin… this’ll only create confusion… :s
October 1st, 2007 at 1:54 pm
[...] Elegantly Fast Ajax - I dig it. Share: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]
October 1st, 2007 at 3:29 pm
[…] Making Ajax Elegantly Fast (tags: ajax javascript fast web development webdev) This entry was written by delicious and posted on 1 Ottobre 2007 at 01:25 and filed under Del.icio.us. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL. […]
October 1st, 2007 at 3:30 pm
First, not that it’s a big deal, but what’s the point of the ‘return http;’ line in your original getXHR function? Since you are redefining the function anyway, it doesn’t seem like it would serve a purpose. However, since that wasn’t in your original writeup, I’m assuming you must have added it for a reason.
Second, why don’t you test for XMLHttpRequest using a !! flag instead of a try / catch block? While the ‘catch’ block for the way you’re doing it would only get hit once in IE6, that’s once more than it needs to be hit it seems. The performance gain would probably be just a few milliseconds or so, and it’s maybe not even worth discussing, but I figured I’d ask anyway.
October 1st, 2007 at 5:51 pm
[...] Making Ajax Elegantly Fast - [...]
October 1st, 2007 at 8:24 pm
[...] Making Ajax Elegantly Fast Uma função para requisições Ajax rápida e elegante (tags: development javascript web ajax) [...]
October 2nd, 2007 at 1:12 am
[...] Making Ajax Elegantly Fast [...]
October 2nd, 2007 at 3:30 am
I’m not gonna pretend like I know anything but after reading “Peter’s Blog” I got the impression that you should use
return getXHR();and nothttp.Or did I miss anything?
October 2nd, 2007 at 4:25 pm
[...] Making Ajax Elegantly Fast (tags: ajax javascript webdev) [...]
October 3rd, 2007 at 1:34 am
very great info ^^! thanks for sharing Dustin.
Much appreciated!
October 3rd, 2007 at 8:01 am
[...] Visto en | Optimized Speedy Ajax Code y en Making Ajax Elegantly Fast [...]
October 3rd, 2007 at 12:01 pm
@Mats Lindblad:
Yes, you are righ. The http variable is redundant, you could remove the “http = new XMLHttpRequest;” and “http = new ActiveXObject(msxml[i]);” lines and simply do “return getXHR();”.
Also note that the code on this page is only good for testing on a local machine where there is no latency in the request.
This only processes handleReadyState once, so if readystate == 1 the first time (ie, the request has not been completed between the first and second lines of code in the snippet above), then the callback will never be called.
There is also no mechanism to handle a failed request.
October 3rd, 2007 at 4:54 pm
[...] Dustin Diaz has revisited his seven JavaScript techniques and has updated his XHR-getting-function to be faster, using the Lazy Function Definition Pattern, which ends up looking like: PLAIN TEXT JAVASCRIPT: [...]
October 3rd, 2007 at 8:37 pm
[...] Faster Ajax posted by admin at 8:37 pm [...]
October 3rd, 2007 at 11:44 pm
here is another way you could define
getXHR:When it is first called, the function will unshift through
paramsuntil it finds a working constructor/argument pair. This becomes the default value.October 4th, 2007 at 12:02 am
Actually, I would amend the previous comment:
October 4th, 2007 at 4:32 am
[...] Dustin Diaz shares some cool tips on making Async JavaScript (Ajax) work faster on the client-side using the Lazy Function Definition Design Pattern. Entry Filed under: Technology, .NET Web Dev, Linklog [...]
October 4th, 2007 at 2:39 pm
“Dustin: Yes, there is… that piece of logic is run upon every request. And not only that, it doesn’t take advantage of IE’s newer methods for XMLHTTP such as MSXML2 and XMLHTTP.3.0.”
You’re kidding, right? A boolean check for the existence of a property is worse than using closured function to wrap object creation? Bwah?
As far as the XMLHTTP vs. other modules goes, I’ve discussed it with the IE team and they agreed that referencing a single version of the XMLHttpRequest code would reduce the number of inter-browser-version bugs and inconsistencies, especially considering that the newer versions don’t provide any tangible benefits in performing the requests. Thus, the code is significantly simplified.
October 5th, 2007 at 11:28 am
Good read, I’ve been trying to learn a little more js on my own lately until my computer fried. Hopefully I’m gonna get back into it soon and I can work on my AJAX a little bit too. Thanks for the post
October 5th, 2007 at 12:03 pm
Making AJAX fast this way, slows page loading (a tiny bit). In terms of performance smaller scripts usually beat cleverer ones, due to time-to-download factor.
October 8th, 2007 at 9:36 am
I faced this problem almost one year ago and I solved in a similar way: implementing a new Object and using an Array to collect all the Objects. Then each use of the callback could be pointed to the relative Object in the Array.
Obviously my code was not so elegant! :D
October 9th, 2007 at 3:05 am
[...] Dustin Diaz has revisited his seven JavaScript techniques and has updated his XHR-getting-function to be faster, using the Lazy Function Definition Pattern, which ends up looking like: [...]
October 13th, 2007 at 12:26 pm
Sorry, your post is another bad example. Besides many issues some comments here pointed before, Lazy Func Def is totally a ANTI-PATTERN. I’ve left some comments on Peter’s article, you should read them.
You only use getXHR() internally, so it will not as bad as Peter’s and FredCK’s examples. But it still no sence, because you can write the code more clearly and memory leak free (for example, henrah’s version, though it still can be imporved).
Finally, about performance, in fact, all versions here are just same in practice (if we don’t consider the bugs in some of them), because compare to network IO, the cost of object initilization and exceptions can be ignored at all.
October 17th, 2007 at 3:41 pm
This doesn’t seem to work as described.
It fetches the file but never fires the callback func.
You only seem to check the readystate once.
To make it work I changed:
handleReadyState(http, callback);
to:
http.onreadystatechange = function(){handleReadyState(http, callback)};
Or maybe I’m missing something?
October 21st, 2007 at 8:34 pm
Hi , i know that im going to sound really special for asking this, but how do i actually use your solutions function?
what variable holds the returned data?
thank you in advance!
October 22nd, 2007 at 2:54 am
I have rewritten the code a bit to work with our site:
And then call it like this:
I’m not sure if this will work as you use a return function, and i stripped that out.
Do you see any problems with this?
October 29th, 2007 at 9:31 pm
[...] Making Ajax Elegantly Fast (tags: web2.0 programming ajax) [...]
November 29th, 2007 at 3:38 pm
[...] Making Ajax Elegantly Fast - This article uses the Lazy Function Definition Pattern to reduce the time need to initialize an [...]
December 1st, 2007 at 6:29 pm
[...] to calling these requests. The first thing you’ll need is an asyncRequest function. As well, I wrote one of these just a bit ago. It works like [...]
December 3rd, 2007 at 4:37 am
Dear Dustin,
isn’t
o.onreadystatechange = function() {}necessary beforeif (o && o.readyState == 4 && o.status == 200)for callback to work as intended?Tom
December 5th, 2007 at 7:13 pm
hi dustin - where are you? there have been some fairly significant issues raised here, i’m curious if you have any response.
what the hell…!? this whole “blog” idea, i just don’t know if it is any good. cancel everything!
December 11th, 2007 at 2:18 pm
Hi
How can I send post data using this function?
I tried but it doesn’t work.
I make it work adding:
http.setRequestHeader(”Content-type”, “application/x-www-form-urlencoded”);
http.setRequestHeader(”Content-length”, postData.length);
http.setRequestHeader(”Connection”, “close”);
before the http.send();
is it ok?
Thanks!
December 13th, 2007 at 12:33 pm
@Pablo
You would write:
asyncRequest('POST', 'foo.php', callbackFunction, postData);
where callbackFunction is function you wish to call to deal with response text and postData are form field values you wish to send to foo.php (postData looks like: field1=value1&field2=value2…)
December 19th, 2007 at 2:42 am
[...] Diaz podjął temat optymalizacji połączeń przez XMLHttpRequest w artykule pt. Making Ajax Elegantly Fast. (Brak głosów) Loading [...]
December 30th, 2007 at 2:58 pm
I put it in myself and it works! :)
http.onreadystatechange = function () { handleReadyState(http, callback); };