views:

131

answers:

6

Hello.

Since i changed my error reporting level to error_reporting(E_ALL | E_STRICT); I am facing this error. I can obviate from this error using isset() but the code looks so ugly!


So my question is: What if I go back to my normal settings of error reporting? does it really matter to know that something is not already defined? because it woks properly without the Notice error.

Because i have +10 inputs and i get them like that:

$username = $_POST['username'];

I also tried to pre-define the variables using this in the top on the file.

$username = null; and $username = 0; but they don't work.

Thanks.

+6  A: 

I would suggest creating a simple function to grab the $_POST values and do the checking for you.

e.g.

<?php
function getPost($key)
{
    return isset($_POST[$key]) ? $_POST[$key] : null;
}

Edit:

Apparently it wasn't clear to the OP how to use this:

$username = getPost('username');
hobodave
+1, even better than just $username = isset($_POST['username']) ? $_POST['username'] : false;
r3zn1k
You'd be better returning null rather than false for missing keys - a key might exist with the value false.
adam
@adam you are correct
hobodave
or specify a default value (which is returned then but if not specified returns null): `function getPost($key, $defaultValue = null)`
Felix Kling
Actually when it returns NULL the error is going to be shown again.Notice: Undefined index: username .. So it didn't solve the issue.
amindzx
I wouldn't recommend this. Maybe you'd want an exception to be thrown if a `$_POST` is missing. Point being, variables should be checked individually.
chelmertz
@amindzx: Then you haven't used it correctly, as `isset` tells you if the key exists or not: http://php.net/manual/en/function.isset.php@chelmertz: Well you can check critical variables individually and make your life easier for the other variables.
Felix Kling
@chelmertz: there's nothing to stop you from throwing an exception if the result of getPost() is null for a critical variable. Throwing exceptions should be left up to the user, and not tied into something as trivial as a fetch from $_POST
hobodave
+2  A: 

It means there is no key 'username' in the POST array.

Generally, it is a good idea to check and correct these things, as they may ripple to other parts in your application that do depend on the missing value.

Gordon
No, it means the key 'username' (or equivalent) is undefined
adam
That's quibble.
Gordon
But you don't have to check with isset. PHP provides array_key_exists, which does exactly what it says on the tin - regardless of the actual value, and Gordon didn't say anything suggesting to use one over the other. That *does* indeed make it quibble.
Michael Madsen
True, the error is only thrown if the key does not exists in the array and not if the value is `null` (I checked it besides that it makes sense anyway). Gordon's answer is totally correct, +1 to for that (and to compensate the downvote ;) ).
Felix Kling
+7  A: 

It does matter. Errors slow down PHP and you really should design you application not to throw errors. Many other languages will completely die in situations where PHP happily continues script execution.

When developing, your script should not throw any errors (even an E_NOTICE).

Inspire
+1 Yeah. That's what i thought. Why i used error reporting is because i read in a blog article that developers should know exactly what's happening with their script. But in a situation like that, it taking my time for nothing.
amindzx
+1  A: 

Notices do have a purpose: they're a tool to detect potential errors in your code. If you write code that triggers notices for trivial operations and you are not willing to change it, you'll have to disable notice reporting and thus reject a helpful tool on purpose and make your work harder than needed.

Historically, PHP was designed with extreme simplicity in mind (in old versions you'd just have an $username available with zero lines of code) but this approach proved highly inadequate as the web evolved: it only lead to code that was insecure and hard to maintain.

Álvaro G. Vicario
+1  A: 

It does matter -- when I get strange behaviour in a php application the error log is the first place I look and nine times out of ten an "UNDEFINED INDEX" message leads me straight to the root cause.

James Anderson
+1  A: 

All errors should be addressed, no matter the level, for portability.

If you build your application not addressing strict errors, and your application is deployed on a server that does have strict error reporting, your application is going to fall over pretty quickly.

Your best bet is to check the existence of $_POST['username'] and then act independently on that return value. Using isset() your return value with either be true or false.

I'm guessing $_POST['username'] is for use in an authentication system of some description? Therefore, if your isset() function returns false you could then display an error detailing to the user that username is required.

Martin Bean