Jump to content

PHP if !==

Joveice
Go to solution Solved by leonfagan71,
14 minutes ago, Joveice said:

So I have this


if ($row['player1a'] !== '1') {
	header("Location: /error/");
	exit();
}

And the $row['player1a'] is 1 and this still triggers, I needed it to only trigger if $row['player1a'] wasent 1

what have I done wrong?

!== is checking for an exact match including value types.

!= is not equals.

== is equals to.

 

remove one of the = signs.

So I have this

if ($row['player1a'] !== '1') {
	header("Location: /error/");
	exit();
}

And the $row['player1a'] is 1 and this still triggers, I needed it to only trigger if $row['player1a'] wasent 1

what have I done wrong?

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Is player1a supposed to return 1 or '1' ?

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Enderman said:

Is player1a supposed to return 1 or '1' ?

1

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Joveice said:

So I have this


if ($row['player1a'] !== '1') {
	header("Location: /error/");
	exit();
}

And the $row['player1a'] is 1 and this still triggers, I needed it to only trigger if $row['player1a'] wasent 1

what have I done wrong?

!== is checking for an exact match including value types.

!= is not equals.

== is equals to.

 

remove one of the = signs.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, leonfagan71 said:

!== is checking for a true or false value.

!= is not equals.

== is equals to.

 

remove one of the = signs.

okey, and if I then change '1' to '2' and the value is 1 it will also trigger? (for later use)

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Joveice said:

okey, and if I then change '1' to '2' and the value is 1 it will also trigger? (for later use)

if ($row['player1a'] != '1') {
	//i will die when it isn't 1
	die();
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Joveice said:

okey, and if I then change '1' to '2' and the value is 1 it will also trigger? (for later use)

yes. If the value is not 2 then it would trigger. Maybe you should explain a little more about what you're trying to do so we can help you better.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Oh god I can't type. You said that the value you were checking against was 1, not '1'.

!= this operator would work. It checks to see if the values match.

!== also checks if the values match, but in greater depth. The value and data type have to match.

 

So if you changed it to this 

if ($row['player1a'] !== 1)

Then it will work. That's because you're feeding it a number, not a string.

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, vorticalbox said:

yes. If the value is not 2 then it would trigger. Maybe you should explain a little more about what you're trying to do so we can help you better.

I just needed it to only trigger if the value wasent 1 else just ignore it, but I got it working thanks to all of you :)

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Joveice said:

I just needed it to only trigger if the value wasent 1 else just ignore it, but I got it working thanks to all of you :)

if ($row['player1a'] != 1)

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

=== and !== mean absolute equality or inequality  , meaning the data type is also checked. 1 would be different than '1'.

 

Why is it used...  some functions return FALSE on error, or a number from 0 to something. For example strpos() returns FALSE if the substring is not found in the bigger string, or a value between 0 and the maximum length of the string if it's found.

But, FALSE is loosely converted by php to 0 so you wouldn't know if the return of a function is actually some kind of error indication (FALSE) or actually the value 0. That's why you'd use !== to compare exactly to FALSE, and not 0 or NULL or ''  (all three are translated to FALSE when compared against a boolean value)

 

In your case, since you probably don't care if the variable in the array stores the information as a number ( 1 ) or as a string holding the value as a series of characters ( '1' ) , you can probably use the plain != and let PHP (if needed) do an additional step, converting the string '1' to the number 1, and comparing it with the value stored in the variable.

If you want to optimize your scripts, to make php work as little as possible, you would be careful to always use numbers and not strings, and any input received from the server you'd be careful to validate before using (test if it's a string that holds a number .. using is_numeric or ctype_digit for integer numbers or something like that) and then convert the actual string to an integer number before storing it in variables in php.

 

See  http://php.net/manual/en/types.comparisons.php for more details.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×