= vs ==

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

= vs ==

Post by jmilane »

I am having trouble checking variables... specifically, if variables are equal to something.

If a variable has not been set, I have been coding

Code: Select all

if $var = ''
But I think that I might want to be using

Code: Select all

if var == ''
I just dont understand the distinction.

So, if I am running down a list and setting a flag:

First setting the variable to "set" because it is my default and what I am checking against

Code: Select all

$flag = "set"
Then looking to disprove the statement by running through my code:

Code: Select all

if ($first_item == '') {
$flag = "not set";
}
Perhaps I want to be running through my code like so:

Code: Select all

if ($first_item = '') {
$flag = "not set";
}
Which is correct, and why?

Will the '=' always assign value - "equals", even if part of an if statement? (Kind of ignoring the 'if'... which is why I am confused)

Does the '==' act as a comparison operator - "is equal to"

And what about NULL? We used to code:

Code: Select all

if @var ISNULL
Not in PHP?


I appreciate your time.

Thank you.
Last edited by jmilane on Fri Sep 22, 2006 8:26 am, edited 1 time in total.
aquanutz
Forum Commoner
Posts: 28
Joined: Thu Sep 14, 2006 9:07 am

Post by aquanutz »

in php (as well as many other langauges when you use '=' it means 'set equal to' so you are assigning the variable in front of the '=' to the value to the right of it.

so:

Code: Select all

$var = 12;
will set the variable $var to be the value of 12.

now the '==' basically means 'is equal to' and is used when you are comparing a variable to a value and not assigning it a value like above, so:

Code: Select all

if($var == 12)
      echo "Yes, var is 12";
check to see if $var is equal to 12 and if it is, print that it is indeed equal to 12.

I hope this helps.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

aquanutz wrote:in php (as well as many other langauges when you use '=' it means 'set equal to' so you are assigning the variable in front of the '=' to the value to the right of it.

so:

Code: Select all

$var = 12;
will set the variable $var to be the value of 12.

now the '==' basically means 'is equal to' and is used when you are comparing a variable to a value and not assigning it a value like above, so:

Code: Select all

if($var == 12)
      echo "Yes, var is 12";
check to see if $var is equal to 12 and if it is, print that it is indeed equal to 12.

I hope this helps.
Thanks, it does help.

But then... if you dont mind... what does this mean (in english)

Code: Select all

if ($var = 12)
Does it mean that $var equals 12, ignoring the 'if'??
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

= is assignment operator
== is comparison operator

using == with if statements is correct since it compares values of $first_name to an empty string

http://www.developer.com/lang/article.php/938511

http://us3.php.net/operators

http://www.tizag.com/phpT/operators.php
aquanutz
Forum Commoner
Posts: 28
Joined: Thu Sep 14, 2006 9:07 am

Post by aquanutz »

jmilane wrote:
aquanutz wrote:in php (as well as many other langauges when you use '=' it means 'set equal to' so you are assigning the variable in front of the '=' to the value to the right of it.

so:

Code: Select all

$var = 12;
will set the variable $var to be the value of 12.

now the '==' basically means 'is equal to' and is used when you are comparing a variable to a value and not assigning it a value like above, so:

Code: Select all

if($var == 12)
      echo "Yes, var is 12";
check to see if $var is equal to 12 and if it is, print that it is indeed equal to 12.

I hope this helps.
Thanks, it does help.

But then... if you dont mind... what does this mean (in english)

Code: Select all

if ($var = 12)
Does it mean that $var equals 12, ignoring the 'if'??
Your code:

Code: Select all

if ($var = 12)

will actually set the $var variable to 12 no matter what (assuming it's a valid statement) and then execute what is in the rest of the 'if' statement.
Last edited by aquanutz on Fri Sep 22, 2006 8:44 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

jmilane wrote:But then... if you dont mind... what does this mean (in english)

Code: Select all

if ($var = 12)
Does it mean that $var equals 12, ignoring the 'if'??
When you set a variable it'll return true if it works, and false if it fails, therefore that code means "set $var to 12, and if that assignment works then do what comes next". As a rule you probably don't want to mix assignments with comparisons. It'll just get confusing.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

onion2k wrote: It'll just get confusing.
Indeed.

You fellows rock.

Thanks a lot.

By the way, if you compared these questions with my first questions a month or so ago, Im learning in quantum leaps!

Thanks...

J
aquanutz
Forum Commoner
Posts: 28
Joined: Thu Sep 14, 2006 9:07 am

Post by aquanutz »

Awesome. I'm glad I could help you learn something. :D
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

this still isnt working

Post by jmilane »

aquanutz wrote:Awesome. I'm glad I could help you learn something. :D
Please, what am I doing wrong?

Code: Select all

if (isset($_POST['employment_chkbox'])) {  //with these, I am trying to see if at least one checkbox has been checked.
  $checked = "yes"; // because if it is set, I want $checked to equal 'yes
}

if (isset($_POST['business_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['certification_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['youth_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['parent_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['advocacy_chkbox'])) { 
$checked = "yes"; 
} 

if ($checked == "no")   // so if nothing was checked... echo the error message. This DOESNT work. 
{
echo "Please use your browser to go back and select at least one program.";
}

-------------------------------------------------------- below is checking text fields-------------------------------------------

$error = "You need to go back and fill in these fields:<br>"; // this is the base error message for required fields

if ($firstname == '') {
$error = "$error. <br>first name"; // I am saying, if $firstname is nothing... if it hasnt been entered, add first name to the list
}

if ($lastname == '') {
$error = "$error. <br>last name" ;  //and so on and so on
}

if ($month == '') {
$error = "$error. <br>month of birth" ;
}

if ($day == '') {
$error = "$error. <br>day of birth"; 
}

if ($year == '') {
$error = "$error. <br>year of birth" ;
}

if ($street == '') {
$error = "$error. <br>street"; 
} 

if ($city == '') {
$error = "$error. <br>city"; 
} 

if ($state == '') {
$error = "$error. <br>state"; 
}

if ($postalcode == '') {
$error = "$error. <br>zip code"; 
}

if ($homephone == '') {
$error = "$error. <br>home phone"; 
}
 
if ($referral == '') {
$error = "$error. <br>referral"; 
}

if ($contact == '') {
$error = "$error. <br>best time to contact"; 
}

if ($race = '') {
$error = "$error. <br>race" ;
}

if ($gender == '') {
$error = "$error. <br>gender" ;
}

echo $error;// this doesnt work. doesnt echo ANYTHING (and I do know I still need to check if errors were set)


Why is this so hard for me?

:(
aquanutz
Forum Commoner
Posts: 28
Joined: Thu Sep 14, 2006 9:07 am

Re: this still isnt working

Post by aquanutz »

jmilane wrote:
aquanutz wrote:Awesome. I'm glad I could help you learn something. :D
Please, what am I doing wrong?

Code: Select all

if (isset($_POST['employment_chkbox'])) {  //with these, I am trying to see if at least one checkbox has been checked.
  $checked = "yes"; // because if it is set, I want $checked to equal 'yes
}

if (isset($_POST['business_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['certification_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['youth_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['parent_chkbox'])) { 
$checked = "yes"; 
} 

if (isset($_POST['advocacy_chkbox'])) { 
$checked = "yes"; 
} 

if ($checked == "no")   // so if nothing was checked... echo the error message. This DOESNT work. 
{
echo "Please use your browser to go back and select at least one program.";
}

-------------------------------------------------------- below is checking text fields-------------------------------------------

$error = "You need to go back and fill in these fields:<br>"; // this is the base error message for required fields

if ($firstname == '') {
$error = "$error. <br>first name"; // I am saying, if $firstname is nothing... if it hasnt been entered, add first name to the list
}

if ($lastname == '') {
$error = "$error. <br>last name" ;  //and so on and so on
}

if ($month == '') {
$error = "$error. <br>month of birth" ;
}

if ($day == '') {
$error = "$error. <br>day of birth"; 
}

if ($year == '') {
$error = "$error. <br>year of birth" ;
}

if ($street == '') {
$error = "$error. <br>street"; 
} 

if ($city == '') {
$error = "$error. <br>city"; 
} 

if ($state == '') {
$error = "$error. <br>state"; 
}

if ($postalcode == '') {
$error = "$error. <br>zip code"; 
}

if ($homephone == '') {
$error = "$error. <br>home phone"; 
}
 
if ($referral == '') {
$error = "$error. <br>referral"; 
}

if ($contact == '') {
$error = "$error. <br>best time to contact"; 
}

if ($race = '') {
$error = "$error. <br>race" ;
}

if ($gender == '') {
$error = "$error. <br>gender" ;
}

echo $error;// this doesnt work. doesnt echo ANYTHING (and I do know I still need to check if errors were set)


Why is this so hard for me?

:(
What exactly is the problem you are having with the code?

Is it because of the 'if' statements?? if that is it, try this instead:

Code: Select all

if ($firstname == NULL) {
$error = "$error. <br>first name"; // I am saying, if $firstname is nothing... if it hasnt been entered, add first name to the list
}

if ($lastname == NULL) {
$error = "$error. <br>last name" ;  //and so on and so on
}

if ($month == NULL) {
$error = "$error. <br>month of birth" ;
}

if ($day == NULL) {
$error = "$error. <br>day of birth"; 
}

if ($year == NULL) {
$error = "$error. <br>year of birth" ;
}

if ($street == NULL) {
$error = "$error. <br>street"; 
} 

if ($city == NULL) {
$error = "$error. <br>city"; 
} 

if ($state == NULL) {
$error = "$error. <br>state"; 
}

if ($postalcode == NULL) {
$error = "$error. <br>zip code"; 
}

if ($homephone == NULL) {
$error = "$error. <br>home phone"; 
}
 
if ($referral == NULL) {
$error = "$error. <br>referral"; 
}

if ($contact == NULL) {
$error = "$error. <br>best time to contact"; 
}

if ($race = NULL) {
$error = "$error. <br>race" ;
}

if ($gender == NULL) {
$error = "$error. <br>gender" ;
}

echo $error;
When I first started developing things it was very difficult for me to grasp some concepts (and still is in some regards) and since I am new to PHP and MySQL I am still rough around the edges on them too.

Anyhow, if that doens't help, let me know exactly what the problem is (always do that when you need help) and I'll try to better help you.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Re: this still isnt working

Post by jmilane »

Thanks so much...

... the problem is that even if I check nothing, it does not cause an error. Goes through like there is no problem.

Thanks for your time... really.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Re: this still isnt working

Post by jmilane »

Im really sorry, but that doesnt work.

I can leave all those fields blank and no errors are triggered...
aquanutz
Forum Commoner
Posts: 28
Joined: Thu Sep 14, 2006 9:07 am

Re: this still isnt working

Post by aquanutz »

jmilane wrote:Thanks so much...

... the problem is that even if I check nothing, it does not cause an error. Goes through like there is no problem.

Thanks for your time... really.
Ok, lets see here.. This is how I would write your top portion of code:

Code: Select all

$checked = "no";

if ($_POST['employment_chkbox'] == "yes") {  //with these, I am trying to see if at least one checkbox has been checked. 
  $checked = "yes"; // because if it is set, I want $checked to equal 'yes 
} 

else if ($_POST['business_chkbox'] == "yes") { 
$checked = "yes"; 
} 

else if ($_POST['certification_chkbox'] == "yes") { 
$checked = "yes"; 
} 

else if ($_POST['youth_chkbox'] == "yes") { 
$checked = "yes"; 
} 

else if ($_POST['parent_chkbox'] == "yes") { 
$checked = "yes"; 
} 

else if ($_POST['advocacy_chkbox'] == "yes") { 
$checked = "yes"; 
} 

if ($checked == "no")   // so if nothing was checked... echo the error message. This DOESNT work. 
{ 
echo "Please use your browser to go back and select at least one program."; 
}
You should set $checked equal to "no" right off the bat becaue that is what you want to compare against later, so if it still equals "no" you know that nothing was checked. If you leave it a blank variable, weird things can happen (mabye not in PHP, but in other languages too). Also, you don't have to use 'isset' in this case so I prefer not to. Just check to see if the check box is checked... and if it is, assign your variable accordingly. Try that to see if it helps.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Re: this still isnt working

Post by jmilane »

It doesnt work.

Honest to God.
aquanutz
Forum Commoner
Posts: 28
Joined: Thu Sep 14, 2006 9:07 am

Re: this still isnt working

Post by aquanutz »

jmilane wrote:It doesnt work.

Honest to God.
'

Oh dear, can you post your html file and php file for me to look over? I'm currently at work and I am leaving in about 20 minutes, but if you get it up before I take off for class, I'll gladly continue to help you.
Post Reply