473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What is wrong with this piece of code?

17
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo "<table style=\ "border: 1px solid black;\">\n;
  3.         for ($y=1; $y<=12; $y++) {
  4.         echo "<tr> \n";
  5.         for ($x=1; $x<=12; $x++) {
  6.         echo "<td style =\ "border: 1px solid black; width: 25px; padding: 4px;
  7.             text-align:center;\">";
  8.         echo ($x * $y);
  9.         echo "</td> \n";
  10.     }
  11.     echo "</tr> \n";
  12. }
  13. echo "</table>";
  14. ?>
Sep 14 '07 #1
9 1431
whitey
17
i noticed a " was missing after the "n" on line two, but still no joy
Sep 14 '07 #2
ak1dnar
1,584 Expert 1GB
Check this out: There were some errors while closing the the echo Strings.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo "<table style=\"border: 1px solid black;\">\n";
  3. for ($y=1; $y<=12; $y++) {
  4. echo "<tr> \n";
  5. for ($x=1; $x<=12; $x++) {
  6. echo "<td style =\"border: 1px solid black; width: 25px; padding: 4px;
  7. text-align:center;\">";
  8. echo ($x * $y);
  9. echo "</td> \n";
  10. }
  11. echo "</tr> \n";
  12. }
  13. echo "</table>";
  14. ?>
  15.  
EDIT:

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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo '<table style="border: 1px solid black;">';
  3. for ($y=1; $y<=12; $y++) {
  4. echo '<tr>';
  5. for ($x=1; $x<=12; $x++) {
  6. echo '<td style ="border: 1px solid black; width: 25px; padding: 4px;
  7. text-align:center;">';
  8. echo ($x * $y);
  9. echo '</td>';
  10. }
  11. echo '</tr>';
  12. }
  13. echo '</table>';
  14. ?>
  15.  
Sep 14 '07 #3
pbmods
5,821 Expert 4TB
Heya, Whitey.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]
Sep 14 '07 #4
gregerly
192 Expert 100+
EDIT:

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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo '<table style="border: 1px solid black;">';
  3. for ($y=1; $y<=12; $y++) {
  4. echo '<tr>';
  5. for ($x=1; $x<=12; $x++) {
  6. echo '<td style ="border: 1px solid black; width: 25px; padding: 4px;
  7. text-align:center;">';
  8. echo ($x * $y);
  9. echo '</td>';
  10. }
  11. echo '</tr>';
  12. }
  13. echo '</table>';
  14. ?>
  15.  
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
Sep 14 '07 #5
ak1dnar
1,584 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $myvar = 'When_you_evaluate_php_variables';
  3. echo "Yes its easy with double quotes: like this $myvar"; //prints: Yes its easy with double quotes: like this When_you_evaluate_php_variables
  4. echo '<br>';
  5. echo "<font type=\"verdana\" color=\"red\">But This way is hard $myvar With HTML Tags.</font>"; //prints: But This way is hard When_you_evaluate_php_variables With HTML Tags.
  6. // In above line you have to use "Escape" for each ["] character within the HTML coding
  7. // But Its easy with single Quotes, With This Way"
  8. echo '<br>';
  9. echo '<font type="verdana" color="red">But This way is Eazy '.$myvar.' With HTML Tags</font>';
  10. ?>
  11.  
  12.  
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!
Sep 15 '07 #6
pbmods
5,821 Expert 4TB
For large amounts of HTML, it's a lot easier (and potentially more efficient) to use heredoc syntax:
Expand|Select|Wrap|Line Numbers
  1. echo <<<END
  2. <div style="border: 1px solid #ffff00; padding: 8px;">
  3.     With heredoc, you can evaluate all the {$var}s you want, and you can use<br />
  4.     <span style="color: #00ffff">quotes ' " ' " &quot;, too!</span>
  5. </div>
  6. END;
  7.  
The code syntax parser here at TheScripts isn't picking it up, but heredoc is valid:
http://php.net/manual/en/language.ty...syntax.heredoc
Sep 15 '07 #7
ak1dnar
1,584 Expert 1GB
you are so correct pb, and always.
I don't know why I hate this "heredoc".
Sep 15 '07 #8
pbmods
5,821 Expert 4TB
Heya, Ajaxrand.

A good templating engine will defeat the need for heredoc any day. That's why you hate it so :)
Sep 15 '07 #9
gregerly
192 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $myvar = 'When_you_evaluate_php_variables';
  3. echo "Yes its easy with double quotes: like this $myvar"; //prints: Yes its easy with double quotes: like this When_you_evaluate_php_variables
  4. echo '<br>';
  5. echo "<font type=\"verdana\" color=\"red\">But This way is hard $myvar With HTML Tags.</font>"; //prints: But This way is hard When_you_evaluate_php_variables With HTML Tags.
  6. // In above line you have to use "Escape" for each ["] character within the HTML coding
  7. // But Its easy with single Quotes, With This Way"
  8. echo '<br>';
  9. echo '<font type="verdana" color="red">But This way is Eazy '.$myvar.' With HTML Tags</font>';
  10. ?>
  11.  
  12.  
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
Sep 15 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: S.Shaigany | last post by:
Hi everybody, Please help me. I have a problem with my login page, which worked fine during the last one month. I have'nt made any changes in it. but from yesterday on, it seems that my PHP code...
5
by: Fendi Baba | last post by:
Hi everyone, I am new to php. I am using a piece of code which is testing if a certain variable is <=0. and the code is written as follows: $lc_align = 'center'; if ($listing<=0) { $lc_text =...
10
by: Greener | last post by:
Hi, I need help badly. Can you do client-side programming instead of server-side to capture the Browser type info? If this is the case, what's wrong with the following? <script...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
1
by: Nobody | last post by:
I'm trying to write a class where I need to sort a list of TreeNode objects in a certain way. So I thought I'd use: List<ListNodeData> and then use the Sort method. I have the code working...
10
by: bob | last post by:
Hello, I use Microsoft Visual C++ .NET (version 7.1.3088) Sometimes (with big codes?) when I get a compile error and click on the error, the cursor is placed next to the wrong piece of code. The...
18
by: __PPS__ | last post by:
Hello, I'm a university student and I'm preparing for my final today. I'm reading course notes, I found completely strange piece of code. It makes me laugh, I think the teacher needs to prepare...
16
by: SirG | last post by:
I'm looking for an explanation of why one piece of code works and another does not. I have to warn you that this is the first piece of Javascript I've ever written, so if there is a better way or a...
10
by: Enkidu | last post by:
Beginner question, sorry! I am using indexers to access an array of StringBuilders in an instance of a class: Getting: value = board1; Setting: board1 = value1 ;
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.