PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > If statement trouble
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
If statement trouble

Réponse
 
LinkBack Outils de la discussion
Vieux 25/02/2008, 20h06   #1
AceX
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 25/02/2008, 20h12   #2
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Vieux 25/02/2008, 20h22   #3
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 25/02/2008, 21h53   #4
junu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 25/02/2008, 22h21   #5
Tony
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 26/02/2008, 13h44   #6
AceX
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: If statement trouble

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?)
  Réponse avec citation
Vieux 26/02/2008, 14h05   #7
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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
==================

  Réponse avec citation
Vieux 26/02/2008, 15h08   #8
Tim Streater
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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.
  Réponse avec citation
Vieux 26/02/2008, 16h05   #9
Toby A Inkster
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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/
  Réponse avec citation
Vieux 26/02/2008, 18h16   #10
Tony
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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.
  Réponse avec citation
Vieux 26/02/2008, 23h06   #11
The Natural Philosopher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: If statement trouble

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..
  Réponse avec citation
Vieux 26/02/2008, 23h40   #12
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: If statement trouble

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
  Réponse avec citation
Vieux 26/02/2008, 23h47   #13
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: If statement trouble

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h25.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23530 seconds with 21 queries