Page 1 of 1

Why won't the script accurately define a var?

Posted: Tue Jan 17, 2012 1:58 pm
by strategos
This returns true no matter what the input is.

Code: Select all

<?php

$inputid = $_POST['steamid'];

$steam_prefix = substr($inputid, 0, 10);

$prefix_length = 10;

$char_total = strlen($inputid);

$steam_suffixchars = ($char_total - $prefix_length);

$suffix_check = substr($inputid, 10, $steam_suffixchars);

if(is_int($suffix_check) == true)
$suffix_valid = true;
elseif(is_int($suffix_check) == false)
$suffix_valid = false;

if($suffix_valid = true)
$y="true";
elseif($suffix_valid = false)
$y="false";

if($char_total > 25 || $char_total < 11)
$char_valid = false;
else
$char_valid = true;


if($steam_prefix === "STEAM_0:1:" || $steam_prefix === "STEAM_0:0:")
$prefix_check = true;
elseif($steam_prefix != "STEAM_0:1:" || $steam_prefix != "STEAM_0:0:")
$prefix_check = false;

echo "$y"


?>
Please Help

Thanks

Re: Why won't the script accurately define a var?

Posted: Tue Jan 17, 2012 2:25 pm
by Tiancris
Look at this if: :wink:

Code: Select all

if($suffix_valid = true)
$y="true";

Re: Why won't the script accurately define a var?

Posted: Tue Jan 17, 2012 2:35 pm
by twinedev
I hate when I do that, then I find it and am like DOH! Usually a sign I've been up too long LOL

Re: Why won't the script accurately define a var?

Posted: Tue Jan 17, 2012 3:14 pm
by strategos
What did I do wrong?

I changed it to

Code: Select all

if($suffix_valid == true)
$y = "true";
elseif($suffix_valid == false)
$y = "false";
same error.

I believe it has to do with

Code: Select all

if(is_int($suffix_check) == true)
$suffix_valid = true;
elseif(is_int($suffix_check) == false)
$suffix_valid = false;
when I change $suffix_check to an integer or number, it returns true or false like its supposed to.

Re: Why won't the script accurately define a var?

Posted: Tue Jan 17, 2012 3:45 pm
by twinedev
What are some examples of $_POST['steamid'] are you using? (list some good, some bad).

Do you have this code broken out for debugging, or do you have other code needing some of it? (there is a lot of unnecessary code for what it appears you are doing).

Once you give a list of some valid values, I can probably simplify this a whole ton with regular expression.

-Greg

Re: Why won't the script accurately define a var?

Posted: Tue Jan 17, 2012 3:54 pm
by twinedev
Just a quick playing, here is something that will probably work.

Code: Select all

if (preg_match('/^STEAM_0:(1|0):(\d{1,15})$/',$_POST['steamid'],$regs)) {
	$bValid = TRUE;
	$intSecondDigit = $regs[1]; // (the 0 or 1 after STEAM_0: )
	$intSuffix = $regs[2];
}
else {
	$bValid = FALSE;
	$intSecondDigit = NULL;
	$intSuffix = NULL;
}

Re: Why won't the script accurately define a var?

Posted: Tue Jan 17, 2012 9:34 pm
by strategos
Yea i'm new to php and this is my second script ever. Basically, the script returns EVERYTHING as false.

Twindev, thank you for posting a possible solution, but I can't decipher what most of it does.

Re: Why won't the script accurately define a var?

Posted: Wed Jan 18, 2012 1:20 am
by Christopher
$suffix_check is the return value of substr() so it is always a string, hence $suffix_valid is always false. Therefore $y is always "false". You may want to try using is_numeric() instead of is_int()

Keep trying, it's the only way to learn these details. I would also recommend using var_dump() to show the both the variable type and value. Then you can see what the values you are comparing are.