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 "...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
..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 |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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?) |
|
|||
|
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 ================== |
|
|||
|
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. |
|
|||
|
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/ |
|
|||
|
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. |