Just a beginner

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamesbirdsley
Forum Newbie
Posts: 3
Joined: Sun May 31, 2009 1:17 pm

Just a beginner

Post by jamesbirdsley »

I am just starting and i am trying to run a simple loop... for some reason it is not working... any one have any advice?

here is the code.. thanks

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
 
 
 
print '<table>';
print '<tr><th>Farenheit</th><th>Celsius</th></tr>';
for ($far=-50; $far <=50; $far +=5;{
$celsius= ($far -32)*5/9;
print "<tr><td>$far</td><td>$celsuis</td></tr>";
}
print '</table>';
 
 
?>
</body>
</html>
 
Last edited by Weirdan on Sun May 31, 2009 3:07 pm, edited 1 time in total.
Reason: added [code=php] tags
kcormier
Forum Newbie
Posts: 11
Joined: Sun May 31, 2009 1:36 am

Re: Just a beginner

Post by kcormier »

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
 
 
 
print '<table>';
print '<tr><th>Farenheit</th><th>Celsius</th></tr>';
// look below: you were missing ) which I've added & bolded for readability
for ($far=-50; $far <=50; $far +=5[b])[/b]{
    // read note below for following line.
    $celsius= ($far -32)*5/9;
    print "<tr><td>$far</td><td>$celsuis</td></tr>";
}
print '</table>';
 
?>
</body>
</html>
 
I don't know if you're only going to be working in php and that's it or if you plan on going further and learning other languages. In php 5/9 will evaluate correctly, but a lot of languages will see that 5 is an integer and 9 is an integer and as such return the answer rounded to an int (in this case 0). So while it works correctly in php it's a pitfall to be careful of if you ever move over to other languages. If you plan on branching out of php ever I suggest getting into the convention of writing all floating point numbers (numbers with decimal places) with the decimal point. So I'd write that as:

Code: Select all

 
$celsius = ($far-32.0)*5.0/9.0;
 
jamesbirdsley
Forum Newbie
Posts: 3
Joined: Sun May 31, 2009 1:17 pm

Re: Just a beginner

Post by jamesbirdsley »

thanks i appreciate the help...

for some reason it still is not working... i feel like im missing something..

i just save this as a .php and then open it with firefox, and i have wamp running... for somereason the output from firefox is :


'; print 'FarenheitCelsius'; for ($far=-50; $far <=50; $far +=5;){ $celsius= ($far -32)*5/9; print "$far$celsuis"; } print ''; ?>
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Just a beginner

Post by mikemike »

Are you sure WAMP is running correctly and in that directory?

Also, I assume you noted the missing bracket ')' at the end of line 15 - that might be causing your issue.
jamesbirdsley
Forum Newbie
Posts: 3
Joined: Sun May 31, 2009 1:17 pm

Re: Just a beginner

Post by jamesbirdsley »

got it... thanks for the help
Post Reply