Author Topic: Randomish Question on if's  (Read 574 times)

0 Members and 1 Guest are viewing this topic.

Offline RyokotsusaiTopic starter

  • Irregular
  • Posts: 39
  • Gender: Male
  • Psychotic Kiwi Lover
    • View Profile
    • Gravity RO
Randomish Question on if's
« on: September 08, 2007, 11:22:21 PM »
I see things used in a lot of scripts:
Code: [Select]
if(!isset(this)) {do this} else {or do this}
and my question is, Is using not set, or not equal to better, or does it have some advantage over using just isset() or ==?

'cause from what i have seen everyone seems to go towards "is not..."
I didn't here you, I have selective hearing, and you weren't selected...

Offline TheFilmGod

  • Addict
  • Posts: 1,933
  • Gender: Male
    • View Profile
Re: Randomish Question on if's
« Reply #1 on: September 08, 2007, 11:24:51 PM »
I dont understand why it matters. It depends on the situation. Sometimes a variable will be given different values depending on parameters and sometimes is only set if a form was submitted. So there is no advantage in using anyone of those. THERE is an advantage in using it when you need to.

common sense.

I'm a noob.
The future is here.

Offline GuiltyGear

  • Addict
  • Posts: 1,692
  • Gender: Male
    • View Profile
    • Laboratori.al - Creative Web Geeks
Re: Randomish Question on if's
« Reply #2 on: September 08, 2007, 11:36:21 PM »
U can use isset() to be sure a variable is not null or not unset()-ed before, while u use the equality operator == to compare that variable to a defined value. They are different methods and provide different handling of situations.

Offline RyokotsusaiTopic starter

  • Irregular
  • Posts: 39
  • Gender: Male
  • Psychotic Kiwi Lover
    • View Profile
    • Gravity RO
Re: Randomish Question on if's
« Reply #3 on: September 08, 2007, 11:53:39 PM »
isset wasn't the question  :D i was talking about th preference ppl seem to have of using "!" in if statements

@filmgod

i agree that in a lot of situations it does make sense, but then again in just as many there is an "else" statment, that was always a part of it, which negates the need for the "!" as it does something anyway and would work the same the other way

ie:
Code: [Select]
if($this != $that) {echo "They are not the same."; } else {echo "They are the same.";}
does the same thing as:

Code: [Select]
if($this == $that) {echo "They are the same.";} else {echo "They are not the same.";} 
right?

yet out of all the examples i have read, post i have seen, and scripts I have torn apart, 90% of the time the person who wrote it used != or !isset() with an else.  So if it has no real advantage in a situation where there is an 'else' are we just drawn toward a negative?
I didn't here you, I have selective hearing, and you weren't selected...

Offline TheFilmGod

  • Addict
  • Posts: 1,933
  • Gender: Male
    • View Profile
Re: Randomish Question on if's
« Reply #4 on: September 09, 2007, 12:04:12 AM »
isset wasn't the question  :D i was talking about th preference ppl seem to have of using "!" in if statements

@filmgod

i agree that in a lot of situations it does make sense, but then again in just as many there is an "else" statment, that was always a part of it, which negates the need for the "!" as it does something anyway and would work the same the other way

ie:
Code: [Select]
if($this != $that) {echo "They are not the same."; } else {echo "They are the same.";}
does the same thing as:

Code: [Select]
if($this == $that) {echo "They are the same.";} else {echo "They are not the same.";} 
right?

yet out of all the examples i have read, post i have seen, and scripts I have torn apart, 90% of the time the person who wrote it used != or !isset() with an else.  So if it has no real advantage in a situation where there is an 'else' are we just drawn toward a negative?

YOu need to code php in such a way that the less common action will be the if. You should code, if (form was submitted) { then do this }else { do this}

Not the other way around for various reasons. Let's say the code somehow got messed up or something, then the default (else) would be exectuted and thus should be the default or more common action.

Awesome thefilmgod example:

if ($authorized != true) {

do not give authorizaton

}
else {

give authorization

}
Althought it should always work the else statement to give authorization as the last end option is pretty stupid logically.
The future is here.

Offline GuiltyGear

  • Addict
  • Posts: 1,692
  • Gender: Male
    • View Profile
    • Laboratori.al - Creative Web Geeks
Re: Randomish Question on if's
« Reply #5 on: September 09, 2007, 12:20:45 AM »
The NOT (!) operator will give u more control if the situation requests it and most of the time make your code a bit cleaner. For example in a boolean variable: instead of "if($var == false)" u use "if(!$var)". It all depends of the situation and how u're used to coding. I have a practice of using it a lot of time and i like that way.

Offline d22552000

  • Devotee
  • Posts: 534
  • Gender: Male
  • ...?
    • View Profile
Re: Randomish Question on if's
« Reply #6 on: September 09, 2007, 02:58:29 AM »
hmm I think this is what hes asking and I would like to know the anser...

in the following, which is the best? fastest? mose secure?

Code: [Select]
if (A!=1)
if (!A=1)
if (A is not 1)
if (A does not equal 1)

Cause on my servery they all work, and my tries to time them were completely random.

Offline d22552000

  • Devotee
  • Posts: 534
  • Gender: Male
  • ...?
    • View Profile
Re: Randomish Question on if's
« Reply #7 on: September 09, 2007, 06:10:23 PM »
so whats hte answer?

Offline jesirose

  • Fanatic
  • Posts: 3,819
  • Gender: Female
  • PHP Goddess
    • View Profile
    • Reed's Training Blog
Re: Randomish Question on if's
« Reply #8 on: September 09, 2007, 06:18:42 PM »
Uhm, only one of them is valid code.
Every time you post "It didn't work" without more explanation, God kills a kitten.
When you post code without code tags, He just teases a puppy. But it's still sad.
What does that php function do? | What does that term mean? | I don't see any errors! | Awesome API Interface
PHP Goddess with Attitude since 2004

Offline prodigy2k7

  • Irregular
  • Posts: 9
    • View Profile
Re: Randomish Question on if's
« Reply #9 on: September 09, 2007, 08:29:55 PM »
well how I like to code is have whichever way will have the MAIN stuff at the top and the error trapping below.

IE: (Only wanting prodigy2k7 to visit)

if ($name == prodigy2k7) {
  do this;
  also this;
  and this;
  and this;
  more of this;
  and this;
}
else {
  echo "You are not allowed."
}

OR (Wanting anyone EXCEPT prodigy2k7 to visit)

if ($name != prodigy2k7) {
  do this;
  also this;
  and this;
  and this;
  more of this;
  and this;
}
else {
  echo "You are not allowed."
}

See what I mean? It really depends on what your doing.
I like to have most of the code at the top, so therefore I do whatever is necesarry at the top using == or != so it really depends on the person and what your doing.


No there is no difference. It is a language that you are coding for yourself. Look at Shakespeare, he writes how he wants to. You can code how you want to, how you best understand it.

Enjoy :)

Offline d22552000

  • Devotee
  • Posts: 534
  • Gender: Male
  • ...?
    • View Profile
Re: Randomish Question on if's
« Reply #10 on: September 11, 2007, 06:27:22 PM »
lol

I even made a function for echo cause im bored of typing the word echo...

function a($a) { echo $a; }

LOL!!!


A("im bored of echo");

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: Randomish Question on if's
« Reply #11 on: September 11, 2007, 06:41:58 PM »
If you have notices turned on with php:

if($var == "hi")

If you do that, and var hasn't been initialialized, then PHP will throw a warning (which most people hide).

As for if(!condition).... I think that's just personal preference....
Why doesn't anyone ever say hi, hey, or whad up world?

Offline d22552000

  • Devotee
  • Posts: 534
  • Gender: Male
  • ...?
    • View Profile
Re: Randomish Question on if's
« Reply #12 on: September 14, 2007, 07:05:45 PM »
no ti wont, depends on if you ahve ~warnings.  and it also depends on if you have automatic variables on.