background preloader

15 very useful PHP code snippets for PHP developers

15 very useful PHP code snippets for PHP developers
Following are list of 15 most useful PHP code snippets that a PHP developer will need at any point in his career. Few of the snippets are shared from my projects and few are taken from useful php websites from internet. You may also want to comment on any of the code or also you can share your code snippet through comment section if you think it may be useful for others. 1. Send Mail using mail function in PHP 2. 3. The above code will not work in case your client is behind proxy server. 4. This function will return the duration of the given time period in days, hours, minutes and seconds. e.g. secsToStr(1234567) would return “14 days, 6 hours, 56 minutes, 7 seconds” 5. 6. Required Extension: SimpleXML 7. 8. Following is the PHP code to create the JSON data format of above example using array of PHP. Following code will parse the JSON data into PHP arrays. 9. 10. This basic snippet will create a random authentication code, or just a random string. 11. Validate a date in “YYYY-MM-DD” format.

10 code snippets for PHP developers « The html blog I’ve compiled a small list of some useful code snippets which might help you when writing your PHP scripts… Email address check Checks for a valid email address using the php-email-address-validation class. Source and docs: 01.include('EmailAddressValidator.php'); 03. 04.if ($validator->check_email_address('test@example.org')) { 07.else { Random password generator PHP password generator is a complete, working random password generation function for PHP. 01.function generatePassword($length=9, $strength=0) { 02. 03. 04. if ($strength & 1) { 05. 07. if ($strength & 2) { 08. 10. if ($strength & 4) { 11. 13. if ($strength & 8) { 14. 17. 18. 19. for ($i = 0; $i < $length; $i++) { 20. if ($alt == 1) { 21. 22. 23. } else { 24. 25. 28. return $password; Get IP address Returns the real IP address of a visitor, even when connecting via a proxy. 01.function getRealIpAddr(){ 02. if (! 04. 06. elseif (! 08. 10. else{ 11. 13. return $ip; XSL transformation 01. 04. 05. 08.

Find broken links on your site with Xenu's Link Sleuth (TM) Find broken links on web sites Wikipedia article about Xenu's Link Sleuth Description Xenu's Link Sleuth (TM) checks Web sites for broken links. Additional features: Simple, no-frills user-interfaceCan re-check broken links (useful for temporary network errors)Simple report format, can also be e-mailedExecutable file smaller than 1MBSupports SSL websites (" ")Partial testing of ftp, gopher and mail URLsDetects and reports redirected URLsSite Map Download System requirements: Microsoft Windows 95/98/ME/NT/2000/XP/Vista/7/8/10, WININET.DLL required (included with Internet Explorer). To have peace of mind, I recommend that you are using an up-to-date anti-virus software on your computer (so do I!). Ok, I have read all that, I want to download! Join the Update Announcements mailing list at Yahoo Groups! You can also join the user group by sending an e-mail to xenu-usergroup-subscribe@yahoogroups.com. The address of this web page is 1. See here. 2.

7 Secure, Lightweight, and Easy to Use PHP Frameworks Choosing a good PHP frameworks can help you develop complex Rich Internet Applications quickly, with a best practices oriented approach, and saving a lot of time reusing code snippets that are already available. There are a lot of interesting PHP frameworks you can choose for your next web project. Which framework you choose is really a personal decision. There is no one “best” framework on the market at the moment. Different frameworks are better for different types of projects, and for different developer. Today we will focus on 7 secure, lightweight and easy to use PHP Frameworks. Why Use a PHP Framework? There are plenty of reasons to use a PHP Framework to build your websites. 1. Below we’ve covered some of the best frameworks available today. 1. CodeIgniter is a powerful, high-performance, open-source PHP framework that helps you author PHP applications rapidly. CodeIgniter has an exciting online manual, a couple of helpful video tutorials and an active user forum. 2. 3. 4. 5. 6. 7.

extremely useful PHP classes PHP PSD Reader A few weeks ago, I wrote an article about this PHP which allow you to display any Adobe PSD file on screen. Very usefull to create preview of PSDs designed for clients, for example.Download Browser detect One of the most common (and boring) problem for front-end developers is definitely cross-browser compatibility. This PHP class will detect almost all browsers and simplify your cross-browser work.Download Akismet Remember those days without spam? ADOdb The large majority of websites and web apps are using databases to store all kinds of data. HTML Purifier As it name tells, HTML Purifier is a PHP class created to help you writing a better code. Google charts API Charts are very useful and highly asked by clients, but they can be a lot of work. pChart pChart is another chart class, and it is as good as Google charts API. PHP Excel Excel documents are highly popular in the corporate world. Country from IP Cache Manager WPGet

sample PHP page I'll start out by explaining some of the things I don't like about PHP. If I get this all out of the way, you can get to the useful stuff. I'll use this opportunity to say something instructive about how to use the language, based on its strengths and weaknesses, though, so it's not wasted time entirely. 1. 2. 3. 4. 5. 6. 7. 8. 9. For a first "assignment" to practice your incipient PHP skills after reading this tutorial, try writing a script that generates a webpage with this text file as the displayed content, formatted so that it is recognizable, readable, and clear on-screen. Note that some of what I say here may seem incomplete to an experienced PHP programmer. ##### embedding in XHTML: PHP is easily embedded in XHTML (or HTML) via some special script tags that identify their contents as PHP code. ##### comments: Comments can be indicated by the methods common to C, C++, and shell scripting. ##### variables and values: All variable names start with a dollar sign. ##### output:

Comparer le temps de chargement de deux sites Mardi 7 décembre Web - 7 décembre 2010 :: 07:38 :: Par Camille Je ne pense pas que je vais vous l’apprendre : le temps de chargement des pages fait partie depuis plusieurs mois des critères de pertinence du moteur de recherche de Google. Un nouveau service intitulé Which loads faster vient de voir le jour, il permet de comparer le temps de chargement de deux sites web. Cet Je ne pense pas que je vais vous l’apprendre : le temps de chargement des pages fait partie depuis plusieurs mois des critères de pertinence du moteur de recherche de Google. Cet outil open-source a été développé en HTML et en JavaScript, et le code s’exécute coté client pour éviter d’avoir un temps de réponse coté serveur. Une fonctionnalité permet de comparer en boucle (5, 10, 100 fois…) et de retourner la moyenne du site le plus rapide à charger. Sans oublier les fonctions de partage et d’exportation au format CSV qui seront bien utiles pour vos comparatifs.

Simple Layouts with PHP This tutorial is for those starting out in PHP and want to learn a great way to create layouts. This will help reduce redundancy and keep you from writing too much code. Download the Source Anyone that has been developing HTML based sites for some time has undoubtedly ran in to the issue of keeping the design consistent across multiple pages. Technique #1 A lot of sites follow this kind of layout scheme The first technique is the entry level solution that works across nearly all situations. <! Take out everything up to the point and put that in a new file called head.php. <? Now just put whatever HTML/PHP content you want in between those two include statements and check it out in a browser. It may be easier to think of it as a puzzle, you are piecing together a complete HTML page with includes. Technique #2 Alright we have that under our belt let’s look at another solution. <! This will be your index.php, but the content will change based on the links the user goes to. Conclusion

Twig - The flexible, fast, and secure template language for PHP PHP command line syntax checking Posted April 24th, 2010 in PHP The PHP CLI (command line interface) allows basic syntax checking of a PHP file. I'll show the basic usage of this in this post and then a couple of ways to check all files in a directory, and all files recursively down the directory tree as posted by a couple of people on Twitter. Syntax checking a single PHP file from the command line The syntax for checking is like this, where the -l flag is a lower case L: If the syntax in the file is correct you will see this: If there are errors then theses will be listed as in the following example output: Note that the syntax checking only checks for syntax errors - it does not report the usage of variables which have not yet been defined, functions or class which have not be declared etc. So something like this will pass: but these will not: Syntax check all PHP files in the current directory

Entri – Outil de création de contenu web collaboratif par Sylvain Entri est un petit service en ligne gratuit qui permet de rédiger du contenu à plusieurs instantanément sur une seule et même interface pour tous les collaborateurs. Avec Entri, d’un simple clic, vous allez pouvoir avoir un éditeur de texte collaboratif pour de la création de contenu à plusieurs. En effet, vous partagez l’URL qui vous est alloué par Entri avec vos amis, collaborateur, autres et ainsi, chacun va pouvoir mettre sa pierre à l’édifice. Plusieurs service du genre existe et sont tout aussi gratuit et, parfois, proposent bien plus de fonctionnalités. Cependant, Entri est orienté web, c’est à dire que lorsque votre article est terminé, un clic sur « Get HTML » et vous aurez un code HTML de votre contenu qu’il vous restera plus coller sur votre site. à découvrir ici:

Related: