Archive for the ‘php’ Category

MongoDb Introduction

April 7, 2016 Leave a comment

MongoDB Databse-
MongoDB is NoSql and document oriented database. MongoDB stores data using a flexible document data model that is similar to JSON.

Create Database-
>use yaju;

Show Database-
>show dbs

Drop Database-
To drop database, first use that database then use following command.

Create Collection-
Collections are like table in MySql. We can create collection in two ways.

First, we can create empty collection

Second, we can create dynamic collection with insert command. Mongo check that collection if collection is exists then insert data in existing collection otherwise create new collection and insert data. That can be through followning command.

Show collections-
Show all collection in a database use following command.
>show collections;

Insert document-
We can insert single document and multiple document as well.

Single document
Multiple document
>db.testtable.insert([{‘name’:’yajuvendra’},{‘name’:’yaj’}, {‘name’:’yaju’},{‘name’:’yajkumar’}]);

Select document-
Select all document
Select particular document
> db.testtable.find({‘name’:’yaj’});

Remove document-
> db.testtable.remove({‘name’:’yaju’});

Update document-
> db.testtable.update({name:’yaj’},{$set:{name:’yaju_update’}})

Speed up site with .htaccess

January 20, 2015 1 comment

Append these line in your .htaccess file.

#Trun on compression

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

#Turn on content caching

ExpiresActive On
ExpiresByType image/jpg “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType text/css “access plus 1 month”
ExpiresByType application/pdf “access plus 1 month”
ExpiresByType text/x-javascript “access plus 1 month”
ExpiresByType text/javascript “access plus 1 month”
ExpiresByType application/javascript “access plus 1 month”
ExpiresByType application/x-shockwave-flash “access plus 1 month”
ExpiresByType image/x-icon “access plus 1 month”

ExpiresByType application/font-woff “access plus 1 month”
ExpiresByType application/ “access plus 1 month”
ExpiresByType application/x-font-ttf “access plus 1 month”
ExpiresByType font/opentype “access plus 1 month”
ExpiresByType image/svg+xml “access plus 1 month”

ExpiresDefault “access 2 days”
Header set Cache-Control “public”
Header unset Cache-control

Header append Vary: Accept-Encoding
Header unset Pragma

#Mod_deflate is an apache module that can be used to compress data using gzip compression before sending it to the browser

AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/x-font-ttf


December 20, 2014 Leave a comment

OPcache is bundled with PHP 5.5.0 and later, and is available in PECL for lower PHP version.
OPcache improves PHP performance. OPcache stores precompiled script bytecode in shared memory on first request. When next request comes then PHP does not compile that script, it uses precompiled script from OPcache.
cache opcache_reset() OPcache offers a function to reset.
pcache_invalidate ( string $script [, boolean $force = FALSE ] ) This function invalidate a script.

Enable OPcache on linux: To enable OPcache on linux uncommment or add this line in php.ini if this not enable OPcache on server then put full path of

OPcache configuration: You can change OPcache configuration from php.ini. Some important configuration setting are
1. opcache.revalidate_freq=60
Here 60 is seconds. OPcache validate cache after given seconds.

2. opcache.max_accelerated_files=7000
This is number of script sotre in OPcache. You can change this number as you needed.

To see OPcache configuratin and status you need a script on same server. Opcache-gui is the good script.

Scaling PHP Book

Categories: php Tags: , , , , , , ,

Install PhpUnit

March 7, 2014 1 comment

Run the Windows Command Processor (cmd.exe).
Run >pear channel-discover
Run >pear remote-list -c phpunit
You will see a similar screen:
DbUnit 1.3.0
Diff 1.1.0
Exporter 1.0.0
File_Iterator 1.3.4
FinderFacade 1.1.0
Git 1.2.0
PHPUnit 4.0.1
PHPUnit_MockObject 1.2.3
PHPUnit_Selenium 1.3.3
PHPUnit_SkeletonGenerator 1.2.1
PHPUnit_Story 1.0.2
PHPUnit_TestListener_DBUS 1.0.0
PHPUnit_TestListener_XHProf 1.0.0
PHPUnit_TicketListener_Fogbugz 1.0.0
PHPUnit_TicketListener_GitHub 1.0.0
PHPUnit_TicketListener_GoogleCode 1.0.0
PHPUnit_TicketListener_Trac 1.0.0
PHP_CodeBrowser 1.0.2
PHP_CodeCoverage 1.2.16
PHP_Invoker 1.1.3
PHP_Timer 1.0.5
PHP_TokenStream 1.2.2
Text_Template 1.2.0
Version 1.0.2
bytekit 1.1.3
phpcov 1.1.0
phpcpd 2.0.0
phpdcd -n/a-
phploc 2.0.4
test_helpers 1.1.0
Run >pear install phpunit/PHPUnit-4.0.1
You will see a similar screen:
downloading PHPUnit-4.0.1.tgz …
Starting to download PHPUnit-4.0.1.tgz (510,701 bytes)
….done: 510,701 bytes
install ok: channel://

Install PHP Mess Detector

March 7, 2014 1 comment

Run the Windows Command Processor (cmd.exe).
Run >pear channel-discover
Run >pear channel-discover
Run >pear install phpmd/PHP_PMD-1.5.0. You will see a similar screen:

Did not download optional dependencies: pecl/imagick, use –alldeps to download
pdepend/PHP_Depend can optionally use package “pecl/imagick” (version >= 2.2.0b2
downloading PHP_PMD-1.5.0.tgz …
Starting to download PHP_PMD-1.5.0.tgz (49,534 bytes)
………….done: 49,534 bytes
downloading PHP_Depend-1.1.4.tgz …
Starting to download PHP_Depend-1.1.4.tgz (179,584 bytes)
…done: 179,584 bytes
install ok: channel://
install ok: channel://

Install PEAR on windows

February 28, 2014 Leave a comment

Follow these steps-

  1. Get the file on the link ->
  2. Go to this directory C:\wamp\bin\php\php5.3.13. Put go-pear.phar her.
  3. Open command prompt (cmd.exe). Supposing you’re using Windows, press Start, type “cmd.exe” and right click on it and select run as administrator.
  4. Type this cd “C:\wamp\bin\php\php5.3.13” Press Enter. You should note that instead of saying “C:\system\32” is now displaying C:\wamp\bin\php\php5.3.13
  5. Write the following command and press Enter: php -d phar.require_hash=0 go-pear.phar
  6. Select whether you like or not a local copy or a system one. By typing system:local and pressing enter you’ll install a local copy. By typing system and pressing enter you’ll install a system-wide copy.
  7. Type yes to confirm the option chosen. The next list of directories, will be the referring directories that PEAR will install its components. If you select from 1 – 12 you can change the directory you’d like to install.

Show data in multicolumns

April 18, 2013 1 comment

Sometimes we need to show data in multi columns. Here is the little script for this logic.
First we need data in array. Let’s create a array.
Sometimes we need to show data in multi columns. Here is the little script for this logic.
First we need data in array. Let’s create an array and display this data in two column.

$columns =2;//No of column
$dataToDisplay = array('green', 'blue', 'white', 'apple', 'computers', 'php');
echo "<table width='100%'>";
$columnwidth = 100/$columns;
$count = count($dataToDisplay);
$i =0;
foreach($dataToDisplay as $data) {
    if($i % $columns == 0){
        echo "<tr>";
    echo "<td>" . $data . "</td>";
    if(($i % $columns) == ($columns - 1) || ($i + 1) == $columns){
        echo "</tr>";
echo "</table>";
%d bloggers like this: