1

So I have created a PHP page that will prompt the user to enter 2 Celsius temperatures (start & stop) and a number to increment between the two. Then it loops through the numbers and displays the Fahrenheit conversion of each number base on the increment in a table format. The problem i am currently having is that I am trying to put the table display in a variable so that i can display it below the number inputs but at the moment i am only able to display it above. I think the problem is that the variable isn't recognizing the table because when i try to echo it below it just says "1". I've tried putting the entire table in quotes and a bracket but it is still not working.

Here is the part of the code where the table comes in:

    if ($start_error == "" && $stop_error == "" && $inc_error == "") {

        $result = print "<table width=\"25%\" border =\"1\" style='border-collapse: collapse'>";
                  print "<tr><th>Celsius</th>";
                  print "<th>Fahrenheit</th></tr>";
                  for ($row=1; $row <= 1; $row++) { 
                      for ($c = $start; $c <= $stop; $c+=$inc) {
                      print "<tr> \n"; 
                      $f = $c * 1.8 +32;
                          for ($col=1; $col <= 1; $col++) { 
                                print "<td align=\"center\">$c&deg;</td> \n"; 
                                print"<td align=\"center\">$f&deg;</td> \n"; 
                           } 
                         print "</tr>"; 
                      } 
                   print "</table>";    
                   }

    }

}

?>

<h2><?php echo $start_error; ?></h2>
<h2><?php echo $stop_error; ?></h2>
<h2><?php echo $inc_error; ?></h2>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
    Starting Temperature:  <input type="text" name="start_number" value="<?php echo $start; ?>" size="5" /><br/>
    Stop Temperature:  <input type="text" name="stop_number" value="<?php echo $stop; ?>" size="5" /><br/>
    Temperature Increment:  <input type="text" name="inc_number" value="<?php echo $inc; ?>" size="5" /><br/>
    <input type="submit" value="Create Temperature Conversion Table" />


</form>

<?php echo $result; ?>

And here is an image of what the result looks like:

4
  • As documented print always returns 1. You don't use print for the return value. Commented Jun 28, 2017 at 16:59
  • @RiggsFolly I tried removing the prints but now it doesn't display anything. Commented Jun 28, 2017 at 17:02
  • See my answer for more details Commented Jun 28, 2017 at 17:02
  • And instead of ; at the ends use . to concatenate the strings to one (of course at the end of the last line you let ;) Commented Jun 28, 2017 at 17:04

2 Answers 2

2

Remove the print statements that directs output immediately and replace with text concatenation into the $result variable

if ($start_error == "" && $stop_error == "" && $inc_error == "") {

    $result = "<table width=\"25%\" border =\"1\" style='border-collapse: collapse'>";
    $result .= "<tr><th>Celsius</th>";
    $result .= "<th>Fahrenheit</th></tr>";
    for ($row=1; $row <= 1; $row++) { 
        for ($c = $start; $c <= $stop; $c+=$inc) {
            $result .= "<tr> \n"; 
            $f = $c * 1.8 +32;
            for ($col=1; $col <= 1; $col++) { 
                $result .= "<td align=\"center\">$c&deg;</td> \n"; 
                $result .= "<td align=\"center\">$f&deg;</td> \n"; 
            } 
            $result .= "</tr>"; 
        } 
        $result .= "</table>";    
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Makes a nice change eh @Fred-ii-
Yes indeed @RiggsFolly
0

The print function sends a string directly to the output buffer (the browser), and returns "1" to confirm it completed. By setting $result = print [...], you are having print perform its actions (sending the string to the browser), and then assigning the returned value ("1") to $result.

In other words, print is going to output the data to the buffer, rather than return a string for you to assign to your variable. The only value your $result variable has is what gets returned by print, which is "1".

You have two options:

1) Remove the call to print, and just set $result to your concatenated strings, as @RiggsFolly suggested.

2) You can keep the print as is, and remove the use of your $result variable, which currently only stores the returned value ("1") from your call to print.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.