Tuesday, July 14, 2009

Difference between mysql_connect() and mysql_pconnect()?

In shortly mysql_pconnect() makes a persistent connection to the database which do not close when the execution of your script ends

Reverse sort an array

$myworld = array("a"=>"PHP","b"=>"learning","c"=>"I'm");
krsort($myworld);
print_r($myworld);
?>


Which confirms your suspicions:

Array
(
[c] => I'm
[b] => learning
[a] => PHP
)

My own sort function?

In PHP you will, in most cases, be able to get by with one of the default sort functions, perhaps used in conjunction with each other.

However there will be times where you want to sort based on some other factor.

When you need to do this, you use the user sort function within PHP, which looks like this usort(array,function) where you specify the user-defined function that will sort the array.

Here is a simple example I've whipped up using a user-defined function to sort an array based on the length of the values, which is useful if you want to find the longest word in an array, for example.

$inputwords = array("whichever","word","is","the","longest","will","appear","at","the","top");
usort($inputwords, "sortlen");
print_r($inputwords);

function
sortlen($a,$b) {
$lena = strlen($a); $lenb = strlen($b);
if (
$lena == $lenb) return 0;
return (
$lena > $lenb) ? -1 : 1;
}
?>


This prints the following:

Array
(
[0] => whichever
[1] => longest
[2] => appear
[3] => word
[4] => will
[5] => top
[6] => the
[7] => the
[8] => at
[9] => is
)

Just for completeness, let's reverse from symbol to sort with the shortest value in the array at the top:

$inputwords = array("whichever","word","is","the","shortest","will","appear","at","the","top");
usort($inputwords, "sortlen");
print_r($inputwords);

function
sortlen($a,$b) {
$lena = strlen($a); $lenb = strlen($b);
if (
$lena == $lenb) return 0;
return (
$lena < $lenb) ? -1 : 1;
}
?>


Which prints this:

Array
(
[0] => is
[1] => at
[2] => top
[3] => the
[4] => the
[5] => word
[6] => will
[7] => appear
[8] => shortest
[9] => whichever
)

How can i reset an array in PHP?

Sometimes if you are moving through the various elements in array, you will want to return the pointer to beginning of the array.

reset function, like this:
reset($array);

This returns the first element and sets it as the current element.

Here is a little example just to make this technique clear:

$numbers = array("Item 1","Item 2","Item 3");
next($numbers);
$thisvalue = current($numbers); echo "We are now at $thisvalue\n\n";
$first = reset($numbers);
echo
"Back to $first";
?>

How can you check that a value is already in an array?

If you wish to check whether a value is already stored in an array or not, then use the in_array function.

This is useful when you don't want any duplicates in the array and therefore only want to add a value if it's not already there. The first argument is the string you are testing for and the second is the array you are checking against.

Here is an example of in_array in action:

$values = array("banana","apple","pear","banana");
$newvalue = "pear";
if (
in_array($newvalue,$values)) { echo "$newvalue is already in the array!"; }
?>