Page 1 of 1

General Tips

Posted: Mon Feb 02, 2009 6:07 pm
by jayshields
Does anyone have any general PHP coding tips to share? Perhaps any little scripts/techniques they've written which constantly help when coding (especially debugging)?

Everyone uses sessions alot, and I find it's really annoying to reboot Apache everytime I screw up the $_SESSION array. So I've been using this for a while now:

Code: Select all

 
<?php
 
session_start();
 
if(isset($_GET['kill']))
    session_unset();
else
{
    $sessPath = ini_get('session.save_path');
    $sessCookie = ini_get('session.cookie_path');
 
    echo '<br>sessPath: ' . $sessPath;
    echo '<br>sessCookie: ' . $sessCookie;
    echo '<br>sessId: ' . session_id();
 
    echo '<hr />';
 
    echo '<br />current session array: '; print_r($_SESSION);
 
    if(isset($_GET['one']))
        $_SESSION['test1'] = 'hello1';
    else
        $_SESSION['test'] = 'hello';
 
    echo '<br />session array after set: '; print_r($_SESSION);
}
 
?>
<br />
<a href="sessions.php">set test to hello</a><br />
<a href="sessions.php?one">set test1 to hello1</a><br />
<a href="sessions.php?kill">kill session</a>
 

Re: General Tips

Posted: Mon Feb 02, 2009 6:08 pm
by Citizen
Everyone uses sessions alot, and I find it's really annoying to reboot Apache everytime I screw up the $_SESSION array.
I just exit the browser :)

Re: General Tips

Posted: Mon Feb 02, 2009 6:17 pm
by jayshields
Well I suppose that means I should be testing in a seperate browser to the one that's got umpteen tabs open! Besides, I find it quicker to reboot Apache than to reboot Firefox...

Re: General Tips

Posted: Mon Feb 02, 2009 6:22 pm
by mickeyunderscore
This is pretty simple, but will help people who are having trouble understanding array structure:

Code: Select all

echo '<pre>';
print_r($arr);
echo '</pre>';
When I first started coding PHP this helped me no end!

Another useful tip for people new to programming:

Code: Select all

if(5 == $var) //instead of if($var == 5)
The above will make PHP throw out a parse error if you miss out an equals sign and do an assignment operation by mistake, instead of making your program behave strangely.

Browser plugins in particular Firebug for Firefox has helped me a lot when debugging AJAX code.

If you are after debugging power and use OO code, then you should look into installing PHPUnit.

Also not really a coding tip, but reading the comments on a function on PHP.net is a fanstastic help too, a lot of useful comments on there.

On the sessions: does tools > clear private data > Authenticated Sessions do the job (FF)?