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
https://forums.phpfreaks.com/topic/233036-someone-explain-in-this-context-solved/
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";
    }

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.