Forum Moderators: coopster

Message Too Old, No Replies

cannot get rid of undefined var

concat value as str

         

henry0

8:04 pm on Jan 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is what I have, it results in undefined var, although gives the expected result
but I like to get rid of those errors
this time .. to no avail!

first I had this
$category.="<option value=\"$row[category_id]\">$row[category_name]</option>";

then I tried:
$category_id=$row['category_id'];
$category_name=$row['category_name'];

$category.="<option value=$category_id>$category_name</option>";

both work but throw an error.

Using the usual var=""; kills the error but kills the row value as well, so it's not an option

eelixduppy

11:27 pm on Jan 19, 2009 (gmt 0)



Which variable is it saying that is undefined?

henry0

11:31 pm on Jan 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The one with concat

$category.="<.....

eelixduppy

11:36 pm on Jan 19, 2009 (gmt 0)



Ahh I understand now. To get rid of the error you have to initialize $category before the loop you are using (I'm assuming). If you initialize $category in the loop it is going to reset each time, so do it before:


[pre]
$category;
while($row = ....) {
$category.="<option value=\"{$row['category_id']}\">{$row['category_name']}</option>";
....
}
[/pre]

henry0

11:49 pm on Jan 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, same story..
this what I have now

$category;
while ($row=$db->fetch_assoc($result))
{
$category.="<option value=\"{$row['category_id']}\">{$row['category_name']}</option>";
}

eelixduppy

12:17 am on Jan 20, 2009 (gmt 0)



Sorry, you actually have to initialize it to something. Thought you didn't but you do:

$category = '';

henry0

1:13 am on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It works, thanks!
But it's strange I tried it as mentioned in the original post.
Although I did not have the {}; I seldom use them in query
WHY does it make such a difference?

eelixduppy

1:15 am on Jan 20, 2009 (gmt 0)



When you include array values in a string you must surround them with the brackets. Otherwise, you're going to have to take them out of the string entirely and concatenate them into the string. Just the way it works. :)

coopster

10:49 pm on Jan 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry, you actually have to initialize it to something. Thought you didn't but you do

Technically, you are not required to do so, but it will throw a NOTICE error if you don't. There is a note on the PHP variables [php.net] manual page ...

E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array.

... and a bunch of examples listing "what-ifs" when you don't initialize a variable first. Best practice is to always initialize it, and to initialize it to the type that you intend for usage, be that a string, array, boolean, etc.

I believe the part of initializing that is "unsaid" here is the expression. You need to assign an expression to a variable (or reference) otherwise you are going to get the NOTICE errors.

The braces are the secondary part, henry0. Your issue was the uninitialized variable. More on the braces part here ...


You have all kinds of options when it comes to using array indexes in strings, but the easiest, cleanest way to do it is to use the braces as demonstrated, IMHO. For clarification though, you actually don't have to break out of a string expression when using associative indexes within the string expression. You leave the quotation marks off though because constants are not looked for within strings:

$arr = array('fruit' => 'apple', 'veggie' => 'carrot'); 
print "Hello $arr[fruit]"; // Hello apple (no errors)

Still, it gets ugly bouncing back and forth between no quotation marks on string indexes outside of a string expression and then making sure they are there when you are not. Braces syntax within the strings gives your code a bit more ... standard continuity? That sound right? See the array "dos and don'ts" on the arrays [php.net] page for usage and examples. But decide on one and stick with it, you'll be happier. Go with the braces :)