First off I know this is a duplicate question that is asked on here fairly often, however I have perused through those answers and still can’t figure out what is wrong with my code. I am fairly new to PHP, and I am 99% sure this is a very easy/novice thing to accomplish, which is why it’s frustrating me.
Here is the code:
$validProgram = 0;
$validProgramCodes = array('ITFZ', 'ITFC', 'ITFP', 'ITFE', 'ITFL', 'ITFS',
'ITFF', 'ITFM', 'ITFT', 'IEME', 'ISMK', 'IPKT');
if ( isset( $_GET[ 'category' ] ) && isset( $_GET[ 'subcategory' ] )
&& $_GET[ 'category' ] != '' && $_GET[ 'subcategory' ] != '' )
{
$selectedSchool = $_GET[ 'category' ];
$selectedProgram = $_GET[ 'subcategory' ];
$selectedSchool = prepString( $selectedSchool );
$selectedProgram = prepString( $selectedProgram );
foreach ( $validProgramCodes as $temp )
{
if ( $temp == $selectedProgram )
{
$validProgram ++;
}
}
if ( $validProgram == 1 )
{
echo "success!";
}
else if ( $validProgram !== 1)
{
echo "failure!";
}
}
}
Now… for some reason, no matter what is supplied for $selectedProgram, the validation check echos out the failurestatement. This is really simple code so I’m sure it’s something glaringly obvious, but I think I just need another pair of eyes to give it a quick glance over. 🙂
Thank you for any help!
if ( $validProgram = 1 )assigns the value1to$validProgram. This assignment returns1, so the statement always passes.Try
if ( $validProgram == 1 )to test for equality.Regardless, looking at the whole block, it looks like you compress a lot of this down to a single
in_arraycheck, removing theforeachloop and the$validProgramvariable entirely: