nickia
08-25-2009, 10:05 PM
Hi I'm learning PHP using HeadFirst PHP&MYSQL but now I ran into a slight problem while trying to understand this script.
echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
Can anyone tell me why there is a dot infront and behind $row['id']? This script wouldn't work without the two dots as it returns some syntax error.
I know that '.' means concatenate in PHP but I don't know why it's needed in this case.
Thanks.
I need to read better. :blush:
It's needed here so the dynamic part (in bold) can be parsed by PHP.
echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
nickia
08-25-2009, 10:16 PM
Thanks but I still don't get it. Are dots used as concatenation in this case ? If yes, what is it concatenating with? The two single-quotation marks?
echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
PHP code within the single quotes won't get parsed, so it's taking the first half of the echo statement and wrapping it within single quotes (because it contains double quotes). It then takes the dynamic part, parses it, and strings it together with the first part and the last part to make one long echo string...with the dynamically generated content in the middle.
nickia
08-26-2009, 01:46 AM
OMg thanks now I see the difference. I had thought that the entire echo statement was one part instead of 3 parts. Is there any other way to achieve the same result without using concatenation? Is there any benefit of using concatenation because I don't really like it since it makes things messy...
xphoid
08-26-2009, 02:27 AM
There's a few different ways. see: http://php.net/manual/language.types.string
Or use a function like: http://php.net/manual/function.sprintf
Neseema M M
08-26-2009, 02:41 AM
Try like this:
echo "<input type=\"checkbox\" value=\"{$row['id']}\" name=\"todelete[]\" />";
Hope it will help.
nickia
08-26-2009, 03:49 AM
Thanks everyone. I guess I will have to get used to these quotation rules.
mattle
08-26-2009, 08:15 AM
Try like this:
echo "<input type=\"checkbox\" value=\"{$row['id']}\" name=\"todelete[]\" />";
Hope it will help.
Or, my personal preference for clean code, use the nowdoc syntax (http://us2.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc)
echo <<<HTML
<input type="checkbox" value="{$row['id']}" name="todelete" />
HTML;