To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-17-2003, 09:23 AM   #1
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
Most useful functions?

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
PHP Code:
$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
__________________
The Hundredth Idiot
Shrike is offline   Reply With Quote
Old 12-17-2003, 05:14 PM   #2
planetsim
code | beer > sleep
 
Join Date: Sep 2002
Location: aus
Posts: 4,826
You may want to create an Auto Mail bit to that so you get notified
__________________
Dont be lazy Search
And use the Manual
Webmobo - Open Source News Script | Portfolio / Blog | Against TCPA
planetsim is offline   Reply With Quote
Old 12-17-2003, 05:58 PM   #3
sijis
Senior Member
 
Join Date: Oct 2002
Location: USA
Posts: 374
Here's one i use for connecting to the database:

PHP Code:
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:

PHP Code:
openConn();
$strSQL = "SELECT * FROM table";
$result = mysql_query($strSQL, $dbServerConn);
// your code
closeConn($result, $dbServerConn);
this might be useful for someone.
enjoy.
__________________
// signature
sijis is offline   Reply With Quote
Old 12-17-2003, 07:57 PM   #4
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
PHP Code:
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;
}


i'm serious, i do use that a lot. usually like
Code:
else
{
    do_nothing();
}
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 12-17-2003, 08:40 PM   #5
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
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
PHP Code:
<?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...

Last edited by LordShryku; 12-18-2003 at 12:42 AM.
LordShryku is offline   Reply With Quote
Old 12-18-2003, 12:39 AM   #6
sijis
Senior Member
 
Join Date: Oct 2002
Location: USA
Posts: 374
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().
__________________
// signature
sijis is offline   Reply With Quote
Old 12-18-2003, 12:41 AM   #7
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
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...
LordShryku is offline   Reply With Quote
Old 12-18-2003, 01:19 AM   #8
sijis
Senior Member
 
Join Date: Oct 2002
Location: USA
Posts: 374
haha...
i see it.
i misread the code when i initially looked at it. I see what you were doing.
__________________
// signature
sijis is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 12:15 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.