jdm95lude Posted February 23, 2008 Share Posted February 23, 2008 I'm working on this assignment and my teacher said that It's incorrect of what I have right now. I just started learning PHP and I'm having a very hard time so any help would be great. the questions is: Write a script that uses a conditional operator to determine whether a variable contains a number and whether the number is even. You need to use the is_numeric() function and the conditional operator. For floating-point numbers, you need to use the round() function to convert the value to the nearest whole number. Hard code the number. <?php $Number = 8; round ($Number); ($Number == is_numeric) ? $Result = "It is not a number or it is not even." : $Result = "The number is even."; echo "<div class='text'>The Number is $Number</div>"; echo "<p class='result'>" ,$Result, "</p>"; ?> </body> Link to comment Share on other sites More sharing options...
teng84 Posted February 23, 2008 Share Posted February 23, 2008 bad idea to do your assignment here's clue.. modulo operator ( 4%2== 0) ------->true ctype data http://www.php.net/manual/en/function.ctype-alpha.php http://www.php.net/manual/en/function.ctype-digit.php hope that helps ... Link to comment Share on other sites More sharing options...
jdm95lude Posted February 23, 2008 Author Share Posted February 23, 2008 Sorry but no.... Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 Why not jusy check is numeric then check if even if( $number & 1 ){ $event_odd = "and it's odd!"; }else{ $event_odd = "and it's even!"; } if (is_numeric($number)){ $numeric = "This number is numeric"; }else{ $numeric = "This number is not numeric"; $not=1; } echo $numeric; if ($not==""){echo $event_odd;} Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 or even like this if (is_numeric($number)){ $Result = "This number is numeric"; if( $number & 1 ){ $Result. = " and it's odd!"; }else{ $Result. = "and it's even!"; } }else{ $Result = "This number is not numeric"; } echo $Result; Link to comment Share on other sites More sharing options...
jdm95lude Posted February 23, 2008 Author Share Posted February 23, 2008 would that be considered a conditional operator? Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 You have them all here http://ca.php.net/reserved.variables Link to comment Share on other sites More sharing options...
jdm95lude Posted February 23, 2008 Author Share Posted February 23, 2008 Ok thank you. Link to comment Share on other sites More sharing options...
drisate Posted February 23, 2008 Share Posted February 23, 2008 sorry that was for an other topic lol hehe helping on to much at the time i get that 25 seconde cooldown alot lol Well i am not 100% sur what you teacher calls a conditional operator but in my opinion "if" and "else" are conditions so yes Link to comment Share on other sites More sharing options...
Barand Posted February 23, 2008 Share Posted February 23, 2008 if you also want the ternary ? operator <?php $number = 111; if (is_numeric($number)){ $Result = "\"$number\" is numeric "; $Result .= ( $number & 1 ) ? 'and it\'s odd' : 'and it\'s even'; }else{ $Result = "\"$number\" is not numeric"; } echo $Result; ?> Link to comment Share on other sites More sharing options...
Carl Skeel Posted September 23, 2012 Share Posted September 23, 2012 Actually this version works without errors and includes the round function as well when it displays the number! <?php // Carl Skeel # 2012/09/24 $Number = "8.1"; if (is_numeric($Number)){ $Result = ""; if( $Number & 1 ){ $Result = "The number (" . round($Number) . ") and is numeric and it's odd!"; }else{ $Result = "The number (" . round($Number) . ") and is numeric and it's even!"; } }else{ $Result = "($Number) is not numeric!"; } echo $Result; ?> Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2012 Share Posted September 23, 2012 This thread is 41/2 years old. Locked. Link to comment Share on other sites More sharing options...
Recommended Posts