Very useful 9 PHP code snippets, _ PHP Tutorial

Source: Internet
Author: User
9 Very useful PHP code snippets ,. 9 Very useful PHP code snippets. This article will share some of the most useful PHP code snippets I have collected. Let's take a look! 1. create nine PHP code snippets that are useful for embedding a data URI,

In this article, we will share some of the most useful PHP code snippets I have collected. Let's take a look!
1. create a data URI

Data URI is useful when embedding images into HTML, CSS, and JS to save HTTP requests and reduce the loading time of websites. The following function creates a data URI based on $ file.

function data_uri($file, $mime) { $contents=file_get_contents($file); $base64=base64_encode($contents); echo "data:$mime;base64,$base64";}

2. Merge JavaScript and CSS files

Another good suggestion for minimizing HTTP requests and saving page loading time is to merge your CSS and JS files. Although I suggest using specialized plug-ins (such as minify), it is also very easy to use PHP to merge files. Let's take a look:

Function combine_my_files ($ array_files, $ destination_dir, $ dest_file_name) {if (! Is_file ($ destination_dir. $ dest_file_name) {// continue only if file doesn't exist $ content = ""; foreach ($ array_files as $ file) {// loop through array list $ content. = file_get_contents ($ file); // read each file} // You can use some sort of minifier here // minify_my_js ($ content); $ new_file = fopen ($ destination_dir. $ dest_file_name, "w"); // open file for writing fwrite ($ new_file, $ content); // write to destination fclose ($ new_file); return'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.