Monday, 24 November 2014

Expire Header | Cache-Control Header | Best Practice to Speed up your website

There are two aspects to this rule: For static components: implement “Never expire” policy by setting far future Expires header For dynamic components: use an appropriate Cache-Control header to help the browser with conditional requests For rich interface websites for first-time visitor to your page may have to make several HTTP requests, but by using the Expires header you make those components cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images, but they should...

PHP array search

Search in array Somtimes you have a quite big array which is filled up from a file or code. In this case you don't know whether a given value exists in the array or not. And if it exists then it would be fine to get the key of this element. Let's suppose you have the following array:  Code: $colorList = array("apple"=>"red", "grass"=>"green", "sky"=>"blue", "night"=>"black", "wall"=>"white"); Now if you want to know whether...

PHP heredoc syntax

PHP heredoc syntax Heredoc is a robust way to create string in PHP with more lines but without using quotations. Heredoc is rarely used as the day by day usage is more complicated as creating strings with quotes or double quotes. Besides this the not properly used heredoc can lead to problems in your code. However if you want to use it you can do it in the following way: Code: <?php $str = <<<DEMO This is a demo message with heredoc. DEMO;   echo $str; ?> Output: This is a demo message...

PHP file write

Write into a file with PHP In cases when you want to store some data, log user activity or anything else you may want to store this information in a file. To do this there are 3 basic steps you need to do: Open the file Write your data Close the file Step 1. If you want to write into a file you need to open it for writing. You have 2 possibilities depending on what you need: w - Truncate the file so all data will be lost. If the file does not exist, attempt to create it.   a - Writes to the end of the existing file. If the...

PHP file read

PHP file read methods PHP file read There are many different ways how you can read the content of a file. Step 1.First of all you need to open the file for reading using the fopen function. You need to provide the filename as parameter and you also need to define to open mode which is in case of reading the 'r'. Before any real reading begins you also need to check if file opening was success. Code: $fileName = 'test.txt'; $fileRes = fopen($fileName,'r'); if ($fileRes === FALSE) { echo 'Can...

PHP in_array function usage examples

In cases when you want to check whether a value exists in an array you can use the in_array function. This function checks if a value exists in an array. You can use it from PHP version 4. The syntax is the following: bool in_array ( mixed $what , array $where [, bool $strict = FALSE ] ) By default you simply use only the 2 mandatory parameters ($what and $where) as you can see in the following example: Code: $myArray = array("Audi","BMW","Lexus","Mercedes");   var_dump($myArray);   if...