Re: [PHP] assignment operator works for comparison??
Dan wrote: > > This confused me for awhile, because the single equal sign seemed to work > for comparison, but created inexplicable errors in my programs. It seems > strange to me that a successful variable value assignment does not return > true. > > example: > > > $shiny = 1; > if($shiny = 0){ echo("This wont print"); } > echo( $shiny ); //this will return 0 > > ?> > > --Dan > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] Okay... First: Checking if $shiny equals 0: if ($shiny == 0) { But that's already been discussed. If you _realy_ want to check if the assignment succeeded: if (($shiny = 0) == 0) { Try the following example: - $a = "Hello"; $b = "Wold"; if ($b == $a) print "Yes, b equals a\n"; else print "No, b doesn't equal a\n"; if (($b = $a) == $a) print "Yes... now b equals a\n"; else print "Oops... b still doesn't equal a!\n"; - Output will be: No, b doesn't equal a Yes... now b equals a -- * R&zE: *** ** Renze Munnik ** ** E: [EMAIL PROTECTED] ** M: +31 6 218 111 43 *** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] assignment operator works for comparison??
Hi, Dan wrote: > > > if($shiny = 0){ > > This does not compare anyting, it assigns 0 to $shiny > > yes i know, but shouldnt this operation return true? No, it doesn't return true. The "=" operator returns the value of the expression on its right hand side. Therefore, the statement given above is equivalent to (as far as the if is concerned): if (0) { because the "=" operator returns 0, the value on its right hand side. That is why a statement like: a = b = c = 2 sets all the above variables to 2. Otherwise, if it were to operate the way you think it should, it would set "c" to 2 and "a" and "b" to "true" ;-). Hope that clears up things for you. -- Regards, Harshdeep Singh Jawanda. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re[2]: [PHP] assignment operator works for comparison??
Hello Dan, Wednesday, April 11, 2001, 1:29:22 AM, you wrote: >> >> > if($shiny = 0){ >> This does not compare anyting, it assigns 0 to $shiny D> yes i know, but shouldnt this operation return true? No. This operator returns the value assigned (0 in this case, which is false). This expression will always return false. -- Best regards, Maxim Derkachev mailto:[EMAIL PROTECTED] Symbol-Plus Publishing Ltd. phone: +7 (812) 324-53-53 http://www.Books.Ru -- All Books of Russia -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] assignment operator works for comparison??
> if($shiny = 0) This line is the same as if ((shiny = 0) == TRUE) It's common error with PHP and C. You could make use of this like if ($fp = fopen($filename,'r')) since this is the same as if (($fp = fopen($filename,'r')) == TRUE) code after this line is executed when fopen() success to open file. Regards, -- Yasuo Ohgaki ""Dan"" <[EMAIL PROTECTED]> wrote in message 9avrti$olc$[EMAIL PROTECTED]">news:9avrti$olc$[EMAIL PROTECTED]... > This confused me for awhile, because the single equal sign seemed to work > for comparison, but created inexplicable errors in my programs. It seems > strange to me that a successful variable value assignment does not return > true. > > example: > > > $shiny = 1; > if($shiny = 0){ echo("This wont print"); } > echo( $shiny ); //this will return 0 > > ?> > > --Dan > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] assignment operator works for comparison??
You are right, thank you. > It looks to me like the value of an assignment is the value assigned, as in > Perl. But I don't know for sure, haven't come across this in the manual. > > Kirk > > > > > if($shiny = 0){ > > > This does not compare anyting, it assigns 0 to $shiny > > > > yes i know, but shouldnt this operation return true? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] assignment operator works for comparison??
It looks to me like the value of an assignment is the value assigned, as in Perl. But I don't know for sure, haven't come across this in the manual. Kirk > -Original Message- > From: Dan [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, April 10, 2001 3:29 PM > To: [EMAIL PROTECTED] > Subject: Re: [PHP] assignment operator works for comparison?? > > > > > > > > if($shiny = 0){ > > This does not compare anyting, it assigns 0 to $shiny > > yes i know, but shouldnt this operation return true? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] assignment operator works for comparison??
> > > if($shiny = 0){ > This does not compare anyting, it assigns 0 to $shiny yes i know, but shouldnt this operation return true? > > > echo( $shiny ) // this will return 0 > That's normal, you just assign 0 to it ;) > > = assignment operator > == comparison operator > > py > > > At 01:53 PM 4/10/01 -0700, you wrote: > >This confused me for awhile, because the single equal sign seemed to work > >for comparison, but created inexplicable errors in my programs. It seems > >strange to me that a successful variable value assignment does not return > >true. > > > >example: > > > > > > > $shiny = 1; > > if($shiny = 0){ echo("This wont print"); } > > echo( $shiny ); //this will return 0 > > > >?> > > > >--Dan > > > > > > > >-- > >PHP General Mailing List (http://www.php.net/) > >To unsubscribe, e-mail: [EMAIL PROTECTED] > >For additional commands, e-mail: [EMAIL PROTECTED] > >To contact the list administrators, e-mail: [EMAIL PROTECTED] > > > + == > + Pierre-Yves Lem@ire > + E-MedHosting.com > + (514) 729-8100 > + [EMAIL PROTECTED] > + == > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] assignment operator works for comparison??
> if($shiny = 0){ This does not compare anyting, it assigns 0 to $shiny > echo( $shiny ) // this will return 0 That's normal, you just assign 0 to it ;) = assignment operator == comparison operator py At 01:53 PM 4/10/01 -0700, you wrote: >This confused me for awhile, because the single equal sign seemed to work >for comparison, but created inexplicable errors in my programs. It seems >strange to me that a successful variable value assignment does not return >true. > >example: > > > $shiny = 1; > if($shiny = 0){ echo("This wont print"); } > echo( $shiny ); //this will return 0 > >?> > >--Dan > > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] >To contact the list administrators, e-mail: [EMAIL PROTECTED] + == + Pierre-Yves Lem@ire + E-MedHosting.com + (514) 729-8100 + [EMAIL PROTECTED] + == -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] assignment operator works for comparison??
This confused me for awhile, because the single equal sign seemed to work for comparison, but created inexplicable errors in my programs. It seems strange to me that a successful variable value assignment does not return true. example: --Dan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]