OldWest Posted December 9, 2010 Share Posted December 9, 2010 I am running through a tutorial and I am getting an error based on a the concatication operator on my output. Below is my code. As you can see I am echoing $display_block .= "<p>$title<br>$rec_label<br>.... If I send this as-is I get this error: Notice: Undefined variable: display_block in C:\wamp\www\php\php_mysql\sel_byid.php on line 36 I can resolve the error by placing this before the while loop: $display_block = ""; Is there a better way to output the concatenation.= so I don't need to do this weird fix? while ($row = mysql_fetch_array($result)) { $id = $row['id']; $format = $row['format']; $title = stripslashes($row['title']); $artist_fn = stripslashes($row['artist_fn']); $artist_ln = stripslashes($row['artist_ln']); $rec_label = stripslashes($row['rec_label']); $my_notes = stripslashes($row['my_notes']); $date_acq = $row['date_acq']; if ($artist_fn != "") { $artist_fullname = trim("$artist_fn $artist_ln"); } else { $artist_fullname = trim("$artist_ln"); } if ($date_acq == "0000-00-00") { $date_acq = "[unknown]"; } $display_block .= "<p>$title<br>$rec_label<br>$artist_fullname<br>$my_notes<br>$date_acq<br>$format</p>"; Link to comment https://forums.phpfreaks.com/topic/221083-echo-on-var-error-unless-i-declare-it-as-first/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 9, 2010 Share Posted December 9, 2010 There's nothing weird about declaring a variable before you reference it. Link to comment https://forums.phpfreaks.com/topic/221083-echo-on-var-error-unless-i-declare-it-as-first/#findComment-1144749 Share on other sites More sharing options...
chronister Posted December 9, 2010 Share Posted December 9, 2010 There's nothing weird about declaring a variable before you reference it. I have always wondered why this is. PHP is my first and only programming language, so I am pretty green when it comes to programming patterns, standards and other best practice type stuff. So could you explain why $somevar = 'foo'; will throw a Notice, but $somevar = ''; $somevar = 'foo'; will be accepted. I guess I am not clear on why setting the var to an empty string first is any different than setting it to an actual value. Thanks, nate Link to comment https://forums.phpfreaks.com/topic/221083-echo-on-var-error-unless-i-declare-it-as-first/#findComment-1144770 Share on other sites More sharing options...
trq Posted December 9, 2010 Share Posted December 9, 2010 $somevar = 'foo'; Will not throw any notice. it's only if you try and concatenate to a variable that doesn't exist yet. Link to comment https://forums.phpfreaks.com/topic/221083-echo-on-var-error-unless-i-declare-it-as-first/#findComment-1144771 Share on other sites More sharing options...
chronister Posted December 9, 2010 Share Posted December 9, 2010 Ohhhh.. ok.. that makes sense... can't concatenate to a var that don't exist. I have always heard that it is "good coding" to declare a var before you assign a value to it. I thought I had gotten notices for this crap before.... must have been some other stupid mistake Thanks, nate Link to comment https://forums.phpfreaks.com/topic/221083-echo-on-var-error-unless-i-declare-it-as-first/#findComment-1144776 Share on other sites More sharing options...
OldWest Posted December 9, 2010 Author Share Posted December 9, 2010 There's nothing weird about declaring a variable before you reference it. It just seems ultra redundant. Link to comment https://forums.phpfreaks.com/topic/221083-echo-on-var-error-unless-i-declare-it-as-first/#findComment-1144784 Share on other sites More sharing options...
chronister Posted December 9, 2010 Share Posted December 9, 2010 There's nothing weird about declaring a variable before you reference it. It just seems ultra redundant. If you look at my last post... your trying to concatenate to a variable that does not exist on the first time round. Programming logic needs to have a variable declared before it can reference it. You can't really do $x + $y if $x does not exist. Any programming language will have an issue trying to use something that is not declared. Link to comment https://forums.phpfreaks.com/topic/221083-echo-on-var-error-unless-i-declare-it-as-first/#findComment-1144789 Share on other sites More sharing options...
OldWest Posted December 9, 2010 Author Share Posted December 9, 2010 There's nothing weird about declaring a variable before you reference it. It just seems ultra redundant. If you look at my last post... your trying to concatenate to a variable that does not exist on the first time round. Programming logic needs to have a variable declared before it can reference it. You can't really do $x + $y if $x does not exist. Any programming language will have an issue trying to use something that is not declared. I get it. It just feels unnatural to create an empty value for that reason. It would be nice if there was a specialized character for loop structure concatenation like: =.= or something that was used specifically for this type of thing, just to at least cut down on the extra code. IMHO. Link to comment https://forums.phpfreaks.com/topic/221083-echo-on-var-error-unless-i-declare-it-as-first/#findComment-1144831 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.