3

how can I prevent PHP from returning an Undefined variable error every time I try to check a variable if it has contents and that certain variable hasn't been used yet? In my previous setup, I can check $_POST['email'] even if I haven't put anything into it yet. It simply returns a blank or empty result. That's how I want my PHP setup to work but for the life of me, I can't seem to find figure out how to configure it. :(

Example:

<?php
if ($_POST['info'] != '') {
  // do some stuff
}
?>
<form method="post">
<input type="text" name="info" />
<input type="submit" />
</form>

When you use the above script in a single PHP page and run it on my current setup, it returns an Undefined variable error. On my previous setup, that script worked like a charm. Can anyone share some light into this problem of mine. If you need further details, just say so and I will try to add more details to this post.

I know about the isset() checking but I don't want to use it on all of my scripts. I want it to not be that restrictive about variables.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
Registered User
  • 8,706
  • 9
  • 32
  • 40
  • Related: http://stackoverflow.com/questions/1960509/isset-and-empty-make-code-ugly – Pekka Oct 10 '10 at 18:58
  • 1
    That's not an error. It's an E_NOTICE. Many cargo culters don't realize it, but that's a debug message. `error_reporting(E_ALL)` returns many such redundant debug notices, so turn it off. Enable it once in while, when you ARE debugging your code. Don't obfuscate it with isset, as this might obstruse tracing when you need it. Btw, `if ($_POST['info'])` suffices. http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting – mario Oct 10 '10 at 19:13

6 Answers6

4

You could use the empty function, which returns true if the variable is either unset or empty (i.e. a zero-length string, 0, NULL, FALSE and so on):

if(!empty($_POST['variable'])){
    /* ... */
}

This is pretty much the same test as the one you're doing now, except that it will also return false (not run the block), without warnings, when the variable is unset.

You
  • 22,800
  • 3
  • 51
  • 64
3

Most likely it's not an error, but a Warning. You can set PHP not to display errors/warnings if you want. Just add this to the begining of your code:

ini_set('display_errors', 0);

Altough, I recommend you not to be lazy and have your code not display any errors, warnings, notices whatsoever, better safe than sorry.

And the preferred way to do what you want to do is

<?php
if (isset($_POST['info'])) {
  // do some stuff
}
?>
<form method="post">
<input type="text" name="info" />
<input type="submit" />
</form>
Claudiu
  • 3,261
  • 1
  • 15
  • 27
  • 1
    Or actually check if you're handling a POST: `if ($_SERVER['REQUEST_METHOD'] == 'POST') { ... }` – Marc B Oct 11 '10 at 00:02
0

I have sometimes done the following to do a comparison and avoid the warning about an unset variable:

if( @$_POST['variable'] == 'value' )
{
     ...
}

Although PHP would still call custom error handlers, so perhaps I need to reconsider this strategy.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Are you using your own Server? If so you can change the error_reporting via PHP.INI. Above the line

"error_reporting = E_ALL;" (might not be E_ALL) There will be a small documentation explaining which type of errors you can show.

If you're using a shared server from a webhosting company, I believe some may offer quick PHP management under your Webhosting account CP.

I hope I helped ;/

ps- If you do have your own server, you'll have to restart Apache. I'm not sure how it's done in Ubunto (if that's what you're using) but for CentOS it's:

/sbin/service httpd restart
Dhamesh Makwana
  • 109
  • 1
  • 2
  • 14
0

The variable will be always SET as long as you sent the form .

So its better to check of the variable is not empty.

if(!empty($_POST['var']))

Gucho Ca
  • 690
  • 6
  • 17
  • 1
    Depends on the input type. Some types, like checkbox, are not sent in the post submit if they are not checked or in some other way interacted with to give them a value. – Geowil Aug 02 '15 at 23:49
0

If you want to set $_POST vars, having a whole lot of these statements really clutters your code:

if (isset($_POST['info']))
  $info = $_POST['info'];
... etc....

How about this?

function setPostVar( &$v ) {
    $trace = debug_backtrace();
    $vLine = file( __FILE__ );
    $fLine = $vLine[ $trace[0]['line'] - 1 ];
    preg_match( "#\\$(\w+)#", $fLine, $match ); 
    eval("\$v = isset(\$_POST[\"$match[1]\"])?\$_POST[\"$match[1]\"]:'';");
}

Now, as long as you are happy naming variables similar to your POST vars

ie. $Foo = $_POST['Foo'];

you can just do this:

setPostVar($Foo); // equivalent to if(isset($_POST['Foo']) $Foo = $_POST['Foo'];
setPostVar($Bar); // ditto
setPostVar($Baz); // ditto