Jump to content

Proper PHP coding


uncleronin

Recommended Posts

Okay, I've been coding in PHP for a while now and I decided to enable the E_NOTICE setting in php.ini. Big mistake! I get all sorts of notices saying I'm a terrible coder and my CSS styles get messed up. Is this a problem with the PHP interpreter or what?

 

If its me then how do I do the following:

Use Session_Start() and not call it when a session is already active? (Just check the size of the session array?)

Reference session variables without using $_SESSION? (Got a notice moaning about variable SESSION)

Reference $_POST variables without using $_POST['Name']? (Notice about unreferenced index - it would be ridiculous to reference each post variable by its index because there are on average about 50 of the things)

Keep my CSS styles without everything getting messed up?

 

*sigh* I have already disabled E_NOTICE because otherwise the site becomes unusable but should I worry about something like this or just carry on like normal, aka the majority.

Link to comment
Share on other sites

No, it's not a problem with the interpreter.

 

session_start() will automatically detect if a session is already in use. If it is, it will continue to use it, if it isn't, it will start a new one. You *must* call session_start() on every page where you wish to use $_SESSION.

 

Invalid/undefined variables and indexes is simply down to you and your code making assumptions.

<?php

$var = $_POST['var'];

?>

Is making the assumption that $_POST['var'] exists, which it may not.Change it to:

<?php

if (isset($_POST['var'])) 
{
    $var = $_POST['var'];
}
else
{
    echo '$_POST[\'var\'] is not set!';
}

?>

and you will not get the notice error.

Link to comment
Share on other sites

Hi there,

I don't know if I got you right or not but here are my points:

1-session_start has to be used in every page...you have no choice

2-It is recommended that you refrence any post, get, cookies, session, thought the global arrays (i.e. $_POST, $_GET, ...etc).

Link to comment
Share on other sites

Alright. The thing is if I just simply state Session_Start() on every page E_NOTICE has a field day and starts telling me that a session has already been started. I know it gets handled automatically but E_NOTICE feels the need to make me fully aware of it. To circumvent this I'm assuming that you'll have to make use of some check for against the size of the Session array?

 

As for $_POST['var'], if I had to catch it in an if statement every time it's used my code starts becoming messy and really, REALLY loses readability. At the moment I pass most of my $_POST['var'] as parameters in functions so if there is no such post variable then its not a problem. Also the only instances where these variables can be used is where a corresponding session variable is used and I already check that. I also check for null/nonset post variables in my page handlers so thats not an issue either. I guess its just the interpreters way of telling me to do these things 'properly' and in a very inconvenient manner.

 

Would you recommend I try and adhere to the 'guidelines' used by the interpreter or just code normally and ignore them? As far as I can tell, unless the post variables are absolutely critical or can cause huge problems it really shouldn't be necessary to check if they have been set? I dunno, what do you guys with eperience say?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.