Jump to content

Recommended Posts

Hi :),

 

I'm new on this forum so don't judge too hard.. :P But I have a few questions. I am recently new to PHP as this is my first computer language I've learnt besides HTML.

 

My first question is, what's the difference between !== and !=, and is it more secure to use !== when comparing two fields such as passwords? I've different things and this has confused me..  :shrug:

 

And my second question is, I have this piece of code

 

/* option error checking */
      $field = "option";  // Use field name for option
         /* Check if the option picked is from the list of options to choose from*/
      if($suboption !== "option1" || $suboption !== "option2" || $suboption !== "option3" || $suboption !== "option4" || $suboption !== "option5" || $suboption !== "option6" || $suboption !== "option7"){
            $form->setError($field, "* Unexpected error with option");
      }

 

I'm guessing in the long run, this will slow down my script.. is there an easier way of checking without using || $suboption !== and is it okay if I've used !==?

Like I said I'm new to this so please don't flame.. :) I've done some research but I can't seem to find some accurate answer.

 

Thank you,

 

ZT

Link to comment
https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/
Share on other sites

To understand the difference between != and !==, you need to understand the difference between == and ===. The triple = will verify type as well as value, so it is stricter:

if( "24" == 24 ) // This will return TRUE
if( "24" === 24 ) // This will return FALSE because one is an INT, the other a STRING
if( 1 == TRUE ) // This will return TRUE
if( 1 === TRUE ) // This will return FALSE because one is an INT, the other a BOOLEAN
if( FALSE == "" ) // This will return TRUE
if( FALSE === "" ) // This will return FALSE because one is a BOOLEAN, the other a STRING

!= is the opposite of ==, so if == is TRUE, then != is FALSE. !== is the opposite of ===, so if === is TRUE, then !== is FALSE.

if( "24" != 24 ) // This will return FALSE
if( "24" !== 24 ) // This will return TRUE because one is an INT, the other a STRING
if( 1 != TRUE ) // This will return FALSE
if( 1 !== TRUE ) // This will return TRUE because one is an INT, the other a BOOLEAN
if( FALSE != "" ) // This will return FALSE
if( FALSE !== "" ) // This will return TRUE because one is a BOOLEAN, the other a STRING

Sometimes you should use one over the other, and sometimes it don't matter. It will take experience to learn when it matters and when it doesn't.

 

I hope this helps.

<?php

$value_one=26; //integer
$value_two="26"; //string

if($value_one == $value_two){
    echo "the values are identical"; //they are both `26` --- string or integer
}


/* 
* this next example featuring "===" (explicit) will never process because $value_one is an integer and $value_two is a string, yet they have the same value of `26`
* comparing two strings with explicit isn't necessary
* good practice is to compare TRUE and FALSE statements this way.
* to make sure that "true" (string) isn't the same as TRUE (boolean)
*/

if($value_one === $value_two){
    echo "the values are identical";
}

?>

 

i hope I got that right.

/* to make sure that "true" (string) isn't the same as TRUE (boolean)   */

PHP considers every non-empty string to be TRUE, except "0", when comparing a STRING to a BOOLEAN.

if( "FALSE" == TRUE ) // This will be considered TRUE because the string is not empty.
if( "FALSE" === TRUE ) // This will be considered FALSE because one is a STRING and the other a BOOLEAN

Perfect :) Thank you so much but I'm still unsure of how I can write

 

[m]  if($suboption !== "option1" || $suboption !== "option2" || $suboption !== "option3" || $suboption !== "option4" || $suboption !== "option5" || $suboption !== "option6" || $suboption !== "option7"){
            $form->setError($field, "* Unexpected error with option");
      }[/m]

 

An easier way :(

<?php //php tag for syntax highlighting
$options = array('option1','option2','option3','option4','option5','option6','option7'); //array holding options.
if(!in_array($suboption,$options)) { //if suboption is not in the options array.
            $form->setError($field, "* Unexpected error with option"); //set the error.
      }

 

There are 100's of ways to do things in PHP, doesn't make them wrong, just different.

Hmm.

 

== and != are opposites. Equal to and not equal to.

=== means equal to and of the same type.

Most notably, !== means equal to or not the same type.

 

Some examples:

null == false // true
null != false // false

null === false // false
null !== false // true

 

=== and !== should not be thought of as direct opposites as == and != are.

!== is actually more flexible than !=, and thus it will return true more often than the latter.

 

Personally, out of the four operators, !== is by far my least used one.

<?php //php tag for syntax highlighting
$options = array('option1','option2','option3','option4','option5','option6','option7'); //array holding options.
if(!in_array($suboption,$options)) { //if suboption is not in the options array.
            $form->setError($field, "* Unexpected error with option"); //set the error.
      }

 

There are 100's of ways to do things in PHP, doesn't make them wrong, just different.

 

Thank you :)

 

Hmm.

 

== and != are opposites. Equal to and not equal to.

=== means equal to and of the same type.

Most notably, !== means equal to or not the same type.

 

Some examples:

null == false // true
null != false // false

null === false // false
null !== false // true

 

=== and !== should not be thought of as direct opposites as == and != are.

!== is actually more flexible than !=, and thus it will return true more often than the latter.

 

Personally, out of the four operators, !== is by far my least used one.

 

Thank you! :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.