How to create stdClass (Object)

function myFunction($stdObj){
 echo $stdObj->name."";
}

$a= new stdClass;
$a->name = "Ramesh Song";
$a->email = "ilovephp@gmail.com";
$a->web = "phpbuddy.blogsport.com";

myFunction($a); // output : Ramesh Song

/********  Other Way ********/

$b = array(
   "name" => "Ramesh song",
   "email" => "ilovephp@gmail.com",
   "web" => "phpbuddy.blogsport.com");

$b = (object) $b;

myFunction($b); // output : Ramesh Song

How to use array_map() with class method?

class MyClass {
 public function __construct(){}

 protected function convertToObject($array){
  return (object) $array;
 }

 public function convertMyArray($array){
  $obj = array_map(array('MyClass','convertToObject'),$array);
  return $obj;
 }
}

$array = array(
 array('Country' => 'US'),
 array('Country' => 'KOREA'),
 array('Country' => 'JAPAN'),
 array('Country' => 'INDIA'),
 array('Country' => 'CANADA')
);

$cls = new MyClass();
$obj = $cls->convertMyArray($array);
echo $obj[0]->Country; // OUTPUT : US

How to use PHP temporary file ?

$temp = tmpfile();
fwrite($temp,'Line #1'.chr(13).chr(10));
fwrite($temp,'Line #2'.chr(13).chr(10));
fwrite($temp,'Line #3'.chr(13).chr(10));
fseek($temp,0); //move file pointer

// char(13) = \r
// char(10) = \n

while($row = fgets($temp,1024)){
 echo $row ."
";
}

fclose($temp); // remove temporary fileand no space!

Output ::
Line #1
Line #2
Line #3

Access parent’s parent variable and Access interface constant variable

// Access parent's parent variable
class Parent1 {
 const PARENT_NAME = "ramesh Song";
 const PARENT_AGE = "31";
 const PARENT_EMAIL = "ilovephp@gmail.com";
 protected $_normal_val = "HeHeHeHe";
}

class Parent2 extends Parent1 {
 public function __construct(){}
}

class Child extends Parent2 {
 public function __construct(){}

 public function getParentName(){
  return parent::PARENT_NAME;
 }

 public function getNormalVal(){
  return $this->_normal_val;
 }
}

$c = new Child();
echo $c->getParentName();
echo "br";
echo $c->getNormalVal();

/*
Output :
 ramesh Song
 HeHeHeHe
*/

// Access interface constant variable

interface Parent3 {
 const PARENT_NAME = "ramesh Song";
 const PARENT_AGE = "31";
 const PARENT_EMAIL = "ilovephp@gmail.com";
 //protected $_normal_val = "HeHeHeHe"; Error : Interfaces may not include member variables
}

class Parent4 implements Parent3 {
 public function __construct(){}

 public function getParentName(){
  //return parent::PARENT_NAME;  Error : current class scope has no parent
  return self::PARENT_NAME;
 }
}

class Child2 extends Parent4 {
 public function __construct(){}

 public function getParentName(){
  return parent::PARENT_NAME;

 }
 public function getParentAge(){
  return parent::PARENT_AGE;
 }
}

$d = new Parent4();
echo $d -> getParentName();
echo "br";
$e = new Child2();
echo $e->getParentName();
echo "br";
echo $e->getParentAge();

/*
Output :
 ramesh Song
 ramesh Song
 31
*/

Serialize with Jquery and Unserialize with PHP

//JS (Jquery)

var form = document.getElememtById('myForm');

var serializedData = $(form).serialize();

$.post(
 'getSerializedData.php',
 {data:serializedData},
 function(data){
 }
)

// PHP

$serializedData = $_POST['data'];
$unserializedData = array();

parse_str($unserializedData,$serializedData);

print_r($unserializedData);

Get Browser Information Using PHP

function getBrowser(){
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version= "";

    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'linux';
    }
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'mac';
    }
    elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'windows';
    }

    // Next get the name of the useragent yes seperately and for good reason
    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
    {
        $bname = 'Internet Explorer';
        $ub = "MSIE";
    }
    elseif(preg_match('/Firefox/i',$u_agent))
    {
        $bname = 'Mozilla Firefox';
        $ub = "Firefox";
    }
    elseif(preg_match('/Chrome/i',$u_agent))
    {
        $bname = 'Google Chrome';
        $ub = "Chrome";
    }
    elseif(preg_match('/Safari/i',$u_agent))
    {
        $bname = 'Apple Safari';
        $ub = "Safari";
    }
    elseif(preg_match('/Opera/i',$u_agent))
    {
        $bname = 'Opera';
        $ub = "Opera";
    }
    elseif(preg_match('/Netscape/i',$u_agent))
    {
        $bname = 'Netscape';
        $ub = "Netscape";
    }

    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?' . join('|', $known) .
    ')[/ ]+(?[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
        // we have no matching number just continue
    }

    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
        //we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){             $version= $matches['version'][0];         }         else {             $version= $matches['version'][1];         }     }     else {         $version= $matches['version'][0];     }     // check if we have a number     if ($version==null || $version=="") {$version="?";}     return array(         'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern
    );
}

Add and Remove HTML elements using javascript





How to count no of Files in a Folder?

if (file_exists("images/"))
{
 $dh = opendir("images/");
 $filecount = 0;
 while (($file = readdir($dh)) !== false) 
 {
  if(is_file("images/".$file))
   $filecount++;
 }
 echo 'No fo Files is: '.$filecount;
}

$directory = "../images/team/harry/";
if (glob($directory . "*.jpg") != false)
{
 $filecount = count(glob($directory . "*.jpg"));
 echo $filecount;
}
else
{
 echo 0;
}

Installing ionCube Loader for Xampp


Installing ionCube Loader In Linux:

1. Download the program using wget or FTP.
http://www.ioncube.com/loader_download.php

2. Unpack it
tar -zxvf ioncube_loaders.tar.gz

3. cd ioncube (if you are using xampp, copy the extracted folder into C:\xampp\apache)

4. copy the following php files into the default document root

ioncube-loader-helper.php
ioncube-encoded-file.php

Then open it as http://localhost/ioncube-loader-helper.php

The output should be something similar to:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
php.ini Installation Instructions
**********************************
Analysis
**********
Analysis of your system configuration shows:

PHP Version 5.2.9
Operating System Linux
Threaded PHP No
php.ini file C:\xampp\apache\bin\php.ini
Required Loader ioncube_loader_win_5.2.dll

Instructions
To install the Loader in your php.ini file, edit or create the file C:\xampp\apache\bin\php.ini and add the following line before any other zend_extension lines:

zend_extension = //ioncube_loader_win_5.2.dll

where // is where you’ve installed the loader, e.g. C:\xampp\apache\ioncube\

If there are no zend_extension lines already, you can put the line at any point in the file.

Finally, stop and restart your web server software for the changes to take effect.

Installing the Loader for run-time loading

To install for runtime loading, create a directory called ioncube at or above the top level of your encoded files, and ensure that the directory contains the ioncube_loader_win_5.2.dll loader. If run-time install of the Loader is possible on your system then encoded files should automatically install the loader when needed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

5. Now lets move the iconcube directory to a permanent location:
cd ..
mv ioncube C:\xampp\apache\

6. Now that you know the location of php.ini you need to edit it.
vi C:\xampp\apache\bin\php.ini

Now find where other zend extentions are in the file.
search for “zend_extension”

Paste in your new line for ioncube loader
zend_extension = C:\xampp\apache\ioncube\ioncube_loader_win_5.2.dll

7. Save the changes

8. Restart the web server to take effect.
/etc/init.d/httpd restart

Success! You should now see a section in your PHP Info page that says:

Additional Modules
Module Name ionCube Loader

You may also use the following URL to verify the installation
http://localhost/ioncube-encoded-file.php

It will show something like:
~~~~~~~~~~~~~~~~~~~~~
This file has been successfully decoded. ionCube Loaders are correctly installed.


Installing ionCube Loader In Windows:

1. Download the Zip File.
http://www.ioncube.com/loader_download.php

2. UnZip it

3. if you are using xampp, copy the extracted folder into C:\xampp\php

4. copy the following php files into the default document root

ioncube-loader-helper.php
ioncube-encoded-file.php

Then open it as http://localhost/ioncube-loader-helper.php

The output should be something similar to:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
php.ini Installation Instructions
**********************************
Analysis
**********
Analysis of your system configuration shows:

PHP Version 5.2.9
Operating System Windows
Threaded PHP No
php.ini file C:\xampp\php\php.ini
Required Loader ioncube_loader_win_5.2.dll

Instructions
To install the Loader in your php.ini file, edit or create the file C:\xampp\php\php.ini and add the following line before any other zend_extension lines:

zend_extension = //ioncube_loader_win_5.2.dll

where // is where you’ve installed the loader, e.g. C:\xampp\php\ioncube\

If there are no zend_extension lines already, you can put the line at any point in the file.

Finally, stop and restart your web server software for the changes to take effect.

Installing the Loader for run-time loading

To install for runtime loading, create a directory called ioncube at or above the top level of your encoded files, and ensure that the directory contains the ioncube_loader_win_5.2.dll loader. If run-time install of the Loader is possible on your system then encoded files should automatically install the loader when needed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

5. Now lets move the iconcube directory to a permanent location:
  ioncube C:\xampp\php\

6. Now that you know the location of php.ini you need to edit it.
 C:\xampp\php\php.ini

Now find where other zend extentions are in the file.
search for “zend_extension”

Paste in your new line for ioncube loader
zend_extension = C:\xampp\php\ioncube\ioncube_loader_win_5.2.dll

7. Save the changes

8. Restart the web server to take effect.

Success! You should now see a section in your PHP Info page that says:

Additional Modules
Module Name ionCube Loader

You may also use the following URL to verify the installation
http://localhost/ioncube-encoded-file.php

It will show something like:
~~~~~~~~~~~~~~~~~~~~~
This file has been successfully decoded. ionCube Loaders are correctly installed.