Click to See Complete Forum and Search --> : Most useful functions?


Shrike
12-17-2003, 09:23 AM
So what's the most useful function you have. Or one you reuse everywhere? If you feel like sharing then post ya stuff here.

Here's one to get started, a debug function I use a bit

$start1 = gettimeofday();

function debug(){
global $start1;
$end1=gettimeofday();
$totaltime1 = (float)($end1['sec'] - $start1['sec']) + ((float)($end1['usec'] - $start1['usec'])/1000000);
echo "Debug info<br>";
echo "Page executed in $totaltime1 seconds | ";
echo "Session user ID: ".$_SESSION['uid']." | ";
echo "Session ID: ".$_COOKIE['PHPSESSID']." | ";
echo "File path/URL: ".$_SERVER['SCRIPT_FILENAME']." / ".$_SERVER['REDIRECT_URL']."<br>";
}

Note it requires that $start1 be the first thing on your page

planetsim
12-17-2003, 05:14 PM
You may want to create an Auto Mail bit to that so you get notified

sijis
12-17-2003, 05:58 PM
Here's one i use for connecting to the database:


define('DB_HOST', 'localhost');
define('DB_USER', 'user');
define('DB_PASS', 'pass');
define('DB_NAME', 'database');

function openConn(){
global $dbServerConn;
$dbServerConn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or mysql_error($dbServerConn);
$dbSel = mysql_select_db(DB_NAME, $dbServerConn) or mysql_error($dbSel);
}
function closeConn($result,$dbServerConn){
if (is_resource($result)){
mysql_free_result($result);
mysql_close($dbServerConn);
}
}


usage:


openConn();
$strSQL = "SELECT * FROM table";
$result = mysql_query($strSQL, $dbServerConn);
// your code
closeConn($result, $dbServerConn);


this might be useful for someone. :)
enjoy.

Moonglobe
12-17-2003, 07:57 PM
function do_nothing()
{
//do_nothing() version 5.2
if ($args=func_get_args())
{
foreach ($args as $arg)
{
if ($arg && !($arg===1) && !($arg===true)) return false;
else return true;
}
}
else return true;
}
:D:D:D:D

i'm serious, i do use that a lot. usually like else
{
do_nothing();
}

LordShryku
12-17-2003, 08:40 PM
Moonglobe, if you actually use that....I'm gonna have to come to Canada just to smack you.

Useful functions....there's so many. Probably my most used function is a really small one for gracefully dying. When you execute die(), things just stop. Well, i want my footer to still appear dammit!

Name inspired by QNX :D
<?php
function slay($message) {
?>
<table border="1" cellspacing="4" cellpadding="4" width="50%" align="center">
<tr>
<td class="sidepad"><b><?=wordwrap($message,50,"<br />")?></b></td>
</tr>
</table>
<br />
<?php
require("/var/include/footer.php");
die();
}
?>

I use DBX functions a lot too, but I wrote my own wrapper for them so I could include LDAP, and not have to pass so many variables to them...

sijis
12-18-2003, 12:39 AM
maybe its just me but you didn't actually post the slay() function's code. I'm assuming that is the function you were describing that would continue unlike die().

LordShryku
12-18-2003, 12:41 AM
Just missing the die() at the end. I wrote it like a year ago, so I was trying to jog the old memory. It ain't quite what it used to be...

sijis
12-18-2003, 01:19 AM
haha...
i see it.
i misread the code when i initially looked at it. I see what you were doing.