If statement trouble

This is a discussion on If statement trouble within the PHP Language forums, part of the PHP Programming Forums category; I'm having trouble with this if statement: if($line[$c]=$ignore { $num--; } $ignore is defined as the string "...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-25-2008
AceX
 
Posts: n/a
Default If statement trouble

I'm having trouble with this if statement:

if($line[$c]=$ignore
{
$num--;
}


$ignore is defined as the string "ig"

$line is an array containing 5 items, some of which may be "ig"

$num starts out as a number representing the number of values in $line


The if statement you see above is nested inside a small for loop that
simply runs throught the process 5 times, and goes through the array
$line. At each point it is SUPPOSED to check and see if the current
value of $line at that particular slot ($c) is equal to "ig". If so,
then it decrements $num by 1.

My problem is it doesn't seem to care whether or not $line[$c] equal
"ig" or not. It's just decrementing $num all willy nilly.

AceX
Reply With Quote
  #2 (permalink)  
Old 02-25-2008
ZeldorBlat
 
Posts: n/a
Default Re: If statement trouble

On Feb 25, 3:06 pm, AceX <AceIncorpora...@gmail.com> wrote:
> I'm having trouble with this if statement:
>
> if($line[$c]=$ignore
> {
> $num--;
>
> }
>
> $ignore is defined as the string "ig"
>
> $line is an array containing 5 items, some of which may be "ig"
>
> $num starts out as a number representing the number of values in $line
>
> The if statement you see above is nested inside a small for loop that
> simply runs throught the process 5 times, and goes through the array
> $line. At each point it is SUPPOSED to check and see if the current
> value of $line at that particular slot ($c) is equal to "ig". If so,
> then it decrements $num by 1.
>
> My problem is it doesn't seem to care whether or not $line[$c] equal
> "ig" or not. It's just decrementing $num all willy nilly.
>
> AceX


You need == in the if test instead of just =.

= is the assignment operator. It assigns a value to a variable. ==
is a logical test for equality.

Reply With Quote
  #3 (permalink)  
Old 02-25-2008
Michael Fesser
 
Posts: n/a
Default Re: If statement trouble

..oO(AceX)

>I'm having trouble with this if statement:
>
>if($line[$c]=$ignore
>{
> $num--;
>}
>
>
>$ignore is defined as the string "ig"
>
>$line is an array containing 5 items, some of which may be "ig"
>
>$num starts out as a number representing the number of values in $line
>
>
>The if statement you see above is nested inside a small for loop that
>simply runs throught the process 5 times, and goes through the array
>$line. At each point it is SUPPOSED to check and see if the current
>value of $line at that particular slot ($c) is equal to "ig". If so,
>then it decrements $num by 1.
>
>My problem is it doesn't seem to care whether or not $line[$c] equal
>"ig" or not. It's just decrementing $num all willy nilly.


= != ==

Your code above is an assigment, not a comparison.

Micha
Reply With Quote
  #4 (permalink)  
Old 02-25-2008
junu
 
Posts: n/a
Default Re: If statement trouble

you are assigning $ignore to $line[$c], so all 5 items become 'ig',
'ig' within if always leads to successful 'if' execution,
use == operator instead to compare.

if there is only one statement within 'if' then curly brackets are not
necessary ( also for 'for', 'while',.....).

use

if($line[$c]==$ignore) $num--;

also there is another operator ===, to know about ===, see php
documentation.
Reply With Quote
  #5 (permalink)  
Old 02-25-2008
Tony
 
Posts: n/a
Default Re: If statement trouble

AceX wrote:
> I'm having trouble with this if statement:
>
> if($line[$c]=$ignore
> {
> $num--;
> }
>
>
> $ignore is defined as the string "ig"
>
> $line is an array containing 5 items, some of which may be "ig"
>
> $num starts out as a number representing the number of values in $line


In addition to using an assignment operator (=) instead of comparison
(==), you are also missing a close parenthesis:
if($line[$c]=$ignore
should be
if($line[$c]==$ignore)

And, yes, the brackets are not NEEDED if you have only one statement,
but they don't hurt.
Reply With Quote
  #6 (permalink)  
Old 02-26-2008
AceX
 
Posts: n/a
Default Re: If statement trouble

On Feb 25, 5:21*pm, Tony <nos...@example.com> wrote:
> AceX wrote:
> > I'm having trouble with this if statement:

>
> > if($line[$c]=$ignore
> > {
> > * * *$num--;
> > }

>
> > $ignore is defined as the string "ig"

>
> > $line is an array containing 5 items, some of which may be "ig"

>
> > $num starts out as a number representing the number of values in $line

>
> In addition to using an assignment operator (=) instead of comparison
> (==), you are also missing a close parenthesis:
> if($line[$c]=$ignore
> should be
> if($line[$c]==$ignore)
>
> And, yes, the brackets are not NEEDED if you have only one statement,
> but they don't hurt.


Thank you all. I don't know if anyone else has this problem, but
sometimes I just have brain lapses. I appreciate all the help guys
(and gals?)
Reply With Quote
  #7 (permalink)  
Old 02-26-2008
Jerry Stuckle
 
Posts: n/a
Default Re: If statement trouble

AceX wrote:
> On Feb 25, 5:21 pm, Tony <nos...@example.com> wrote:
>> AceX wrote:
>>> I'm having trouble with this if statement:
>>> if($line[$c]=$ignore
>>> {
>>> $num--;
>>> }
>>> $ignore is defined as the string "ig"
>>> $line is an array containing 5 items, some of which may be "ig"
>>> $num starts out as a number representing the number of values in $line

>> In addition to using an assignment operator (=) instead of comparison
>> (==), you are also missing a close parenthesis:
>> if($line[$c]=$ignore
>> should be
>> if($line[$c]==$ignore)
>>
>> And, yes, the brackets are not NEEDED if you have only one statement,
>> but they don't hurt.

>
> Thank you all. I don't know if anyone else has this problem, but
> sometimes I just have brain lapses. I appreciate all the help guys
> (and gals?)
>


Ace,

I don't know how many times I've done that - stretching all the way back
to the mid 80's when I was writing C code :-)

Also C++, Java, PHP...

It's also a common error in many of my classes - students will look and
look at the code and don't see what the problem is. When I point it out
in about 2 seconds, they feel embarrassed - until I tell them I spotted
it so quickly just because I HAVE made that mistake so many times. :-)


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #8 (permalink)  
Old 02-26-2008
Tim Streater
 
Posts: n/a
Default Re: If statement trouble

In article <iZednQvBrNDbgFnanZ2dnUVZ_oqhnZ2d@comcast.com>,
Jerry Stuckle <jstucklex@attglobal.net> wrote:

> AceX wrote:
> > On Feb 25, 5:21 pm, Tony <nos...@example.com> wrote:
> >> AceX wrote:
> >>> I'm having trouble with this if statement:
> >>> if($line[$c]=$ignore
> >>> {
> >>> $num--;
> >>> }
> >>> $ignore is defined as the string "ig"
> >>> $line is an array containing 5 items, some of which may be "ig"
> >>> $num starts out as a number representing the number of values in $line
> >> In addition to using an assignment operator (=) instead of comparison
> >> (==), you are also missing a close parenthesis:
> >> if($line[$c]=$ignore
> >> should be
> >> if($line[$c]==$ignore)
> >>
> >> And, yes, the brackets are not NEEDED if you have only one statement,
> >> but they don't hurt.

> >
> > Thank you all. I don't know if anyone else has this problem, but
> > sometimes I just have brain lapses. I appreciate all the help guys
> > (and gals?)
> >

>
> Ace,
>
> I don't know how many times I've done that - stretching all the way back
> to the mid 80's when I was writing C code :-)
>
> Also C++, Java, PHP...
>
> It's also a common error in many of my classes - students will look and
> look at the code and don't see what the problem is. When I point it out
> in about 2 seconds, they feel embarrassed - until I tell them I spotted
> it so quickly just because I HAVE made that mistake so many times. :-)


It's not only that. A certain blindness creeps in and one can overlook a
mistake a number of times even though you know there's one there. That's
when it's often much quicker to have a*fresh* set of eyes looking.
Reply With Quote
  #9 (permalink)  
Old 02-26-2008
Toby A Inkster
 
Posts: n/a
Default Re: If statement trouble

Jerry Stuckle wrote:

> It's also a common error in many of my classes - students will look and
> look at the code and don't see what the problem is. When I point it out
> in about 2 seconds, they feel embarrassed - until I tell them I spotted
> it so quickly just because I HAVE made that mistake so many times. :-)


One handy trick to help yourself avoid this problem is that whenever one
of the sides in your comparison is a constant or an expression, place that
side on the left.

For example, instead of one of these:

if ($foo == 0) { /* ... */ }
while ($bar == abs($i)) { /* ... */ }

write:

if (0 == $foo) { /* ... */ }
while (abs($i) == $bar) { /* ... */ }

That way, if you accidentally leave out an equals sign you get:

if (0 = $foo) { /* ... */ }
while (abs($i) = $bar) { /* ... */ }

and you'll get a nice compile-time error telling you the line number.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 27 days, 22:18.]

Bottled Water
http://tobyinkster.co.uk/blog/2008/02/18/bottled-water/
Reply With Quote
  #10 (permalink)  
Old 02-26-2008
Tony
 
Posts: n/a
Default Re: If statement trouble

Jerry Stuckle wrote:
> AceX wrote:
>>
>> Thank you all. I don't know if anyone else has this problem, but
>> sometimes I just have brain lapses. I appreciate all the help guys
>> (and gals?)
>>

>
> Ace,
>
> I don't know how many times I've done that - stretching all the way back
> to the mid 80's when I was writing C code :-)
>
> Also C++, Java, PHP...
>
> It's also a common error in many of my classes - students will look and
> look at the code and don't see what the problem is. When I point it out
> in about 2 seconds, they feel embarrassed - until I tell them I spotted
> it so quickly just because I HAVE made that mistake so many times. :-)


I don't know how often I've simply asked someone to take a look at some
code - because sometimes you get too close to the code, and you just
don't see those sort of simple errors. Someone else's fresh perspective
is all it takes.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:28 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0