Jump to content

Php Conditional Operator


jdm95lude

Recommended Posts

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

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

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

  • 4 years later...

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

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.