My PHP Code-Standards Part 3

Always check the value before using it.
This way you can make sure the value is valid.

  1. $intPageNumber = (isset($_REQUEST[‘pnumber’]) and is_nummeric($_REQUEST[‘pnumber’]) ) ? $_REQUEST[‘pnumber’] : 0 ;

If you know the choices, do not use it directly. Use an array instead.

  1. $intPageNumber = (isset($_REQUEST[‘pnumber’]) and is_nummeric($_REQUEST[‘pnumber’]) ) ? $_REQUEST[‘pnumber’] : 0 ;
  2.  
  3. $arrPName = array(0 =>‘zero’ , 1=>‘one’ , 2=>‘wrong number’, 666=>‘hmm’ );
  4.  
  5. $strReturn = ( isset($arrPName[$intPageNumber] ) )? isset($arrPName[$intPageNumber] : ‘-0-0-’;
  6. echo $strReturn ;

.htaccess Deflate

To have your files deflated in apache you can add these lines to your .htassecc file.
Deflating files helps keeping transferring size small. Apache automatically pack the files and your browser unpacks it but the you can save lots of MB a year on transfer size. And it helps the site to load faster on slow connections. This is almost like gziping but deflating method is less CPU aggressive on apache server compared to gzip.

  1.  
  2. AddOutputFilterByType DEFLATE text/plain
  3. AddOutputFilterByType DEFLATE text/html
  4. AddOutputFilterByType DEFLATE text/xml
  5. AddOutputFilterByType DEFLATE text/php
  6. AddOutputFilterByType DEFLATE text/css
  7. AddOutputFilterByType DEFLATE application/xml
  8. AddOutputFilterByType DEFLATE application/xhtml+xml
  9. AddOutputFilterByType DEFLATE application/rss+xml
  10. AddOutputFilterByType DEFLATE application/javascript
  11. AddOutputFilterByType DEFLATE application/x-javascript

.htaccess Expiredate

To put expiredate on your files you can put these lines to your .htaccess file.
It reduses loading time for your site.

  1. ExpiresActive On
  2.  
  3. ExpiresByType image/png "access plus 20 days"
  4. ExpiresByType image/jpg "access plus 50 days"
  5. ExpiresByType image/jpeg "access plus 50 days"
  6. ExpiresByType image/ico "access plus 10 month"
  7. ExpiresByType image/gif "access plus 30 days"
  8. ExpiresByType text/javascript "access plus 8 days"
  9. ExpiresByType application/javascript "access plus 8 days"
  10. ExpiresByType text/css "access plus 8 days"

.htaccess gzip

To put gzip on files from apache server you have to add there lines to your .htaccess file.

  1.  
  2.   mod_gzip_on Yes
  3.   mod_gzip_dechunk Yes
  4.   mod_gzip_item_include file \.(html?|txt|css|js|php)$
  5.   mod_gzip_item_include handler ^cgi-script$
  6.   mod_gzip_item_include mime ^text/.*
  7.   mod_gzip_item_include mime ^application/x-javascript.*
  8.   mod_gzip_item_exclude mime ^image/.*
  9.   mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

All rights reserved. © Copyright 2012.
Powered by WordPress   Design by swape.net