Jump to content

Someone explain .= in this context? (solved)


ryandward

Recommended Posts

[Edit: Sorry I found my answer by searching for "period", then finding out that it is a string operator, then read about that. Doesn't seem easy to search for ".=" in Google. Thanks]

 

I got some help here earlier, and I can pretty much understand everything, except I don't understand why there is

$recordOptions .= "<option value=\"{$row['id']}\">";
        $recordOptions .= " {$row['firstname']} {$row['lastname']}, {$row['gender']}, {$row['dob']}";
        $recordOptions .= "</option>\n";

 

Why does this whole thing break without .= ? I am new to PHP so, sorry if the answer is really obvious.

 

$recordOptions = '';
$query = "SELECT * FROM people";
$result = mysql_query($query);
if(!$result)
{
    $recordOptions = "<option>Error Retrieving Records</option>\n";;
}
else
{
    while($row=mysql_fetch_assoc($result))
    {
        $recordOptions .= "<option value=\"{$row['id']}\">";
        $recordOptions .= " {$row['firstname']} {$row['lastname']}, {$row['gender']}, {$row['dob']}";
        $recordOptions .= "</option>\n";
    }
}

?>
<html>
<body>

<?php echo $confirmMsg; ?><br />

<form action ='' method='post'>

Link to comment
Share on other sites

The .= operator basically appends the contents of the right side to the variable, so it'd be the same as doing this:

    while($row=mysql_fetch_assoc($result))
    {
        $recordOptions = $recordOptions."<option value=\"{$row['id']}\">";
        $recordOptions = $recordOptions." {$row['firstname']} {$row['lastname']}, {$row['gender']}, {$row['dob']}";
        $recordOptions = $recordOptions."</option>\n";
    }

Link to comment
Share on other sites

The .= operator basically appends the contents of the right side to the variable, so it'd be the same as doing this:

    while($row=mysql_fetch_assoc($result))
    {
        $recordOptions = $recordOptions."<option value=\"{$row['id']}\">";
        $recordOptions = $recordOptions." {$row['firstname']} {$row['lastname']}, {$row['gender']}, {$row['dob']}";
        $recordOptions = $recordOptions."</option>\n";
    }

 

Omg this is exactly what I needed. Thank you very much! I didn't know that it was cumulative.

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.