Jump to content

is_int() problems


s1lence

Recommended Posts

Hello,

I am a newbie to php, and am having some difficulties with understanding type identifier functions.  I have been using a variety of resources to teach myself php such as: tizag's php tutorial for basic concepts and "Beginning PHP and MySql."  I like mixing and testing things to see if I understand concepts, thus I have run into a problem.

The is_int() function is not identifying my variable as a integer from a form post.

 

The form code is just simple html:

<html>
<head>
<title>This is a Form</title>
</head>
<body>
<h2>S1lence's Items</h2>
<form action="action.php" method="post">
<p>Select an Item:</p>
<select name="item">
	<option>Wand</option>
	<option>Sword</option>
	<option>Axe</option>
	<option>Bow</option>
</select>
<p>Quantity:</p>
<input name="quantity" type="text" />
</body>
</html>

 

The problem occurs when the "action.php" acts upon the posted data and stores the quantity and item values.

 

Here is the "action.php" code:

<?php
$item = $_POST['item'];
$quantity = $_POST['quantity'];
if (is_int($quantity)) { //I. if $quantity as an integer, then the following code will be executed otherwise go to (II)
echo "You ordered ". $quantity . " " .$item . ".<br />"; //This tells the the people what and how many of each item they ordered.
if ($item != "Sword"){  //A.The if statement tells anyone who did not order a sword that that made the wrong choice.
echo "You made the wrong choice!";}
else {
echo "You made the right choice!";} //B. This else statement tells anyone who ordered nothing other than a sword that the made the right choice.
}
else {echo "$quantity is an Invalid Input.  Please make sure that you entered an integer value.";}
//II. If the argument returns false, then this will be displayed.

?>

 

The values seem to be stored because I get the following message:

 

10 is an Invalid Input. Please make sure that you entered an integer value.

 

where 10 could be any integer (it just happened to be the one I chose).

 

The value returns as an integer, and therefore, it must have been properly stored, but for some reason it's not being evaluated as an integer.  Is there some other function I should use to have it checked as an integer?  And also, what is making the is_int() return FALSE.  I apologize, if this is a simple question.  I'm just trying to get a better grasp on things.

 

Link to comment
https://forums.phpfreaks.com/topic/169698-is_int-problems/
Share on other sites

and since you wan't an int... and after that, you'll either want to floor() it (to get it to round down to an int if it's a decimal) or multiply it by 1 or something so it's no longer a numeric string, but rather a number, and then test if it's an int.... like:

 

is_int(1*$quantity)

 

after you check to make sure it is numberic

Link to comment
https://forums.phpfreaks.com/topic/169698-is_int-problems/#findComment-895245
Share on other sites

Thanks a lot for the help.  It's working smoothly now.  I was hoping that a little insight into the programming language from one with more experience would help me understand a bit better.  Thanks for taking the time out of your day to post here it is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/169698-is_int-problems/#findComment-895248
Share on other sites

Archived

This topic is now archived and is 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.