background preloader

43 Ways to Optimize your PHP Code

43 Ways to Optimize your PHP Code

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:

PHP Optimization Tips | ProgTuts This tutorial will explain many small techniques which will, hopefully, help optimize your php scripts. I considered myself a bit of a PHP pro until I started researching some of this stuff and realized that there is a whole realm of information out there about optimizing php that I didn’t know about. I hope you will be as surprised as I was about some of the things you might learn from this article. Output of Data So first off lets start with outputting data to the user. Here are some handy tips to remeber: echo is faster than print (cite) When outputting strings: Single quotes (’) with concatenation is faster than putting your variables inside a double quote (”) string. Or even better: Use echo’s multiple parameters instead of string concatenation. Loops and Counting Here are some ways to make your loops and counting a bit more efficient. Use pre-calculations and set the maximum value for your for-loops before and not in the loop. When checking the length of strings: When incrementing: And

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. 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 Some websites are able to detect your location and automatically display information related with your language. Cache Manager WPGet

15 very useful PHP code snippets for PHP developers | ViralPatel.net 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. 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. 12. 13. 14. 15.

Twig - The flexible, fast, and secure template language for PHP 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;

Getting Started with CRUD In PHP It has become a common necessity for website owners to collect data and manage it properly. Creating a MySQL CRUD class allows you to conveniently create, read, update and delete entries for any of your projects, indifferent of how the database is devised. CRUD allows us to generate pages to list and edit database records. So, in this tutorial I will show you how to build a simple CRUD web app, that will empower you with the basic functions of database management. In this article I will discuss following things:- • Creating the database • Creating the table • Make connection to the database • Insert records in table • Update records table • Delete records from table The tools I will be using are:- • XAMPP • Dreamweaver CS5 (you can use Notepad++ or any other IDE) Before continuing with the tutorial I would like to discuss the following definitions: • What is CRUD? What is CRUD? CRUD stands for create, read, update, delete. What is database? What is table? What is XAMPP? Code of Index.php file

Json serialization/deserialization faster than protocol buffers? I was reading the The Book of JOSH and saw the following statement: “Json delivers on what XML promised. Simple to understand, effective data markup accessible and usable by human and computer alike. Serialization/Deserialization is on par with or faster then XML, Thrift and Protocol Buffers.” That seemed a bit too definite for my taste. There are so many variables that can affect the results that I was interested in more information, so I asked for it and eventually got an answer. I had a brief look at the benchmark referenced and that was enough to come up with some talking points. Ok, so json doesn’t seem to be faster on deserialization and the size is almost 50% bigger (a big deal if the network is the bottleneck as is often the case). How about we replace that with content.toByteArray()? That’s more like it. That reduces some of the cost of object creation for protobuf, but it’s still substantially slower than json. What does this mean? Like this: Like Loading...

Related: