larka06 Posted August 16, 2013 Share Posted August 16, 2013 I have two questions; One: I have looked at this code for two days and can not find why it will not run. Can someone tell me what is wrong? Two I think this wrox php is moving to fast for me as a newbie. Can anyone recommend a book that maybe starts out a little slower? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Fibonacci sequence</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8"/> <meta http-equiv="content-style-type" content="text/css"/> <link href="common.css" rel="stylesheet" type="text/css"/> <style type="text/css"> th{text-align:left; background-color: #999;} th, td{padding: 0.4em;} tr.alt td{background: #ddd;} </style> </head> <body> <h2>Fibonacci sequence</h2> <table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;"> <tr> <th>Sequence #</th> <th>Value</th> </tr> <tr> <td>F<sub>0</sub></td> <td>0</td> </tr> <tr class="alt"> <td>F<sub>1</sub></td> <td>1</td> </tr> <?php $iterations = 10; $num1 = 0; $num2 = 1; for($i=2; $i <= $iterations; $i++) { $sum = $num1 + $num2; $num1 = $num2; $num2 = $sum; ?> <tr<?php if($i % 2 != 0) echo 'class="alt"' ?>> <td>F<sub><?php echo $i?></sub></td> <td><?php echo $num2?></td> </tr> } </table> </body> </html> I am not sure I did that code thing right? Quote Link to comment https://forums.phpfreaks.com/topic/281261-wrox-php-needs-help-agian/ Share on other sites More sharing options...
Solution boompa Posted August 16, 2013 Solution Share Posted August 16, 2013 You are missing the PHP tags around the closing } in your loop. Quote Link to comment https://forums.phpfreaks.com/topic/281261-wrox-php-needs-help-agian/#findComment-1445448 Share on other sites More sharing options...
Psycho Posted August 17, 2013 Share Posted August 17, 2013 (edited) Plus, you have lines of code that aren't closed with semi-colons. I don't know if there is some requirement of WROX to write code like that - but that's a terrible format in my opinion. I think this looks and works much better. <?php $iterations = 10; $num1 = 0; $num2 = 1; $output = ''; for($i=0; $i<=$iterations; $i++) { $class = ($i%2) ? 'alt' : ''; $sum = $num1 + $num2; $output .= "<tr class=\"{$class}\">"; $output .= "<td>F<sub>{$i}</sub></td>"; $output .= "<td>{$sum}</td>\n"; $output .= "</tr>\n"; $num1 = $num2; $num2 = $sum; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Fibonacci sequence</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8"/> <meta http-equiv="content-style-type" content="text/css"/> <link href="common.css" rel="stylesheet" type="text/css"/> <style type="text/css"> th{text-align:left; background-color: #999;} th, td{padding: 0.4em;} tr.alt td{background: #ddd;} </style> </head> <body> <h2>Fibonacci sequence</h2> <table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;"> <tr><th>Sequence #</th><th>Value</th></tr> <?php echo $output; ?> </table> </body> </html> Edited August 17, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/281261-wrox-php-needs-help-agian/#findComment-1445450 Share on other sites More sharing options...
larka06 Posted August 17, 2013 Author Share Posted August 17, 2013 Thanks to both of you for your time. I put the php tags around the ending brace and it worked! Suprised yes because I had just taken them off but must of done something else because it works. Quote Link to comment https://forums.phpfreaks.com/topic/281261-wrox-php-needs-help-agian/#findComment-1445457 Share on other sites More sharing options...
mac_gyver Posted August 17, 2013 Share Posted August 17, 2013 the missing php tags would have resulted in a fatal php parse error. do you have php's error_reporting set to E_ALL and display_errors set to ON in your php.ini (putting these settings in your script won't show fatal parse errors) so that php will help you by reporting and displaying all the errors it detects? Quote Link to comment https://forums.phpfreaks.com/topic/281261-wrox-php-needs-help-agian/#findComment-1445523 Share on other sites More sharing options...
larka06 Posted August 17, 2013 Author Share Posted August 17, 2013 Thanks for the advice on the php.ini file. I did as you say and will try it out now. As for me I am having more success now although, I was about to give up php. I like its' style alot like C. Quote Link to comment https://forums.phpfreaks.com/topic/281261-wrox-php-needs-help-agian/#findComment-1445556 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.