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.

What's the Right Way to Prevent SQL Injection in PHP Scripts? - Brian Swan How to prevent SQL injection in PHP scripts is probably a topic that doesn’t need anything more written about it. It is pretty easy to find blog posts, documentation, videos, etc. that explain the importance of preventing SQL injection and suggestions for preventing it. In fact, I’ve already written a post on this topic as a guest writer on the SQL Server Driver for PHP team blog. However, it is important to have fresh information for new Web developers and I don’t necessarily agree with some of the most common suggestions for preventing SQL injection. (Besides, I hear that this is the Month of PHP Security.) So, this will be yet another post about preventing SQL injection, but I will offer my 2 cents about what I think is the right way to prevent it. SQL injection (or a SQL injection attack) occurs when a user provides SQL code as user input for a Web page, and the SQL code is then executed in the database. Now consider the form with the following inputs: Thanks. -Brian

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

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

Still life: Bent objects » Article » OWNI.eu, Digital Journalism UPDATE: The Return of Bent Objects Wires transform these objects from inanimate to hilarious works of art. Little polish girl McDonalds as Sculpture Materials Yeah, this is where those come from Dancing Queens English breakfast Sylvia Muffin put her head in the oven. The introvert Bananas in bed – let’s slip into bed together You Say Tomato, I Say Tomahto. Fruit with life experience Zombies are nuts about brains Modest pear Literary interpretations Paper training our little dog, Frank A little cat doodle Photo Credits: Terry Border at Bent Objects View more In Pictures sets on Owni.eu Twig - The flexible, fast, and secure template language for PHP

PDF Generation with Zend Framework | LiveDocx in PHP - Zend_Service_LiveDocx Generating print-ready well-formatted PDF documents with PHP is not an easy task. Traditionally, there are two main approaches to PDF generation with PHP. Given sufficient time and patience, both partially get the job done, but still leave a lot to be desired: HTML-to-PDF: This approach is widely used in mainstream applications. Programmatic: This approach offers total control of the resulting PDF. A completely new approach In this article, the author presents an entirely new, third approach. Before we delve into a technical discussion on the inner workings of this new approach, let us first take a look at a practical example. The code demonstrated in this article ships with Zend Framework 1.10 or newer. Introducing LiveDocx LiveDocx is a SOAP-based document generation service, based on the market-leading, word processing component TX Text Control .NET. Templates and documents Throughout this article, we refer to the terms templates and documents. Using LiveDocx and Learn more Footnotes

Object passing (with and without serialization) in php - This article will show you how to handle in a proper way a very common situation: a user logs in your site and you want to keep trace of his details while he browses around your site using just one Imagine you have done a site (an e-commerce one e.g.) and you have a nice login page. Your users will insert their username and password and if the data they insert is correct, they are redirected e.g. to the catalog page where they can buy your products. Many of us do this almost everyday buying stuff from many different sites. Ok, how would you handle variable passing? // we have surely remembered to put // session_start(); // at the beginning of our .php page // process user input here if (user has inserted correct vals) { // retrieve firstname, lastname, id, username from database and store them // retrieving code here $rs=mysql_fetch_array($qry); session_register('username'); session_register('firstname'); session_register('lastname'); session_register('id'); $username=$rs['username']; $firstname=$rs['firstname']; $lastname=$rs['lastname']; $id=$rs['id']; // now in every page where we put session_start() we're able to access this 4 vars } ? Nice huh? Very simple class. ?

9 Tips For Working with MySQL Databases MySQL is one of the most popular relational database management systems (RDBMS) around, with over 6 million installations. It’s a fantastic choice for any new developer, because of its open source nature, wide support and abundance of tutorials available on the subject. Here are some great tips and tools (beside phpMyAdmin) to improve your MySQL coding, and to help you save time. Installing SQL If you’re a newbie to the server side coding arena, don’t worry. Mac only PHP, SQL & Apache Installer Windows, Linux & Mac installer A windows only LAMP installer MySQL Server 5.1 If you just want MySQL server, and like using command line then this might suit you. For info on how to install and work with the MySQL server, check this NetTuts tutorial: Everything You Need to Get Started With MySQL. SQL Tips 1- Optimize your code Imagine if you have 100 records, 10 columns.Your working with a database of your clients details, and need to select their name, phone number and address. SELECT * FROM clients;

Top 20+ MySQL Best Practices Database operations often tend to be the main bottleneck for most web applications today. It's not only the DBA's (database administrators) that have to worry about these performance issues. We as programmers need to do our part by structuring tables properly, writing optimized queries and better code. In this article, I'll list some MySQL optimization techniques for programmers. Before we start, be aware that you can find a ton of useful MySQL scripts and utilities on Envato Market. Most MySQL servers have query caching enabled. The main problem is, it is so easy and hidden from the programmer, most of us tend to ignore it. The reason query cache does not work in the first line is the usage of the CURDATE() function. Using the EXPLAIN keyword can give you insight on what MySQL is doing to execute your query. The results of an EXPLAIN query will show you which indexes are being utilized, how the table is being scanned and sorted etc... After adding the index to the group_id field:

Related: