|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
..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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Feb 25, 5:21pm, 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 guys (and gals?) |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 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 ================== |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 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. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 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/ |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
Tony wrote:
> 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 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. I spent nearly half an hour beating my brains out till I realised the variable I was comparing had a subtle spelling error. In C you get an undeclared variable type error..not (necessarily) in PHP.. |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
On Wed, 27 Feb 2008 00:06:43 +0100, The Natural Philosopher <a@b.c> wrote:
> Tony wrote: >> 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 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. > I spent nearly half an hour beating my brains out till I realised the > variable I was comparing had a subtle spelling error. > > In C you get an undeclared variable type error..not (necessarily) in > PHP.. .... which is why you should control the error_reporting level in a development environment. -- Rik Wasmus |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
On Tue, 26 Feb 2008 19:16:11 +0100, Tony <nospam@example.com> wrote:
> 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 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. Hehe, the numner of times way back when I started out to ask this group something, tried to boil it down to it's essentials, and the answer allready became clear to me. Asking oneself how to ask another efficiently for with a particular problem often yields the result without having to actually ask . It's kept the number of embarrassments quite low,spending a good half hour on a question, and then just discarding it .Offcourse, the mistake in this case is one we all make/made, and less easily spotted with aforementioned method. -- Rik Wasmus |
|
![]() |
| Outils de la discussion | |
|
|