Dynadot โ€” .com Registration $8.99

Wts wrong here

Spaceship Spaceship
Watch
Impact
19
HEY
i am trying to learn php and i tried making a small script that tells the user to type in a number...and checks if its an integer..if its not an integer it gives an error..but its not working...no matter wt they enter..it says that its not an integer
here is the code

PHP:
Please enter some numbers..ONLY NUMBERS<br>
		<form action="<?$_SERVER[PHP_SELF]?>" method='post'><br>
			<input type='text' name='input'> <br>
			<input type='Submit' name='Submit' value='Submit'>
		</form>

<?
$stuff=$_POST['input'];
$submit= $_POST['Submit'];
$get = gettype($stuff);
$get=gettype($stuff);
if (isset($submit)){
if ($get != 'integer') {
echo "You posted $stuff and its not an integer";}
else
{echo "thanks " ;}
}
else 
{}


?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
use the is_int() function.
 
0
•••
hey i am sorry i dont know what that is
i am new to php and the first tutorial i read was on gettype() and settype() so i tried messin with it but it dint work...duno wts wrong wid it?
 
0
•••
0
•••
grr
is anyone going to tell me whats wrong wid the script?
i know php.net is a great site!!!
i bought a BOOK and i am reading it
the first chapter talked about this thing and i tried to play with it to learn and its not working as it should and i am asking someone to tell me HOW 2 FIX IT! not to tell me to go to that site to learn this or that !!
 
0
•••
Please enter some numbers..ONLY NUMBERS<br>
<form action="<?$_SERVER[PHP_SELF]?>" method='post'><br>
<input type='text' name='input'> <br>
<input type='Submit' name='Submit' value='Submit'>
</form>

<?
$stuff=$_POST['input'];
$submit= $_POST['Submit'];
$input=$_POST[input];
if (isset($submit)){
if (is_int($input)) {
echo "You posted $stuff and its not an integer";}
else
{echo "thanks " ;}
}
else
{}


?>
 
0
•••
grr but y isnt the gettype not working? y isnt recognizing numbers as integers?
 
0
•••
Why does it matter? You can use the is_whatever family of functions to do it more efficiently.
 
0
•••
because i am trying to learn php so i need to know y its not working..i am not trying to create a script :p just learning
and the other one dint work either
learn.apkafuture.com/ops.php :(
 
0
•••
I think it's not working because the post variable is handled as a string.
 
0
•••
Jim_ said:
I think it's not working because the post variable is handled as a string.
:bingo: and using is_int will produce the same result. Btw, to quote the PHP manual:
Never use gettype() to test for a certain type, since the returned string may be subject to change in a future version. In addition, it is slow too, as it involves string comparison.

Instead, use the is_* functions.

However, you could use (int)$_POST['input'] , or intval($_POST['input']). That would solve your problem...almost. If anything other than numbers are posted..intval or (int) will return 0, which will still pass as an integer.

A good way to do it would probably be to do a preg_match
PHP:
<?php

$stuff = '9';

if(!preg_match('/([0-9]+)/', $stuff))
{
  echo "You posted $stuff and its not an integer";
}
else
{
  echo "thanks ";
}

?>
 
Last edited:
0
•••
okay got it
Thanks :D
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back