RE: [PHP] Are these two pieces of code equivalent

From: Date: Tue, 25 Jul 2000 12:37:17 +0000
Subject: RE: [PHP] Are these two pieces of code equivalent
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-8017@lists.php.net to get a copy of this message
> From: Mário Filipe [mailto:mjnf@uevora.pt] > > Hi > > I would like to know if these pieces of code are equivalent: > > if (pg_NumRows($result) > 0){ > $num = pg_NumRows($result); > echo $num; > > Would result, for example, 10 > > And > > if ($num = pg_NumRows($result) > 0){ > echo $num > > would also result, 10 ? Well, first thing you can do is try it. There's two things different. First, ">" has higher precedence than "=", so $num would be assigned the true/false value of the comparison, and it would print "true". So fix that with parenthesis: if (($num = pg_NumRows($result) > 0) Then it would print 10, but you still have a difference: in the first case "$num" would not be assigned a value if pg_NumRows is 0. It would be equivalent to: $num = pg_NumRows($result); if ($num > 0){ echo $num;

« previous php.general (#8017) next »