Developers Archive for the 'php programming' Category

Disadvantages of global variables

Disadvantages of global variables Tuesday, March 6th, 2007

The following example demonstrates use of the global keyword:

<?php
$my_var = ‘Hello World’;test_global();

function test_global() {
// Now in local scope
// the $my_var variable doesn’t exist

// Produces error: “Undefined variable: my_var”
echo $my_var;

// Now let’s important the variable
global $my_var;

// Works:
echo $my_var;
}

?>

As you can see in the above example, the global keyword is used to important variables from the global scope. Seems to work fine, and it’s nice and simple, so why should you worry about using the global keyword?

There are three good reasons:

1. Reusing parts of the script is impossible
If a certain function relies on global variables, it becomes almost impossible to use that function in a different context. Another problem is that you can’t take that function, and use it in another script.

2. Solving bugs is much harder
Tracking a global variable is much harder than a non-global variable. A global variable could be declared in some obscure include file, which could take hours to find, although a good text editor / IDE could help with this.

3. Understanding the code in a year will be much more difficult
Globals make it difficult to see where a variable is coming from and what it does. You might know about every global during development, but after a year or so you’ll probably have forgotten at least half of them, and then you’ll be kicking yourself for using so many globals

Colorfade

Colorfade Tuesday, February 27th, 2007

<?php

function colorfade_string($hex_start, $hex_end, $string)

{

$realLen = trim(strlen($string));

//we need to skip spaces

$n_spaces = substr_count($string, ” “);

$len = $realLen - $n_spaces;

if($len == 0)

die(”String length = 0″);

if(!ereg(”([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})”, $hex_start, $firstcolor))

die(”First Color is Invalid”);

if(!ereg(”([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})”, $hex_end, $endcolor))

die(”Second Color is invalid”);

// Converting hexa decimal to decimal

$start_1 = hexdec($firstcolor[1]);

$start_2 = hexdec($firstcolor[2]);

$start_3 = hexdec($firstcolor[3]);

$end_1 = hexdec($endcolor[1]);

$end_2 = hexdec($endcolor[2]);

$end_3 = hexdec($endcolor[3]);

$diff1 = $end_1 - $start_1;

$diff2 = $end_2 - $start_2;

$diff3 = $end_3 - $start_3;

$step1 = round($diff1 / $len);

$step2 = round($diff2 / $len);

$step3 = round($diff3 / $len);

$red = $start_1;

$blue = $start_2;

$green = $start_3;

// Calculating hex[] array using the start and step value

for($i = 0; $i < $len; $i++)

{

$hex[] = sprintf(”%02X%02X%02X”, $red, $blue, $green);

$red += $step1;

$blue += $step2;

$green += $step3;

}

for($i = 0, $k = 0; $i < $realLen; $i++)

{

// Code to fetch each character with different color

$char = substr($string, $i, 1);

if($char != ” “)

{

print(”<font color=’ . $hex[$k] . ‘>$char</font>”);

$k++;

}

else

print(” ”);

}

}

echo colorfade_string(”aabbcc”, “aaabbb”, “This is Sample text”);

?>

In above function, we can pass start color,end shade color and the string. In colorfade_string() funtion, we can display text with fading effect.

Displaying “New” text next to title in Web forums

Displaying “New” text next to title in Web forums Tuesday, February 27th, 2007

In web forums, we can display “New” text next to tiltles using the following example. Here we will select a title, the current date minus the DATE field which was set when the item was added to the database.

We’ll turn the sql result into a varialbe and check to see if it’s less than 15 (15 days old) and if it is, in the next if statement we’ll simply echo “New!!” next to the title.

This is handy for showing people new items in databases and pages.

<?

$sql_date = mysql_query(”SELECT title, TO_DAYS(now())-TO_DAYS(date) as date_diff FROM table WHERE id=’$id’”) or die (mysql_error());

while(list($title, $date_diff)=mysql_fetch_array($sql_date)){

if($date_diff < 14){

$new = “y”;

}

echo “Title: $title”;

if($new == “y”){

echo ” <font color=”red”>New!!</font>”;

}

mysql_free_result($sql_date);

unset($new);

}

?>


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.