Jump to content

Recommended Posts

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. Save the document as

IsEven.php.

Reinforcement Exercises1-6

 

This has been a hard to find solution and I am submitting mine.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"[url="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd%22>"]http://www.w3.org/TR...l1-strict.dtd">[/url]
<html xmlns="[url="http://www.w3.org/1999/xhtml%22>"]http://www.w3.org/1999/xhtml">[/url]
<head>
<title>PHPIsEven1-6</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

</head>
<body>

<form action="isEven.php" method="post">
Input Number:<input type="text" size="36" maxlength="36" name="Fnumber">:<br />
<input type="submit" value="submit" name="submit"><br />
</form><br />

<?php
// Carl Skeel
# 2012/09/24
$Fnumber = $_POST['Fnumber'];

$Number = $Fnumber;

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;

?>

</body>
</html>

I hope this helps anyone on this one!

IsEven.php

Edited by Zane
Please use code tags
Link to comment
https://forums.phpfreaks.com/topic/268718-conditional-operator/
Share on other sites

You have to use PHP´s isset function to check whether or not the variable exists or not... That is why you get an undefined index...you haven´t hit the submit button therefore POST is empty.

 

$Number = isset($_POST['Fnumber']) ? $_POST['Fnumber'] : null;

Sorry Jessica and Zane I appreciated the help and the only thing the text was asking for was the general if statement but I had went way beyond that with creating the form and all and had only asked for help with the one little err I had even tho it worked and what seems so minor to you all was a big help to me then after Zane's help I found I needed to add another If arround the whole thing otherwise it kicked out the echo this is not numeric just by running it but after looking it over I figured out the rest after Zane's help with the isset so again thank you Zane for your time and help.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.