Why won't the script accurately define a var?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
strategos
Forum Newbie
Posts: 4
Joined: Thu Jul 28, 2011 4:15 pm

Why won't the script accurately define a var?

Post 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
User avatar
Tiancris
Forum Commoner
Posts: 39
Joined: Sun Jan 08, 2012 9:54 pm
Location: Mar del Plata, Argentina

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

Post by Tiancris »

Look at this if: :wink:

Code: Select all

if($suffix_valid = true)
$y="true";
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post 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
strategos
Forum Newbie
Posts: 4
Joined: Thu Jul 28, 2011 4:15 pm

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

Post 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.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post 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
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post 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;
}
strategos
Forum Newbie
Posts: 4
Joined: Thu Jul 28, 2011 4:15 pm

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
Post Reply