8 Replies - 272 Views - Last Post: 17 August 2012 - 07:32 AM Rate Topic: -----

#1 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 105
  • Joined: 31-July 12

what's the difference here? - isset()

Posted 16 August 2012 - 01:53 PM

Hi guys,

I saw this recently on some PHP scripts and I can't figure out why the guy who wrote them used this syntax:
if(isset($_POST['something']) === true)

I mean, I know what "===" means (same value & same type), but why did he used it? It is somehow more eficient / secure than just
if(isset($_POST['something']))
or is just a programmer prefference?

Any response will be highly appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: what's the difference here? - isset()

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2485
  • View blog
  • Posts: 8,524
  • Joined: 08-August 08

Re: what's the difference here? - isset()

Posted 16 August 2012 - 02:00 PM

Personal preference.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2890
  • View blog
  • Posts: 7,535
  • Joined: 08-June 10

Re: what's the difference here? - isset()

Posted 16 August 2012 - 02:19 PM

or continuity. some functions (e.g. strpos()) wouldn’t work correctly if used without the explicit comparison.
Was This Post Helpful? 1
  • +
  • -

#4 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 433
  • View blog
  • Posts: 789
  • Joined: 17-June 08

Re: what's the difference here? - isset()

Posted 16 August 2012 - 04:21 PM

What does the rest of that guy's code look like? Because it's possible that he's just incompetent. Or maybe he was just indoctrinated with a really stupid coding style. I used to work with a guy like that - very intelligent, but his college professors had instilled some truly bone-headed ideas in him about the "right" way to do things. Usually, that meant writing ten lines of code where two would suffice.

But seriously, there's absolutely no reason for that. Sometimes the strict equality comparison is necessary, but not here - isset() always returns a boolean. The guy just chose to write nine characters more than he needed to for absolutely no good reason.
Was This Post Helpful? 1
  • +
  • -

#5 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 105
  • Joined: 31-July 12

Re: what's the difference here? - isset()

Posted 17 August 2012 - 02:21 AM

Thanks for replies guys, I'll stick with if(isset($_POST['something'])). It wasn't really an emergency, but I always wanted to program as efficient as possible. ;)
Was This Post Helpful? 0
  • +
  • -

#6 vaayaaedu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-August 12

Re: what's the difference here? - isset()

Posted 17 August 2012 - 05:24 AM

== is used to compare values and === is used to compare objects.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2485
  • View blog
  • Posts: 8,524
  • Joined: 08-August 08

Re: what's the difference here? - isset()

Posted 17 August 2012 - 05:51 AM

View Postvaayaaedu, on 17 August 2012 - 08:24 AM, said:

== is used to compare values and === is used to compare objects.

Not only objects.
<?php
$x = 2; $y = 2; $z = "2";

if($x == $y) {
	echo "x equals y.<br>";
} else {
	echo "x and z are not equal.<br>";
}

if($x == $z) {
	echo "x equals z.<br>";
} else {
	echo "x and z are not equal.<br>";
}

if($x === $y) {
	echo "x equals y and they are of the same type.<br>";
} else {
	echo "x and y are not equal AND of the same type.<br>";
}

if($x === $z) {
	echo "x equals z and they are of the same type.<br>";
} else {
	echo "x and z are not equal AND of the same type.<br>";
}
?>

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2485
  • View blog
  • Posts: 8,524
  • Joined: 08-August 08

Re: what's the difference here? - isset()

Posted 17 August 2012 - 06:34 AM

I should have said not objects at all. The code below produces incorrect results:
<?php
class testing {
	public $x;
	public $y;
	
	function _construct() {
		$this->x = 10;
		$this->y = 20;
	}
}

$obj_one = new testing();
$obj_two = new testing(); // Identical to first.
$obj_three = new testing(); // Identical to first
$obj_three->y = 30; // Make third object different from the first two

if($obj_one === $obj_two) {
	echo "obj1 and obj2 are equal.<br>";
} else {
	echo "obj1 and obj2 are NOT equal.<br>";
}
if($obj_one === $obj_three) {
	echo "obj1 and obj3 are equal.<br>";
} else {
	echo "obj1 and obj3 are NOT equal.<br>";
}

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2890
  • View blog
  • Posts: 7,535
  • Joined: 08-June 10

Re: what's the difference here? - isset()

Posted 17 August 2012 - 07:32 AM

incorrect in what way? looks perfectly valid to me.

http://www.php.net/manual/en/language.oop5.object-comparison.php said:

On the other hand, when using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1