Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2008
    Posts
    128

    Basic PHP Syntax Question PLEASE HELP!

    Hi I'm learning PHP using HeadFirst PHP&MYSQL but now I ran into a slight problem while trying to understand this script.

    Code:
        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.

  2. #2
    Join Date
    Oct 2002
    Location
    /roof/ledge
    Posts
    28,088
    I need to read better.
    It's needed here so the dynamic part (in bold) can be parsed by PHP.

    echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
    Last edited by bear; 08-25-2009 at 10:12 PM.
    Your one stop shop for decentralization

  3. #3
    Join Date
    Jun 2008
    Posts
    128
    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?

  4. #4
    Join Date
    Oct 2002
    Location
    /roof/ledge
    Posts
    28,088
    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.
    Your one stop shop for decentralization

  5. #5
    Join Date
    Jun 2008
    Posts
    128
    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...

  6. #6
    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

  7. #7
    Join Date
    Jun 2009
    Posts
    35
    Try like this:

    PHP Code:

    echo "<input type=\"checkbox\" value=\"{$row['id']}\" name=\"todelete[]\" />"
    Hope it will help.
    Neseema M M
    Ezeelogin - The ultimate multiple server administration software.
    * Parallel shell * rm -rf protection * SSH logging * automated password changes * encrypted storage *
    AdMod.com - Delivering innovative web hosting solutions

  8. #8
    Join Date
    Jun 2008
    Posts
    128
    Thanks everyone. I guess I will have to get used to these quotation rules.

  9. #9
    Join Date
    May 2009
    Posts
    766
    Quote Originally Posted by Neseema M M View Post
    Try like this:

    PHP Code:

    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/languag....syntax.nowdoc)

    PHP Code:
    echo <<<HTML
    <input type="checkbox" value="{$row['id']}" name="todelete" />
    HTML; 

Similar Threads

  1. Very simple php syntax question.Plz help me check.
    By Zach185 in forum Programming Discussion
    Replies: 9
    Last Post: 05-01-2006, 11:10 AM
  2. DNS proper SYntax Question
    By neoshell in forum Hosting Security and Technology
    Replies: 3
    Last Post: 01-12-2004, 09:56 PM
  3. mysql_query syntax question
    By Jeanco in forum Programming Discussion
    Replies: 6
    Last Post: 12-26-2003, 07:31 AM
  4. stuck on syntax of a basic mysql command
    By Derrick in forum Programming Discussion
    Replies: 30
    Last Post: 12-01-2003, 01:42 PM
  5. mod_rewrite syntax question
    By RandallKent in forum Hosting Security and Technology
    Replies: 0
    Last Post: 04-02-2003, 10:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •