Results 1 to 3 of 3

Thread: Temparature conversion with php

  1. #1
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Arrow Temparature conversion with php

    The following code is not working .It is giving an error in the last line. But I have checked the code repeatedly & I can't find any.
    Any advice pls?
    Code:
    <?php
    $f=$c= "";
    if(isset($_POST['f'])) $f=sanitizeString($_POST['f']);
    if(isset($_POST['c'])) $c=sanitizeString($_POST['c']);
    
    if($f!= '')
    {
     $c=intval((5/9)*($f-32));
     $out="$f degree farenheit equals $c degree celsius";
     }
     elseif($c!= '')
     {
     $f=intval((9/5)* $c + 32);
     $out="$c degree celsius equals $f degree farenheit";
     }
     else $out= "";
     
     
     echo <<<_END
     <html><head><title>Temparature Converter</title>
     </head><body><pre>
     Enter either Fahrenheit or Celsius and click on Convert
     <b>$out</b>
     <form method="post" action="convert.php">
     Fahrenheit <input type="text" name="f" size="7"/>
     Celsius <input type="text" name="c" size="7"/>
      <input type="submit"  value="convert" />
      </form>
      </pre>
      </body>
      </html>
      _END;
      
      function sanitizeString($var)
      {
      $var=stripslashes($var);
      $var=htmlentities($var);
      $var=strip_tags($var); 
      return $var;
      } 
    ?>
    Last edited by dcr33; 09-11-2011 at 04:07 PM.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You need to use your end-of-file string terminator at the beginning of a line with no characters before it. That includes spaces.

    Wrong:
    PHP Code:
    echo <<<_END
    string here
           _END; 
    Right:
    PHP Code:
    echo <<<_END
    string here
    _END; 
    This code should work:
    PHP Code:
    <?php
    $f
    =$c"";
    if(isset(
    $_POST['f'])) $f=sanitizeString($_POST['f']);
    if(isset(
    $_POST['c'])) $c=sanitizeString($_POST['c']);

    if(
    $f!= '')
    {
     
    $c=intval((5/9)*($f-32));
     
    $out="$f degree farenheit equals $c degree celsius";
     }
     elseif(
    $c!= '')
     {
     
    $f=intval((9/5)* $c 32);
     
    $out="$c degree celsius equals $f degree farenheit";
     }
     else 
    $out"";
     
     
     echo <<<_END
     <html><head><title>Temparature Converter</title>
     </head><body><pre>
     Enter either Fahrenheit or Celsius and click on Convert
     <b>
    $out</b>
     <form method="post" action="convert.php">
     Fahrenheit <input type="text" name="f" size="7"/>
     Celsius <input type="text" name="c" size="7"/>
      <input type="submit"  value="convert" />
      </form>
      </pre>
      </body>
      </html>
    _END;
      
      function 
    sanitizeString($var)
      {
      
    $var=stripslashes($var);
      
    $var=htmlentities($var);
      
    $var=strip_tags($var); 
      return 
    $var;
      } 
    ?>
    - Josh

  3. The Following User Says Thank You to JShor For This Useful Post:

    dcr33 (09-11-2011)

  4. #3
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks Brother ,it works now.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •