jmongan Posted October 9, 2010 Share Posted October 9, 2010 Help! Fatal error: Unsupported operand types in /net/fsstud.itu.dk/export/stud/www/e2010/DSDS/arhe/incomplete/ps5/bmi2.php on line 29 THIS IS THE PROBLEM LINE: $bmi = $vaegt / (($hoejde/100) * ($hoejde/100)); Here is my code...any ideas what's wrong? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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 // faste variabler $vaegt = $_REQUEST['vaegt']; $hoejde = $_REQUEST['hoejde']; if ( preg_match_all('/^[0-9]{2}$|^[0-9]{3}$/', $vaegt, $hoejde)) { // udskriv variabler echo "Vægt: $vaegt kg."; echo "<br />"; echo "Højde: $hoejde cm."; echo "<br />"; // udregn BMI $bmi = $vaegt / (($hoejde/100) * ($hoejde/100)); // udskriv BMI echo "Dit BMI er $bmi<br />"; // condition - hvad er BMI if ($bmi < 20.0 ) { echo "Dit BMI er for lavt."; } elseif ( $bmi < 25.0 ) { echo "Dit BMI er normalt."; } else { echo "Dit BMI er for højt."; }} //Hvis man ikke indtaster et tal, genereres et tilfældigt tal else { echo "Indtast vægt i kilo og/eller højde i centimeter (fx. 65 - 170)"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/215487-fatal-error-unsupported-operand-types/ Share on other sites More sharing options...
jcbones Posted October 9, 2010 Share Posted October 9, 2010 You are making "$hoejde" into an array in the preg_match_all() function. That is where the error comes from. You cannot add,subtract,divide,multiply with an array. Quote Link to comment https://forums.phpfreaks.com/topic/215487-fatal-error-unsupported-operand-types/#findComment-1120516 Share on other sites More sharing options...
jmongan Posted October 9, 2010 Author Share Posted October 9, 2010 I thought the array was an issue...any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/215487-fatal-error-unsupported-operand-types/#findComment-1120518 Share on other sites More sharing options...
Pikachu2000 Posted October 9, 2010 Share Posted October 9, 2010 The problem is your usage of preg_match_all(). The syntax is wrong, and causing $hoejde to be returned as an array. Quote Link to comment https://forums.phpfreaks.com/topic/215487-fatal-error-unsupported-operand-types/#findComment-1120519 Share on other sites More sharing options...
jmongan Posted October 9, 2010 Author Share Posted October 9, 2010 any suggested syntax change? Quote Link to comment https://forums.phpfreaks.com/topic/215487-fatal-error-unsupported-operand-types/#findComment-1120526 Share on other sites More sharing options...
jmongan Posted October 9, 2010 Author Share Posted October 9, 2010 $hoejde is not supposed to be an array. The point is that you are only aloud to type in numbers of two or three, preferably 3 in the height... that is why I have to do the preg_match ? so as to avoid people typing other things. Quote Link to comment https://forums.phpfreaks.com/topic/215487-fatal-error-unsupported-operand-types/#findComment-1120527 Share on other sites More sharing options...
Pikachu2000 Posted October 9, 2010 Share Posted October 9, 2010 Well, what are you trying to do? Make sure the entries are numeric? Quote Link to comment https://forums.phpfreaks.com/topic/215487-fatal-error-unsupported-operand-types/#findComment-1120528 Share on other sites More sharing options...
jcbones Posted October 9, 2010 Share Posted October 9, 2010 Change this line: if ( preg_match_all('/^[0-9]{2}$|^[0-9]{3}$/', $vaegt, $hoejde)) To if ( preg_match('/^[0-9]{2}$|^[0-9]{3}$/', $vaegt) && preg_match('/^[0-9]{2}$|^[0-9]{3}$/', $hoejde)) The syntax for preg_match(_all) is preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] ); It doesn't support multiple $subjects to compare against. Quote Link to comment https://forums.phpfreaks.com/topic/215487-fatal-error-unsupported-operand-types/#findComment-1120529 Share on other sites More sharing options...
jmongan Posted October 9, 2010 Author Share Posted October 9, 2010 cool tnx...works a treat. Quote Link to comment https://forums.phpfreaks.com/topic/215487-fatal-error-unsupported-operand-types/#findComment-1120530 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.