Thursday, June 2, 2011

Generate Logs in php

class logfile{
    function write($the_string )
    {
        if( $fh = @fopen( "storeLog.txt", "a+" ) )
        {
            fputs( $fh, $the_string, strlen($the_string) );
            fclose( $fh );
            return( true );
        }
        else
        {
            return( false );
        }
    }
}


$lf = new logfile();
$lf->write("Log starts here");
echo "Log data";
$lf->write("End First Log");
?>

Thursday, February 24, 2011

mysql_pconnect()

mysql_pconnect() acts very much like mysql_connect() with two major differences.
First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).

CAPTCHA

PHP stored procedure

delimiter //
create function Area (R double) returns double
deterministic
begin
declare A double;
set A = R * R * pi();
return A;
end
//
delimiter ;

And to call it from php code to display the area of a circle with radius 22cm,
$rs_area = mysql_query(“select Area(22)”);
$area = mysql_result($rs_area,0,0);
echo “The area of the circle with radius 22cm is ”.$area.” sq.cm”;
?>


visit link

Wednesday, February 23, 2011

Get file contents in a single variable

echo $file = file_get_contents("index.php");
print("Size of the file: ".strlen($file)."\n");

Sunday, January 23, 2011

Unsupervised learning technique to find clusters (subsets of data with similar caracteristics) in unknown data.

 Given a random set of numbers, the problem to solve here is to determine
 a fixed number of intervals that best describe the distribution of the 
initial dataset. Instead of trying to identify how many clusters exists 
in the dataset (also possible, but outside the scope of this article), a
 K-means clustering algorithm will always return a fixed (K) number of 
subsets.
 
 
 


print_r(kmeans(array(1, 3, 2, 5, 6, 2, 3, 1, 30, 36, 45, 3, 15, 17), 3));
 
 
 
function kmeans($data, $k)
{
        $cPositions = assign_initial_positions($data, $k);
        $clusters = array();
 
        while(true)
        {
                $changes = kmeans_clustering($data, $cPositions, $clusters);
                if(!$changes)
                {
                        return kmeans_get_cluster_values($clusters, $data);
                }
                $cPositions = kmeans_recalculate_cpositions($cPositions, $data, $clusters);
        }
}
 
 
function kmeans_clustering($data, $cPositions, &$clusters)
{
        $nChanges = 0;
        foreach($data as $dataKey => $value)
        {
                $minDistance = null;
                $cluster = null;
                foreach($cPositions as $k => $position)
                {
                        $distance = distance($value, $position);
                        if(is_null($minDistance) || $minDistance > $distance)
                        {
                                $minDistance = $distance;
                                $cluster = $k;
                        }
                }
                if(!isset($clusters[$dataKey]) || $clusters[$dataKey] != $cluster)
                {
                        $nChanges++;
                }
                $clusters[$dataKey] = $cluster;
        }
 
        return $nChanges;
}
 
 
 
 
function kmeans_recalculate_cpositions($cPositions, $data, $clusters)
{
        $kValues = kmeans_get_cluster_values($clusters, $data);
        foreach($cPositions as $k => $position)
        {
                $cPositions[$k] = empty($kValues[$k]) ? 0 : kmeans_avg($kValues[$k]);
        }
        return $cPositions;
}
 
function kmeans_get_cluster_values($clusters, $data)
{
        $values = array();
        foreach($clusters as $dataKey => $cluster)
        {
                $values[$cluster][] = $data[$dataKey];
        }
        return $values;
}
 
 
function kmeans_avg($values)
{
        $n = count($values);
        $sum = array_sum($values);
        return ($n == 0) ? 0 : $sum / $n;
}
 
function distance($v1, $v2)
{
  return abs($v1-$v2);
}
 
 
function assign_initial_positions($data, $k)
{
        $min = min($data);
        $max = max($data);
        $int = ceil(abs($max - $min) / $k);
        while($k-- > 0)
        {
                $cPositions[$k] = $min + $int * $k;
        }
        return $cPositions;
}
 
 
Output
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 2
            [3] => 5
            [4] => 6
            [5] => 2
            [6] => 3
            [7] => 1
            [8] => 3
        )
 
    [2] => Array
        (
            [0] => 30
            [1] => 36
            [2] => 45
        )
 
    [1] => Array
        (
            [0] => 15
            [1] => 17
        )
 
)
 

Saturday, January 22, 2011

GET /POST

GET method will be showing the information information to the users.But in the case of POST method information will not be shown to the user.


The data passed using the GET method would be visible to the user of the website in the browser address bar but when we pass the information using the POST method the data is not visible to the user directly.



Thursday, December 23, 2010

Converting double pricision to binary

echo decbin(round('put double precision no here'));

Monday, October 11, 2010

php-soap configuration

INSTALLATION OF nusoap ON UBUNTU

1.To enable SOAP support, configure PHP with --enable-soap .

2.sudo aptitude install php-soap

3.The behaviour of these functions is affected by settings in php.ini.

SOAP Configure Options Name Default Changeable Changelog
soap.wsdl_cache_enabled 1 PHP_INI_ALL
soap.wsdl_cache_dir /tmp PHP_INI_ALL
soap.wsdl_cache_ttl 86400 PHP_INI_ALL
soap.wsdl_cache 1 PHP_INI_ALL
soap.wsdl_cache_limit 5 PHP_INI_ALL

Saturday, September 25, 2010

No of occurrences of substring in a string.

function strposOffset($search, $string, $offset)
{
    /*** explode the string ***/
    $arr = explode($search, $string);
    /*** check the search is not out of bounds ***/
    switch( $offset )
    {
        case $offset == 0:
        return false;
        break;
  
        case $offset > max(array_keys($arr)):
        return false;
        break;

        default:
        return strlen(implode($search, array_slice($arr, 0, $offset)));

    }
}


$offset = 2;

/*** the string to search for ***/
$search = 'is';

/*** the string to search ***/
$string = 'this is a best way to get no of occurrences of a substring in a string ';

echo $p= strposOffset($search, $string, $offset);
if($p=='')
echo 'not found'
?>

Tuesday, August 3, 2010

__set() & __get() Magic Methods

In the below example class Foo uses set and get methods while class Bar uses the magic methods. To summarise set and get, basically if you are trying to access a property of an object (and it has a get method) the name of the property you are requesting is passed into the get method and it can handle it how it wants. set works the same but it is only called when you attempt to assign a value to a member variable. The only difference being set takes two paramemters, the variable its setting and the value. 




class Foo{

    private $name;
    private $age;

    public function setName($name){
        $this->name = $name;
    }

    public function getName(){
        return $this->name;
    }

    public function setAge($age){
        $this->age = $age;
    }

    public function getAge(){
        return $this->age;
    }

}

$foo = new Foo();
$foo->setName("Dougal");
$foo->setAge(10);

echo $foo->getName(). " is " . $foo->getAge(). " years old\n";

class Bar{

    private $name;
    private $age;

    public function __set($var, $val){
        $this->$var = $val;
    }

    public function __get($var){
        return $this->$var;
    }

}
$bar = new Bar();
$bar->name = "Dougal";
$bar->age = 10;

echo $bar->name . " is " . $bar->age. " years old";
?>

Ajax Cache-Problem with Internet Explorer.

Include those lines into php file.

header('Pragma: no-cache');
header('Cache-Control: no-cache');
header('Expires: 0');

Word Wrap in php

$text "A very long woooooooooooord.";$newtext wordwrap($text8"\n"true);

echo 
"$newtext\n";?>

PHP Scripting in Command line

1. How can I execute a PHP script using command line?
Ans : “php filename” or “php -f filename” will run a php file in command line.

ZEND BLOG

http://shiflett.org/blog/2005/apr/zend-certification-self-test

ZEND TEST BOOK

http://www.boysj.com/the-zend-php-certification-practice-test-book

PHP CLASSES AND IF/ELSE BLOCK

//*********************************working
$flag='3';

if($flag=='3')
{
class cc {
function __construct() {
echo 'cc!';

}
}

}

$type = 'cc';
$obj = new $type;
//****************************************not working

$type = 'cc';
$obj = new $type;
$flag='3';

if($flag=='3')
{
class cc {
function __construct() {
echo 'cc!';

}
}

}


?>

what is interface in php? how it is use?


An Interface is like a template similar to abstract class
with a difference where it uses only abstract methods.

In simple words, an interface is like a class using
interface keyword and contains only function
declarations(function with no body).

An Interface should be implemented in the class and all the
methods or functions should be overwridden in this class.

for eg: 

interface InterfaceName{
  function fun1();
  function fun2(a,b);    
}

class ClassName implements InterfaceName{
  fuction fun1(){
    function implementation.......
  }
  fuction fun2(a,b){
    function implementation.......
  }
}