Wednesday, March 3, 2010

Benchmark or time script

Here is a little example of how to benchmark or time something with php


<?php

// a function to get microtime
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

// start time
$time_start = getmicrotime();

// a little loop to time
for ($i=0; $i < 10000; $i++)
{
// print the loop number
echo $i.'<br />';
}

// the end time
$time_end = getmicrotime();

// subtract the start time from the end time to get the time taken
$time = $time_end - $time_start;


// echo a little message
echo '<br />Script ran for ' . round($time,2) .' seconds.';

?>



This will produce a list of numbers from 0 to 9999 in your browser and tell you how long it took too complete the iterations.

Read More...

PHP Function Number to Roman and Roman to Number


class konversi{
function roman2number($roman){
$conv = array(
array("letter" => 'I', "number" => 1),
array("letter" => 'V', "number" => 5),
array("letter" => 'X', "number" => 10),
array("letter" => 'L', "number" => 50),
array("letter" => 'C', "number" => 100),
array("letter" => 'D', "number" => 500),
array("letter" => 'M', "number" => 1000),
array("letter" => 0, "number" => 0)
);
$arabic = 0;
$state = 0;
$sidx = 0;
$len = strlen($roman);

while ($len >= 0) {
$i = 0;
$sidx = $len;

while ($conv[$i]['number'] > 0) {
if (strtoupper($roman[$sidx]) == $conv[$i]['letter']) {
if ($state > $conv[$i]['number']) {
$arabic -= $conv[$i]['number'];
} else {
$arabic += $conv[$i]['number'];
$state = $conv[$i]['number'];
}
}
$i++;
}

$len--;
}

return($arabic);
}


function number2roman($num,$isUpper=true) {
$n = intval($num);
$res = '';

/*** roman_numerals array ***/
$roman_numerals = array(
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
'XL' => 40,
'X' => 10,
'IX' => 9,
'V' => 5,
'IV' => 4,
'I' => 1);

foreach ($roman_numerals as $roman => $number)
{
/*** divide to get matches ***/
$matches = intval($n / $number);

/*** assign the roman char * $matches ***/
$res .= str_repeat($roman, $matches);

/*** substract from the number ***/
$n = $n % $number;
}

/*** return the res ***/
if($isUpper) return $res;
else return strtolower($res);
}
}

Read More...

PHP Substr In Array

To find if a value exists in an array, the PHP in_array() function works quite nicely. But there are times when only a partial match is required to check in the array. This substr_in_array() function checks will search the values of an array for a substring. The $needl can be a string or an array of strings to search for.


<?php
/**
*
* @Search for substring in an array
*
* @param string $neele
*
* @param mixed $haystack
*
* @return bool
*
*/
function substr_in_array($needle, $haystack)
{
/*** cast to array ***/
$needle = (array) $needle;

/*** map with preg_quote ***/
$needle = array_map('preg_quote', $needle);

/*** loop of the array to get the search pattern ***/
foreach ($needle as $pattern)
{
if (count(preg_grep("/$pattern/", $haystack)) > 0)
return true;
}
/*** if it is not found ***/
return false;
}
?>


Example Usage


<?php

/*** an arrray to search through ***/
$array = array('dingo', 'wombat', 'kangaroo', 'platypus');


/*** an array of values to search for ***/
$strings = array('foo', 'bar', 'kang');

/*** check for true or false with ternary ***/
echo substr_in_array( $strings, $array ) ? 'found' : 'not found';

/*** a single string to search for ***/
$string = 'plat';

/*** check for true or false with ternary ***/
echo substr_in_array( $string, $array ) ? 'found' : 'not found';
?>

Read More...

Monday, March 1, 2010

The Complete Reference for Any jQuery Developer

To make optimal use of jQuery, it's good to keep in mind the breadth of capabilities it provides. You can add dynamic, interactive elements to your sites with reduced development time using jQuery. If you are looking for a comprehensive reference guide to this popular JavaScript library, this book is for you.

Revised and updated for version 1.4 of jQuery, this book offers an organized menu of every jQuery method, function, and selector. Each method and function is introduced with a summary of its syntax and a list of its parameters and return value, followed by a discussion, with examples where applicable, to assist in getting the most out of jQuery and avoiding the pitfalls commonly associated with JavaScript and other client-side languages.

In this book you will be provided information about the latest features of jQuery that include Sizzle Selector, Native event delegation, Event triggering, DOM manipulation, and many more. You won't be confined to built-in functionality, you'll be able to examine jQuery's plug-in architecture and we discuss both how to use plug-ins and how to write your own. If you're already familiar with JavaScript programming, this book will help you dive right into advanced jQuery concepts. You'll be able to experiment on your own, trusting the pages of this book to provide information on the intricacies of the library, where and when you need it.

Download :
- jQuery and jQuery UI Reference 1.2.chm
- jquery-1.4.chm
- jquery-api-20090115.chm

jQuery Resources :
- Jquery.com
- Downloading jQuery
- jQuery Documentation
- jQuery Code Repository on GitHub
- The jQuery Project
- Learningjquery.com

Read More...

Latest Comments

About Me

My photo
Makassar, Sulawesi Selatan, Indonesia

Guest Book


ShoutMix chat widget

Script Sense ©Template Blogger Green by Dicas Blogger.

TOPO