Correctly defining $vars & catching Notice Errors

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
Riquez
Forum Newbie
Posts: 10
Joined: Sun Feb 17, 2008 6:39 pm

Correctly defining $vars & catching Notice Errors

Post by Riquez »

This is an oddly noobish question for me, but I've been unable to find a satisfactory answer. I hope someone here can help.

I would like to know the correct method to define & check for variables, arrays & variable variables to avoid error reporting Notice being thrown.

Over the years I've developed a few bad coding habits & I discovered many old scripts are throwing Notice warnings for undefined vars.
Most of them I can easily fix by first declaring the $var as an empty string or 0 or array()

Code: Select all

$var="";
if($var=="greeting"){
 echo "hello";
}
but then there are situations where it would be wrong to declare it, such as $_POST['var'], so I use this method to catch the notice.

Code: Select all

if(!empty($_POST['var']) && $_POST['var']=="test"){
 echo "this is a test";
}
Just want to check this is OK.

Also, while working on this, I found a behaviour which seemed odd.

I found an old page which is used for creating products in a database & it was throwing several Notice errors. The page is used for both NEW products & EDITING existing products.
In the case of the product ID being passed in, it would get product data in the array $line, if no ID is passed in it must be a new product and so $line doesnt exist.

Code: Select all

<th>Product Name</th>
<td><input name="ProductName" type="text" value="<?php echo $line['ProductName']; ?>" /></td>
So we get a "Notice" if the ID isnt passed in. I can define $line as an array, but then I will get "Notice: Undefined index" for each key used in this product table. I dont want to define every key so I just put

Code: Select all

global $line;
at the top of the page. & all Notice warnings & Undefined Key warnings were eliminated!!
I have never used "global" in this way before, I would normally only use it inside a function to reference external $vars.

Is this a valid use? Why is it suppressing all Notices? If I declare like this instead...

Code: Select all

$line=array();
I will still get "undefined index" so declaring global seems to be a golden gift to solve this type of issue.

Hope this isnt too long winded, just want to correctly understand & handle vars like this. I should know better.


Note: I realise I can suppress the Notice warnings, but this exercise is to eliminate them completely.
dejan
Forum Newbie
Posts: 8
Joined: Tue Dec 21, 2010 6:11 am

Re: Correctly defining $vars & catching Notice Errors

Post by dejan »

if(!empty($_POST['var']) && $_POST['var']=="test"){
echo "this is a test";
}
empty is a bit tricky. E.g.

Code: Select all

$var = ""; // empty($var) returns true
$var = 0; // empty($var) also returns true
So, empty($var) will return false positives for you. E.g. say that a person in a form chooses from "Number of children: 0, 1, 2, 3 or more" and chooses 0.

empty($_POST['children']) will return TRUE, although the value in it will be "0" (string).

You are probably better off using isset. It only returns false if
a) the variable was never defined.
b) the variable has a value of NULL.

so:

Code: Select all

$var = ""; // isset($var) returns true
$var = 0; // isset($var) returns true
$var = "0"; // isset($var) returns true
$var = NULL or $var not set at all returns false
Hope that helps
Last edited by Benjamin on Thu Jan 13, 2011 3:06 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
Riquez
Forum Newbie
Posts: 10
Joined: Sun Feb 17, 2008 6:39 pm

Re: Correctly defining $vars & catching Notice Errors

Post by Riquez »

Thanks.
I should probably have used isset() in the example as its more explicit, but yes, I am aware of the differences.

You're right though, it's better working practice to use isset() in most cases.

It brings me to question the efficiency of php in situations like this though, wouldn't it be much nicer to write

Code: Select all

if($_POST['var']){
It does work of course, but it will throw a notice. To me its much more compact code than having to effectively double-check everything to eliminate Notices.

Anyway, thanks - you have confirmed that its OK to do that. I already sort of knew it was, but it just niggles me.

If anyone would like to comment on the 2nd part, declaring

Code: Select all

global $line
in open code suppresses undefined var & undefined index all at once, that will give me some more confidence with this.
Post Reply