Jump to content

Arrays store data sa strings, but what if I don't want that?


taps128

Recommended Posts

I fill an array with data collected from a webpage form it looks like this:

while ($a<$redovi) {d
$pk="proizvedeno_komada".$a;
$sp="sifra_proizvoda".$a;
$proizvedeno_komada[]=$_POST[$pk];
$sifra_proizvoda[]=$_POST[$sp];
$a++;
}

And since only integers can be stored inside $proizvedeno_komada, I want to produce a "You can't do that" message if the user entered anything else besides an integer.

When I entered only integers the gettype() function echoed string for all of them my checking function goes like this :
function kontrola_niza($niz)
{
$a=array_merge($niz);
foreach ($a as $value){

if (is_integer($value)==FALSE) {$r=0;return $r; break;}


}
$r=1;
return $r;
}

I appriciate all the help you can give me.

Nikola
Link to comment
Share on other sites

The "numbers" that come in through your $_POST values are always POSTed as strings, even if they're numbers.

You need to use PHP's is_numeric() function instead of is_integer(). You can also cast your POSTed values as integers before doing anything with them:
[code]$val = intval($val);[/code]
Link to comment
Share on other sites

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.