Page 1 of 2
= vs ==
Posted: Fri Sep 22, 2006 8:19 am
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
But I think that I might want to be using
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
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:
Not in PHP?
I appreciate your time.
Thank you.
Posted: Fri Sep 22, 2006 8:25 am
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:
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.
Posted: Fri Sep 22, 2006 8:27 am
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:
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)
Does it mean that $var equals 12, ignoring the 'if'??
Posted: Fri Sep 22, 2006 8:28 am
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
Posted: Fri Sep 22, 2006 8:30 am
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:
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)
Does it mean that $var equals 12, ignoring the 'if'??
Your code:
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.
Posted: Fri Sep 22, 2006 8:31 am
by onion2k
jmilane wrote:But then... if you dont mind... what does this mean (in english)
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.
Posted: Fri Sep 22, 2006 8:44 am
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
Posted: Fri Sep 22, 2006 8:45 am
by aquanutz
Awesome. I'm glad I could help you learn something.

this still isnt working
Posted: Fri Sep 22, 2006 11:32 am
by jmilane
aquanutz wrote:Awesome. I'm glad I could help you learn something.

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?

Re: this still isnt working
Posted: Fri Sep 22, 2006 11:38 am
by aquanutz
jmilane wrote:aquanutz wrote:Awesome. I'm glad I could help you learn something.

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.
Re: this still isnt working
Posted: Fri Sep 22, 2006 11:40 am
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.
Re: this still isnt working
Posted: Fri Sep 22, 2006 11:43 am
by jmilane
Im really sorry, but that doesnt work.
I can leave all those fields blank and no errors are triggered...
Re: this still isnt working
Posted: Fri Sep 22, 2006 11:49 am
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.
Re: this still isnt working
Posted: Fri Sep 22, 2006 11:52 am
by jmilane
It doesnt work.
Honest to God.
Re: this still isnt working
Posted: Fri Sep 22, 2006 11:55 am
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.