Home > Coding dan Programming > Fungsi Optimasi Dengan PHP

Fungsi Optimasi Dengan PHP

[August] [20] [2007] Leave a comment Go to comments

Saya akan menuliskan sedikit tentang php hack, atau membuat celah dengan php, php adalah bahasa standar yang dibangun untuk web programming. beberap fungsi akan saya tulis dibawah yaitu :

1.Fungsi Debugging variable :

function ss_array_as_string (&$array, $column = 0) {
$str = “Array(
\n”;
while(list($var, $val) = each($array)){
for ($i = 0; $i < $column+1; $i++){
$str .= ” “;
}
$str .= $var.’ ==> ‘;
$str .= ss_as_string($val, $column+1).”
\n”;
}
for ($i = 0; $i < $column; $i++){
$str .= ” “;
}
return $str.’)’;

}

function ss_object_as_string (&$object, $column = 0) {
if (empty($object->classname)) {
return “$object”;

}
else {
$str = $object->classname.”(
\n”;
while (list(,$var) = each($object->persistent_slots)) {
for ($i = 0; $i < $column; $i++){
$str .= ” “;
}
global $$var;
$str .= $var.’ ==> ‘;
$str .= ss_as_string($$var, column+1).”
\n”;
}
for ($i = 0; $i < $column; $i++){
$str .= ” “;
}
return $str.’)’;
}

}
function ss_as_string (&$thing, $column = 0) {
if (is_object($thing)) {
return ss_object_as_string($thing, $column);
}
elseif (is_array($thing)) {
return ss_array_as_string($thing, $column);
}
elseif (is_double($thing)) {
return “Double(“.$thing.”)”;
}
elseif (is_long($thing)) {
return “Long(“.$thing.”)”;
}
elseif (is_string($thing)) {
return “String(“.$thing.”)”;
}
else {
return “Unknown(“.$thing.”)”;
}
}

2. Fungsi Menciptakan Log :

$ss_log_level = 0;

$ss_log_filename = ‘/tmp/ss-log’;

$ss_log_levels = array(

NONE => 0,
ERROR => 1,
INFO => 2,
DEBUG => 3);

function ss_log_set_level ($level = ERROR) {
global $ss_log_level;
$ss_log_level = $level;

}

function ss_log ($level, $message) {
global $ss_log_level, $ss_log_filename;
if ($ss_log_levels[$ss_log_level] < $ss_log_levels[$level]) {// no logging to be done
return false;

}

$fd = fopen($ss_log_filename, “a+”);
fputs($fd, $level.’ – [‘.ss_timestamp_pretty().’] – ‘.$message.”\n”);
fclose($fd);
return true;

}

function ss_log_reset () {

global $ss_log_filename;

@unlink($ss_log_filename);

}

Terdapat 4 log level yang dapat diambil. pesan log akan dimunculkan jika tingkatan pemasangan dari pemasangan sebelumnya.sehingga kita bisa mematikan log dengan perintah

ss_log_set_level(INFO);

Sekarang pesan log dari tingkatan ERROR atau INFO akan direkam, pesan DEBUGGING akan dilanjutkan, kita bsa memiliki pesan2 log seperti yang kita inginkan, yakni

ss_log(ERROR, “testing level ERROR”);

ss_log(INFO, “testing level INFO”);

ss_log(DEBUG, “testing level DEBUG”);

Sehingga dari keluaran log yang kita buat akan menghasilkan :

ERROR – [August 18, 2007 20:58:17] – testing level ERROR

INFO – [August 18, 2007 20:58:17] – testing level INFO

Untuk itu kita bisa mengosongkan kembali log yang kita buat dengan mereset ulang :

ss_log_reset();

3. Menciptakan Fungsi Optimasi :

function ss_timing_start ($name = ‘default’) {
global $ss_timing_start_times;
$ss_timing_start_times[$name] = explode(‘ ‘, microtime());

}

function ss_timing_stop ($name = ‘default’) {
global $ss_timing_stop_times;
$ss_timing_stop_times[$name] = explode(‘ ‘, microtime());

}

function ss_timing_current ($name = ‘default’) {
global $ss_timing_start_times, $ss_timing_stop_times;
if (!isset($ss_timing_start_times[$name])) {
return 0;
}

if (!isset($ss_timing_stop_times[$name])) {
$stop_time = explode(‘ ‘, microtime());

}

else {
$stop_time = $ss_timing_stop_times[$name];

}

// do the big numbers first so the small ones aren’t lost
$current = $stop_time[1] – $ss_timing_start_times[$name][1];
$current += $stop_time[0] – $ss_timing_start_times[$name][0];
return $current;

}

4.Debugging dan Fungsi menciptakan optimasi database :

function query($Query_String, $halt_on_error = 1) {
$this->connect();
ss_timing_start();
$this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
ss_timing_stop();
ss_log(INFO, ss_timing_current().’ Secs – ‘.$Query_String);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if ($halt_on_error && !$this->Query_ID) {
$this->halt(“Invalid SQL: “.$Query_String);

}
return $this->Query_ID;

}

5. Menggunakan tag Inline dengan ECHO

<h2>Test Inline Tags vs echo</h2>

<p>

php ss_timing_start(‘echo’); ?>

php

for ($i=0; $i<1000; $i++) {
echo $i.”
“;

}

?>

php ss_timing_stop(‘echo’); ?>

<p>

php ss_timing_start(str); ?>

php

$str = ”;

for ($i=0; $i<1000; $i++) {
$str .= $i.”
“;

}

echo $str;

?>

php ss_timing_stop(str); ?>

<p>

php ss_timing_start(inline); ?>
php

for ($i=0; $i<1000; $i++) {

?>
123<br>
php
}
?>
php ss_timing_stop(inline); ?>
<p>
<br>
<h2>Results</h2>
echo – php echo ss_timing_current(‘echo’) ?>
<p>
str –
<p>
inline – php echo ss_timing_current(inline) ?>

6. Menciptakan Fungsi str_replace vs ereg_replace

<h2>Test str_replace vs ereg</h2>

<p>

php $string = ‘Testing with emphasis‘; ?>

php ss_timing_start(‘str_replace’); ?>

php

for ($i=0; $i<1000; $i++) {
str_replace(‘i>’, ‘b>’, $string).'<br>’;

}

?>

php ss_timing_stop(‘str_replace’); ?>

<p>

php ss_timing_start(ereg); ?>

php

for ($i=0; $i<1000; $i++) {
ereg_replace(‘i>’, ‘b>’, $string).'<br>’;

}

?>

php ss_timing_stop(ereg); ?>

<p>

php ss_timing_start(ereg_pattern); ?>

php

for ($i=0; $i<1000; $i++) {
ereg_replace(‘<([/]*)i>’, ‘<\1b>’, $string).'<br>’;

}

?>

php ss_timing_stop(ereg_pattern); ?>

<p>

<br>

<h2>Results</h2>

str_replace – php echo ss_timing_current(str_replace) ?>

<p>

ereg –

<p>

ereg_pattern – php echo ss_timing_current(ereg_pattern) ?>

7. Fungsi Optimasi Query : Menggabungkan tabel

$db->query(“select * from doel”);

0.032273 secs

$db->next_record();

0.00048999999999999 secs

$db->query(“insert into doel values (NULL)”);

0.019506 secs

$db->query(“select * from doel as a, doel as b”);

17.280596 secs

$db->query(“select * from doel as a,doel as b where a.id > b.id”);

14.645251 secs

$db->query(“select * from doel as a, doel as b where a.id = b.id”);

0.041269 secs

$db->query(“select * from doellah”);

25.393672 sec

8. Memanggil Fungsi Method yang tersembunyi :

class A {
function A() { }
function A_dspTwo() {
echo “A: Two<br>”;

}

function dspTwo() {
return $this->A_dspTwo(); // Memanggil kelas method A dspTwo

}

}

class B extends A {
function B() {
$this->A(); // memanggil konstruktor induk.

}

function B_dspTwo() {
$this->A_dspTwo();
echo “B: Two<br>”;

}

function dspTwo() {
return $this->B_dspTwo();

}

}

$object = new B();

$object->dspTwo();

9. Menuntaskan masalah dengan tipe data :

<h2>Test String Integer Comparisons</h2>

php

$a = 1;

$b = ‘2’;

if ($a < $b) {

echo ss_as_string($a).’ < ‘.ss_as_string($b);

}

else {

echo ss_as_string($a).’ >= ‘.ss_as_string($b);

}

?>

<p>

php

$a = 2;

$b = ‘2’;

if ($a == $b) {
echo ss_as_string($a).’ == ‘.ss_as_string($b);

}

else {
echo ss_as_string($a).’ != ‘.ss_as_string($b);

}

?>

<p>

php

$a = array(2, ‘1’);

if ($a[0] > $a[1]) {
echo ss_as_string($a[0]).’ > ‘.ss_as_string($a[1]);

}

else {

echo ss_as_string($a[0]).’ <= ‘.ss_as_string($a[1]);

}

?>

<p>

<?

$a = array(‘2’, ‘1’);

echo ss_as_string($a).'<br>sorts to<br>’;

sort($a);

echo ss_as_string($a);

?>

<p>

<?

$a = array(2, 1);

echo ss_as_string($a).'<br>sorts to<br>’;

sort($a);

echo ss_as_string($a);

?>

<p>

<?

$a = array(‘2’, 1);

echo ss_as_string($a).'<br>sorts to<br>’;

sort($a);

echo ss_as_string($a);

?>

<p>

<?

$a = array(2, ‘1’);

echo ss_as_string($a).'<br>sorts to<br>’;

sort($a);

echo ss_as_string($a);

?>

Sehingga dapat disederhanakan :

Long(1) < String(2)

Long(2) == String(2)

Long(2) > String(1)

Array(
0 ==> String(2)
1 ==> String(1)
)
sorts to
Array(
0 ==> String(1)
1 ==> String(2)
)

Array(
0 ==> Long(2)
1 ==> Long(1)
)
sorts to
Array(
0 ==> Long(1)
1 ==> Long(2)
)

Array(
0 ==> String(2)
1 ==> Long(1)
)
sorts to
Array(
0 ==> Long(1)
1 ==> String(2)
)

Array(
0 ==> Long(2)
1 ==> String(1)
)
sorts to
Array(
0 ==> Long(2)
1 ==> String(1)

)

10. Fungsi Include dan Require :

Include :

if($something){

include(“somefile”);

}

Include dengan looping/pengulangan:

$i = 1;

while ($i < 3) {

include(“somefile.$i”);

$i++;

}

require dengan looping :

$i = 1;

while ($i < 3) {

require(“somefile.$i”);

$i++;

}

11. Fungsi Echo dan Print

Fungsi Echo :

echo expression [, expression[, expression] … ]

Fungsi Print :

$ret = print “Hello World”;

dan $ret akan 1

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: