General Tips

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

General Tips

Post 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>
 
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: General Tips

Post 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 :)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: General Tips

Post 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...
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: General Tips

Post 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)?
Post Reply