Author Topic: If statement with Numbers  (Read 345 times)

0 Members and 1 Guest are viewing this topic.

Offline tarletonTopic starter

  • Irregular
  • Posts: 42
    • View Profile
If statement with Numbers
« on: March 18, 2010, 11:47:37 PM »
Hello.
Here is my statement:
if ($bmi = < 18.5)
	
	
echo 
"You are Underweight" ;
elseif (
$bmi = > 18.524.9)
	
	
echo 
"You are Normal Weight" ;
elseif (
$bmi = > 2529.9)
	
	
echo 
"You are Overweight" ;
elseif (
$bmi = > 30)
	
	
echo 
"You are obese" ;
	

?>


But I get  Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\W06\bmi.php  on line 40
FYI Line 40 =
if ($bmi = < 18.5)

I've looked at examples on Google and yet they seem to look the same as this what am i doing wrong?

Offline Zane

  • Global Moderator
  • Fanatic
  • *
  • Posts: 3,896
  • Gender: Male
    • View Profile
Re: If statement with Numbers
« Reply #1 on: March 18, 2010, 11:49:59 PM »
the equal sign and the greater than sign have to form as one in order to be a .... united operator.

So... instead of
= >
use
=>

Also.. you have to specify each comparison.  like $blahblah >= 25.5 AND $dfjsdklf <= 10.25
« Last Edit: March 18, 2010, 11:51:07 PM by Zanus »

Want to thank me?  Contribute to my PayPal piggy-bank

Offline teamatomic

  • Devotee
  • Posts: 1,197
  • Gender: Male
  • fat skis rule!
    • View Profile
    • myPHPtemplate
Re: If statement with Numbers
« Reply #2 on: March 19, 2010, 12:02:48 AM »
use the && for a double comparison
elseif ($bmi <= 18.5 && $bmi <  24.9)
and
=< will throw a unexpected '<' error
=>will throw an T_DOUBLE_ARROW error
use >= or <= instead
its "greater than or equal to" or "less than or equal to"


HTH
Teamatomic
« Last Edit: March 19, 2010, 12:04:40 AM by teamatomic »
when in doubt...ski fast
when scared...ski faster
when terrified...point em down

Offline tarletonTopic starter

  • Irregular
  • Posts: 42
    • View Profile
Re: If statement with Numbers
« Reply #3 on: March 19, 2010, 12:21:42 AM »
<!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>bmi.php</title>
</
head>
<?
php
$name 
$_POST['name'];
$weight $_POST['weight'];
$height $_POST['height'] ;
?>
<body>
<?php 
echo "<table>
<tr>
<td>Name:</td>
<td>
$name</td>
</tr>
<tr>
<td>Weight:</td>
<td>
$weight</td>
</tr>
<tr>
<td>Height:</td>
<td>
$height</td>
</tr>"
;
echo 
"</table>";
?>

<?php
$bmi 
= ($weight)*($weight) ;
$bmi = ($bmi)/($weight);
echo 
"$name, your BMI is $bmi <br>" ;


//Open the file which order will be written to.
$file fopen("$name.txt","wr");
//Write the string to the file
echo fwrite($file,"Name: $name , Weight: $height , Height: $height , BMI: $bmi");
fclose($file);

if (
$bmi <= 18.5)
	
	
echo 
"You are Underweight" ;
elseif (
$bmi >= 18.5 && $bmi <= 24.9)
	
	
echo 
"You are Normal Weight" ;
elseif (
$bmi >= 25 && $bmi <=  29.9)
	
	
echo 
"You are Overweight" ;
elseif (
$bmi >= 30)
	
	
echo 
"You are obese" ;
	

?>

</body>
</html>

Above is my code fixed and thankyou for your help. A question question however why when I print this does it show the number of characters written to the text file?

Offline oni-kun

  • Addict
  • Posts: 1,964
  • Gender: Male
  • When you're down, Logic can be a cure.
    • View Profile
    • oni-kun.com
Re: If statement with Numbers
« Reply #4 on: March 19, 2010, 12:39:04 AM »
$file fopen("$name.txt","wr");

wr is not a valid option, Look into r/r+ and w/w+
PHP Documentation (Examples and Usage) || Posting code? [php] Code Here [/php]. (good ref: Truth Tables )
//If you wish a program to have inf args. $params is an array of them.
function($args) {
  if (
is_scalar($args)) {$params func_get_args();}
}

Offline teamatomic

  • Devotee
  • Posts: 1,197
  • Gender: Male
  • fat skis rule!
    • View Profile
    • myPHPtemplate
Re: If statement with Numbers
« Reply #5 on: March 19, 2010, 12:45:12 AM »
 Because you are echo'ing out fwrite. fwrite, as you know writes to a file, however, it returns the bytes written. so when you echo out fwrite you get the bytes written.
so
if !fwrite is the same as if fwrite<1


HTH
Teamatomic
when in doubt...ski fast
when scared...ski faster
when terrified...point em down