1

I've-yet-to-meet About 30 years ago, I wrote, in Commodore basic, a small arithmetic program for my children and have been trying to "replicate" it using html & php. Using this code it does what I want, when the file(test.php) is run in IE8, i.e. it displays the value of the variables (except the \r\n do not give a line break), Like so: What is 4 + 15? 4 + 15 = 19 However, this is not the case when the same code, including tags is inserted into an html page; below A web page

<body bgcolor="#fefee8">
<p><H1 align="center"> <font color="#00c630">Page header</font></H1> </p>       
<HR> <!HR provides horizontal line below the TOPH>
</body>

<BR><!line break tag
</font> </br> 
<body>  
<P align="center"> <font color="#707070">
<div>
<?php
$a=rand (2, 20);
$b=rand (2, 20);
$c=$a+$b;
print "What is $a + $b? \r\n";
print " $a + $b = $c";
?>
</div>
</Body> 
</html>

I am teaching myself the html and PHP as I go along, and have scoured the search engines for guidance but I cannot find much succour there, so appeal to you. My resources are Notepad++ as my editor, Apache as my localhost web server, and my guidebooks are SAMS Teach yourself PHP and IDG's Teach yourself HTML 4 (yes I know they are old). My hardware is Lenovo Thinkpad, Win XP (SP3) with IE8. It never was this difficult with Basic, (but the graphics sucked) :-), Any help is greatly appreciated.

2 Answers2

0

I think the problem you are having is caused because you are using your code in a .html document. If you save the document as a PHP file (.php) with your notepad++ then your code will work :)

  • @patricksweeney Yes, look at the title.. displaying php variable on browser is OK but does not work when inserted into html page.. PHP variable, in a HTML page.. His code is fine, if used in a PHP file.. –  Mar 04 '14 at 17:28
  • @user3356094. Spot on, my friend. Simply saving it as .php and it works a treat. Now to find out what's next; as I said much easier with BASIC :D, Thank you. – user3379909 Mar 05 '14 at 12:34
  • Well hopefully the original poster noted that they specifically stated they saved it as .php originally - gotta love when OP says when thing when they mean another :) – patricksweeney Mar 26 '14 at 02:13
0

Using double quotation marks (") (the real name escapes me), PHP does not take it liturally. Try using:

print 'What is ' . $a . ' + ' . $b . '?<br>';
print $a . ' +  ' . $b . ' = ' . $c;

< br > is a break in the display.

James Elliott
  • 1,012
  • 9
  • 20
  • Using single quotes and variables directly will not work - http://stackoverflow.com/a/3446286/1603275 – Kami Mar 04 '14 at 17:19
  • @Kami my mistake, it was a long day. Edited answer to reflect this. – James Elliott Mar 05 '14 at 09:59
  • @Kami and James. Thank you both. FYI, it works OK with double quotes and thanks for the
    update, but the "fixer" was saving it as .php not as .html. Doesn't matter how old one gets, one lives and learns so thanks again. I guess this Q can be closed now??
    – user3379909 Mar 05 '14 at 12:40
  • @user3379909 Ah, did not see that one coming. I assumed you had saved it as php, lol. To mark the question as closed, click the accept answer link next to the response that answered your question. – Kami Mar 05 '14 at 13:03