IF statement not working

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
andbengts
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Mar 16, 2018 2:20 pm

Fri Mar 16, 2018 2:32 pm

Hi,

I read a number from a file and would like to compare it with a number that user post to the page. But it seems like something is wrong with the IF statement. What even number the user post to the page, the IF statement always say "CORRECT hq code". For example

CORRECT hq code
You have configured hq code: 7
Correct hq code is: 342522901

Anyone that can see what is wrong?

Thanks!


Code: Select all

$correcthqcode = file_get_contents('../../files/hq.txt', NULL, NULL, 3, 10);

if( empty($_REQUEST['hqcode']))	{
		$hqcode = "NULL";	
		echo 'HQ CODE MISSING - PLEASE UPDATE SETTINGS AND TRY AGAIN';
		exit(1);
	}
        else
	{
	
		$hqcode = $_REQUEST['hqcode'];
		$hqcode = str_replace('#', "", $hqcode);
		$hqcode = str_replace('<', "", $hqcode);
		$hqcode = str_replace('>', "", $hqcode);
		$hqcode = str_replace('(', "", $hqcode);
		$hqcode = str_replace(')', "", $hqcode);
		$hqcode = str_replace('*', "", $hqcode);
		$hqcode = str_replace('/', "", $hqcode);
		$hqcode = str_replace('&', "", $hqcode);

	}


		if ($HQcode == $CorrectHQcode) {
			echo "CORRECT hq code";
			echo '<BR>';
			echo "You have configured hq code: $hqcode <BR>";
			echo "Correct hq code is: $correcthqcode";
		}

		else
		{
			echo "INCORRECT HQ CODE - PLEASE UPDATE SETTINGS AND TRY AGAIN <BR>";
			echo "You have configured hq code: $hqcode <BR>";
			echo "Correct hq code is: $correcthqcode";
			exit(1);
		}

User avatar
phpRob
New php-forum User
New php-forum User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Fri Mar 16, 2018 5:25 pm

Your if statement is not even using the correct variables. Variables in php are case-sensitive.
"if ($HQcode == $CorrectHQcode) {"
Since null==null, the if-statement runs the true part every time.
Had you used null===null, it would have failed every time.
So, at least for starters, correct those var names.

andbengts
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Mar 16, 2018 2:20 pm

Sun Mar 18, 2018 9:44 am

Thanks! I think I was just to tired that night. Thanks for pointing it out. It all works fine now.

User avatar
phpRob
New php-forum User
New php-forum User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Sun Mar 18, 2018 1:19 pm

No problem, glad to help, and I totally get it--we all need a second pair of eyes sometimes.

Post Reply