-1

So I'm new to PHP and compared to other languages I've used, some things in PHP just don't make any sense to me.

My code currently

$secret = $_POST['secret']; //HTML input to enter secret code to unlock easter egg
$theSecret = "1234"; //Pre-defined test code

enter code here
if($secret !== $theSecret){
echo "You got the secret wrong!";
exit;}

Now in C++ or C# this would work and it makes sense, but however in PHP when I use !== it will always equal true, and when I use === it's as if nothing is even checked because it always passes.

What I'm trying to achieve is if the code is correct then I can proceed, if it is not correct then terminate the connection. Is there something I am unaware of that I am doing wrong? PHP has not been nice to me, I appreciate everyone's help.

  • Your statements are false. You must give what is `$_POST['secret']`. If both are `"1234"` then === is true and !== is false. – AbraCadaver Sep 02 '15 at 03:03
  • if one is an integer and the other is a string, of course they're not identical and that's what `===` checks for. Doing `$theSecret = (int)"1234";` would cast the string to an integer, **IF** the POST array is an integer. Casting it (integer) as a string `$theSecret = (string)1234;` when the POST array is a string, will be TRUE against `$theSecret = "1234";`. [**RTFM**](http://il.php.net/manual/en/language.operators.comparison.php) – Funk Forty Niner Sep 02 '15 at 03:19

2 Answers2

2

triple ='s means it also matches data type.

example

$x=0;
if($x==false) returns true;
if($x===false) returns false; //different datatype

if($x=='0') returns true;
if($x==='0') returns false; //different datatype

$y=0;
$z=1;
if($x===$y) returns true; //same datatype and value
if($x===$z) returns false; //different value
kayleighsdaddy
  • 670
  • 5
  • 15
-1

Use

if($secret != $theSecret)

!== Is most commonly used when you're comparing the variables if their types are identical.

WaduNoop
  • 65
  • 1
  • 2
  • 14
  • Do not down vote without explaining, this site is mostly for learning not for posting code gd. @Newbie.Ridick your statement is false –  Sep 02 '15 at 03:09
  • 1
    I need to have 125 reputation in order to down vote sir. @GRC – WaduNoop Sep 02 '15 at 03:11
  • I told to somebody who down voted your question the second part of my comment is related to your answer. –  Sep 02 '15 at 03:13
  • basically somebody is going around and down voting without giving you a chance to explain or learn, and as I said this site is for learning. –  Sep 02 '15 at 03:14
  • Well, i do apologize for not explaining my answer. Just to clear things that i'm unable to down vote because i need 125 reputation in order to do so. :| – WaduNoop Sep 02 '15 at 03:22
  • no somebody who downvoted your question should of explained to you why he/she thinks your answer is not correct then if you do not do something, he/she should down vote. So basically put your answer down and people should tell you that it is wrong answer, and point you in right direction if you do not take their advice then they should down vote, they should give you a chance of explaining your answer or learning why your answer is wrong. –  Sep 02 '15 at 03:29
  • I agree. If there's something wrong with the answer, they should gave advises to correct it. – WaduNoop Sep 02 '15 at 03:33