What is wrong with this piece of code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whitey
    New Member
    • Sep 2007
    • 17

    What is wrong with this piece of code?

    [code=php]<?php
    echo "<table style=\ "border: 1px solid black;\">\n;
    for ($y=1; $y<=12; $y++) {
    echo "<tr> \n";
    for ($x=1; $x<=12; $x++) {
    echo "<td style =\ "border: 1px solid black; width: 25px; padding: 4px;
    text-align:center;\" >";
    echo ($x * $y);
    echo "</td> \n";
    }
    echo "</tr> \n";
    }
    echo "</table>";
    ?>[/code]
    Last edited by pbmods; Sep 14 '07, 12:53 PM. Reason: Added CODE tags.
  • whitey
    New Member
    • Sep 2007
    • 17

    #2
    i noticed a " was missing after the "n" on line two, but still no joy

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Check this out: There were some errors while closing the the echo Strings.
      [CODE=php]
      <?php
      echo "<table style=\"border: 1px solid black;\">\n";
      for ($y=1; $y<=12; $y++) {
      echo "<tr> \n";
      for ($x=1; $x<=12; $x++) {
      echo "<td style =\"border: 1px solid black; width: 25px; padding: 4px;
      text-align:center;\" >";
      echo ($x * $y);
      echo "</td> \n";
      }
      echo "</tr> \n";
      }
      echo "</table>";
      ?>
      [/CODE]

      EDIT:

      ALWAYS use single quotes instead of double quotes. Then no more "Escape" (\) characters.

      [CODE=php]
      <?php
      echo '<table style="border: 1px solid black;">';
      for ($y=1; $y<=12; $y++) {
      echo '<tr>';
      for ($x=1; $x<=12; $x++) {
      echo '<td style ="border: 1px solid black; width: 25px; padding: 4px;
      text-align:center;"> ';
      echo ($x * $y);
      echo '</td>';
      }
      echo '</tr>';
      }
      echo '</table>';
      ?>
      [/CODE]

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Whitey.

        Please use CODE tags when posting source code:

        &#91;CODE=ph p]
        PHP code goes here.
        &#91;/CODE]

        Comment

        • gregerly
          Recognized Expert New Member
          • Sep 2006
          • 192

          #5
          Originally posted by ajaxrand
          EDIT:

          ALWAYS use single quotes instead of double quotes. Then no more "Escape" (\) characters.

          [CODE=php]
          <?php
          echo '<table style="border: 1px solid black;">';
          for ($y=1; $y<=12; $y++) {
          echo '<tr>';
          for ($x=1; $x<=12; $x++) {
          echo '<td style ="border: 1px solid black; width: 25px; padding: 4px;
          text-align:center;"> ';
          echo ($x * $y);
          echo '</td>';
          }
          echo '</tr>';
          }
          echo '</table>';
          ?>
          [/CODE]
          It is worth it to note however, that if there is a variable inside single quotes, it will not be evaluated. I typically use " to start and end an echo, and single quotes within it, as this allows variables to be evaluated.

          for the OP:

          [PHP]<?
          $var = "HELLO WORLD";
          echo "$var";//echo's HELLO WORLD
          echo "<br />";
          echo '$var'; //echo's $var
          echo "<br />";
          echo "123 '$var'"; // echo's 123 'HELLO WORLD'
          ?>[/PHP]
          Greg

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Originally posted by gregerly
            It is worth it to note however, that if there is a variable inside single quotes, it will not be evaluated. I typically use " to start and end an echo, and single quotes within it, as this allows variables to be evaluated.

            for the OP:

            [PHP]<?
            $var = "HELLO WORLD";
            echo "$var";//echo's HELLO WORLD
            echo "<br />";
            echo '$var'; //echo's $var
            echo "<br />";
            echo "123 '$var'"; // echo's 123 'HELLO WORLD'
            ?>[/PHP]
            Greg
            [CODE=php]
            <?php
            $myvar = 'When_you_evalu ate_php_variabl es';
            echo "Yes its easy with double quotes: like this $myvar"; //prints: Yes its easy with double quotes: like this When_you_evalua te_php_variable s
            echo '<br>';
            echo "<font type=\"verdana\ " color=\"red\">B ut This way is hard $myvar With HTML Tags.</font>"; //prints: But This way is hard When_you_evalua te_php_variable s With HTML Tags.
            // In above line you have to use "Escape" for each ["] character within the HTML coding
            // But Its easy with single Quotes, With This Way"
            echo '<br>';
            echo '<font type="verdana" color="red">But This way is Eazy '.$myvar.' With HTML Tags</font>';
            ?>

            [/CODE]

            Think about a situation that you have to "echo" (only echo) some CSS/HTML tags with Php. Then what is so easier "Double quotes" or "single quotes" ? Real world Web application, ALWAYS not pure php. Its combination of PHP,HTML,CSS. So are you going to escape the double quotes when echoing? Its good for hello world Apps!

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              For large amounts of HTML, it's a lot easier (and potentially more efficient) to use heredoc syntax:
              [code=php]
              echo <<<END
              <div style="border: 1px solid #ffff00; padding: 8px;">
              With heredoc, you can evaluate all the {$var}s you want, and you can use<br />
              <span style="color: #00ffff">quotes ' " ' " &quot;, too!</span>
              </div>
              END;
              [/code]

              The code syntax parser here at TheScripts isn't picking it up, but heredoc is valid:

              Comment

              • ak1dnar
                Recognized Expert Top Contributor
                • Jan 2007
                • 1584

                #8
                you are so correct pb, and always.
                I don't know why I hate this "heredoc".

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, Ajaxrand.

                  A good templating engine will defeat the need for heredoc any day. That's why you hate it so :)

                  Comment

                  • gregerly
                    Recognized Expert New Member
                    • Sep 2006
                    • 192

                    #10
                    Originally posted by ajaxrand
                    [CODE=php]
                    <?php
                    $myvar = 'When_you_evalu ate_php_variabl es';
                    echo "Yes its easy with double quotes: like this $myvar"; //prints: Yes its easy with double quotes: like this When_you_evalua te_php_variable s
                    echo '<br>';
                    echo "<font type=\"verdana\ " color=\"red\">B ut This way is hard $myvar With HTML Tags.</font>"; //prints: But This way is hard When_you_evalua te_php_variable s With HTML Tags.
                    // In above line you have to use "Escape" for each ["] character within the HTML coding
                    // But Its easy with single Quotes, With This Way"
                    echo '<br>';
                    echo '<font type="verdana" color="red">But This way is Eazy '.$myvar.' With HTML Tags</font>';
                    ?>

                    [/CODE]

                    Think about a situation that you have to "echo" (only echo) some CSS/HTML tags with Php. Then what is so easier "Double quotes" or "single quotes" ? Real world Web application, ALWAYS not pure php. Its combination of PHP,HTML,CSS. So are you going to escape the double quotes when echoing? Its good for hello world Apps!
                    Hey Ajaxrand,

                    I wasn't trying to discount your advice, you are correct. When echoing HTML I usually use double quotes to start the string and single quotes for my html. Works just the same. That's what I was trying to get across.

                    Thanks,

                    Greg

                    Comment

                    Working...