Jump to content

Echo .= on var error unless I declare it as "" first ?


OldWest

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.