RE: [PHP] Odd Code Error.
[snip] Interesting. I wouldn't have expected that. However, that is how it should work. Check out table K-2 on this page: http://us4.php.net/manual/en/types.comparisons.php '==' is a loose comparison. You can use '===' instead which will give you the results you are looking for. if ($EA === NFH) Hmm after all this time, are you saying its best to use === instead of == in any application ? [/snip] For strings, yes. And you should also re-order the question ... if(NFH === $EA) Why, you ask? Because this will prevent possible assignment errors. If you accidentally type if($EA = NFH) NFH gets assigned to $EA and is true...you might not see the error until quite a bit later. However if you accidentally type if(NFH = $EA) an error will get thrown right away. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Odd Code Error.
On 21 January 2004 04:01, Luke contributed these pearls of wisdom: ok, i read the section, but even so if $a == $b and $a == $c then $b should be equal to $c No, not necessarily! but php is saying otherwise? Yes. this sounds confusing i want to try n get my head round it a string equals a integer of zero, and a string equals true, but the reason the bool doesnt equal the int is because when the string and int are compared, the string is zero (because it has no numerical value)? did that make sense? am i right? Well, I'm not sure 'cos I find your reasoning hard to follow...!! ;) The essentials go like this: when performing most comparisons (==, !=, , , =, =), if the operands are of different types then at least one of them has a type conversion performed on it -- and it's the rules about which one is converted that determines what the outcome will be. The following is based on empirical results, but I think is fairly accurate (except, possibly, in one or two edge cases): * If either operand is numeric (integer or float), the other one is converted to the same type and the comparison performed. * If either operand is Boolean, the other operand is converted to Boolean and the comparison performed. * If both operands are strings: - if both are strictly representations of decimal numbers, convert both to numeric and compare. - otherwise compare as strings. * Otherwise (I think, but who really cares by now!) do exact comparison. Note particularly the behaviour if both operands are strings -- this means that, for example: '0123' == '123' and '0123' '122.3' but '0123a' != '123a' and '0123x' '122.3x' Thus, you shouldn't use loose comparisons for comparing strings if there's *any* chance that both operands will look like numbers *and* you want a strictly string-type comparison -- in those circumstances, strcmp() is very much your friend. ;) Cheers! Mike -- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Thanks. It makes sense now. Now I have one more question. Not to confuse the issue more. :) $EA = 0 if ($EA == NFH) // would work because the string is converted to an integer and then compared correct ? And by using === I tell it to compare type and value ? If that is true would 0 == 0? I know I can run it through a string to integer function which is probably a good idea anyways. Thanks again for all your responses. Jonathan Pitcher On Jan 21, 2004, at 9:21 AM, Ford, Mike [LSS] wrote: On 21 January 2004 04:01, Luke contributed these pearls of wisdom: ok, i read the section, but even so if $a == $b and $a == $c then $b should be equal to $c No, not necessarily! but php is saying otherwise? Yes. this sounds confusing i want to try n get my head round it a string equals a integer of zero, and a string equals true, but the reason the bool doesnt equal the int is because when the string and int are compared, the string is zero (because it has no numerical value)? did that make sense? am i right? Well, I'm not sure 'cos I find your reasoning hard to follow...!! ;) The essentials go like this: when performing most comparisons (==, !=, , , =, =), if the operands are of different types then at least one of them has a type conversion performed on it -- and it's the rules about which one is converted that determines what the outcome will be. The following is based on empirical results, but I think is fairly accurate (except, possibly, in one or two edge cases): * If either operand is numeric (integer or float), the other one is converted to the same type and the comparison performed. * If either operand is Boolean, the other operand is converted to Boolean and the comparison performed. * If both operands are strings: - if both are strictly representations of decimal numbers, convert both to numeric and compare. - otherwise compare as strings. * Otherwise (I think, but who really cares by now!) do exact comparison. Note particularly the behaviour if both operands are strings -- this means that, for example: '0123' == '123' and '0123' '122.3' but '0123a' != '123a' and '0123x' '122.3x' Thus, you shouldn't use loose comparisons for comparing strings if there's *any* chance that both operands will look like numbers *and* you want a strictly string-type comparison -- in those circumstances, strcmp() is very much your friend. ;) Cheers! Mike -- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Wed, 2004-01-21 at 15:38, Jonathan Pitcher wrote: Thanks. It makes sense now. Now I have one more question. Not to confuse the issue more. :) $EA = 0 if ($EA == NFH) // would work because the string is converted to an integer and then compared correct ? And by using === I tell it to compare type and value ? If that is true would 0 == 0? I know I can run it through a string to integer function which is probably a good idea anyways. ?php sarcasmOn() ? I know of a REALLY simple way to find out... but involves a lot of work. You would have to create a teeny tiny file and add a teeny tiny amount of code, something like follows: test.php: ?php echo (0 == 0 ? 'Yay!' : 'Naay!'); ? Crap that's too long, a better bet would be to ask on the list. Much shorter and quicker response time. And you don't even have to think for yourself. ?php sarcasmOff() ? Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Luke wrote: yeah its strange, $test is equal to $test2, $test is equal to $test3 but $test2 is not equal to $test3, $test = string; $test2 = true; $test3 = 0; if($test == $test2){ echo hi; } somone tell me im wrong please? else thats seriously weird I'm sure many will disagree with me on this but, this is EXACTLY why I think all this loose type conversion in php, perl and others, is a bad idea. An int should be an int and a string should be a string. It's not that hard to write and use functions like strtoint() and inttostr(). If you want an easy way to compare 2 strings with out the old C function strcmp(), then use operator overloading like in C++. I also think that not forcing the user to declare variables and types before they are used is also a very bad idea and will lead to far more subtle bugs than if you do use type declarations. Chris W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
I agree with Chris... In contrast you can also see a common problem in many of the downloadable scripts from different collections on the net, like hotscripts.com, where the programmers start building control structures whitout having the different variables they use set. For instance if($name) instead of if(isset($name)), and it is done, simply bacuse php does not force you to declare your variables. I also help out in different danish internet forums, and there are some of the newbees that have no clue about type juggeling, which at times can cause problems. And start explaing to another person, who is not an experienced programmer about types over the net, is not that easy, and many times only leads to more confusion. /Martin I'm sure many will disagree with me on this but, this is EXACTLY why I think all this loose type conversion in php, perl and others, is a bad idea. An int should be an int and a string should be a string. It's not that hard to write and use functions like strtoint() and inttostr(). If you want an easy way to compare 2 strings with out the old C function strcmp(), then use operator overloading like in C++. I also think that not forcing the user to declare variables and types before they are used is also a very bad idea and will lead to far more subtle bugs than if you do use type declarations. Chris W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Hmmm, I don't think other programmer's sloppy coding practices are a good argument for having to declare variables or against loose typing. There are crappy coders in whatever language you use. The good coders will do the right thing. If you are concerned about scripts on hotscripts.com where the coder doesn't seem to understand the principles of security or fully understand what is happening with their variables, then perhaps you should be writing the code yourself or hiring someone who does understand the language more completely. Caveat Emptor. Part of PHPs simplicity and attractiveness is that you DON'T need to declare variables, and you DON'T need to do manual casting. Start adding these and you may as well be writing Java or C/C++. Cheers, Rob. On Wed, 2004-01-21 at 17:45, Martin Hjort Eriksen wrote: I agree with Chris... In contrast you can also see a common problem in many of the downloadable scripts from different collections on the net, like hotscripts.com, where the programmers start building control structures whitout having the different variables they use set. For instance if($name) instead of if(isset($name)), and it is done, simply bacuse php does not force you to declare your variables. I also help out in different danish internet forums, and there are some of the newbees that have no clue about type juggeling, which at times can cause problems. And start explaing to another person, who is not an experienced programmer about types over the net, is not that easy, and many times only leads to more confusion. /Martin I'm sure many will disagree with me on this but, this is EXACTLY why I think all this loose type conversion in php, perl and others, is a bad idea. An int should be an int and a string should be a string. It's not that hard to write and use functions like strtoint() and inttostr(). If you want an easy way to compare 2 strings with out the old C function strcmp(), then use operator overloading like in C++. I also think that not forcing the user to declare variables and types before they are used is also a very bad idea and will lead to far more subtle bugs than if you do use type declarations. Chris W -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
I agree with Chris... In contrast you can also see a common problem in many of the downloadable scripts from different collections on the net, like hotscripts.com, where the programmers start building control structures whitout having the different variables they use set. For instance if($name) instead of if(isset($name)), and it is done, simply bacuse php does not force you to declare your variables. I also help out in different danish internet forums, and there are some of the newbees that have no clue about type juggeling, which at times can cause problems. And start explaing to another person, who is not an experienced programmer about types over the net, is not that easy, and many times only leads to more confusion. So do you recommend using isset($name) instead of ($name) ? And what are you meaning by setting the var as in var $name ? i usually set them in classes but how about in normal scripts and functions ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
So do you recommend using isset($name) instead of ($name) ? And what are you meaning by setting the var as in var $name ? i usually set them in classes but how about in normal scripts and functions ? Hmm i was checking out a pear class, what about the variables within a function ? like function foo($name) { if ($name) { } } or foo($name) { if (isset($name)) { } } it uses isset on variables coming outside the function -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
[EMAIL PROTECTED] wrote: So do you recommend using isset($name) instead of ($name) ? And what are you meaning by setting the var as in var $name ? i usually set them in classes but how about in normal scripts and functions ? Yes, if you want to examine if a variable is set or not, then you should use isset(). Exactly how a variable should be set, well it could be for instance var $age:int; But you could also do both things at the same time, for instance that you PHP5 can make class type hints about the parameters to a method, but you can also not do it. /Martin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
[EMAIL PROTECTED] wrote: Hmm i was checking out a pear class, what about the variables within a function ? like function foo($name) { if ($name) { } } or foo($name) { if (isset($name)) { } } it uses isset on variables coming outside the function Those examples don't make much sense. Without a default value, if foo() is called without a parameter, then it'll cause an error. So, obviously $name is going to be set. So isset() will always be true. if($name) is a valid check, I guess, since an empty string or zero could be passed and cause that to fail. It'd make more sense to set a default value for $name or check for an actual value instead of just seeing if (boolean)$name is TRUE or FALSE. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Yes, if you want to examine if a variable is set or not, then you should use isset(). Ok i'll start using it Exactly how a variable should be set, well it could be for instance var $age:int; Where on the php site does it tell you to set it like that ? I've never seent that before ? But you could also do both things at the same time, for instance that you PHP5 can make class type hints about the parameters to a method, but you can also not do it. I dont get it do you have an example ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
[EMAIL PROTECTED] wrote: Exactly how a variable should be set, well it could be for instance var $age:int; Where on the php site does it tell you to set it like that ? I've never seent that before ? It is also because you cannot do it... :) It was an idea on how it could be done, with a little inspiration from pascal. But you could also do both things at the same time, for instance that you PHP5 can make class type hints about the parameters to a method, but you can also not do it. I dont get it do you have an example ? Well the changes document for PHP5 explains it pretty good: In PHP5 this: |?php function foo(ClassName $object) { // ... } ? | is equivalent to in PHP4: | ?php function foo($object) { if (!($object instanceof ClassName)) { die(Argument 1 must be an instance of ClassName); } } ? | Meaning that if you hint the class type of a parameter of your method, and you set the parameter with a different type when you call the parameter, it will produce an error. You can read more about it here: http://www.php.net/zend-engine-2.php /Martin
Re: [PHP] Odd Code Error.
[EMAIL PROTECTED] wrote: Hmm i was checking out a pear class, what about the variables within a function ? like function foo($name) { if ($name) { } } or foo($name) { if (isset($name)) { } } it uses isset on variables coming outside the function Those examples don't make much sense. Without a default value, if foo() is called without a parameter, then it'll cause an error. So, obviously $name is going to be set. So isset() will always be true. if($name) is a valid check, I guess, since an empty string or zero could be passed and cause that to fail. It'd make more sense to set a default value for $name or check for an actual value instead of just seeing if (boolean)$name is TRUE or FALSE. I dont understand, i assume that would check if name was set, i also set functions like function foo($name,$bar = null) { } so therefore bar doesnt need to be inputted, seeing as i set it to null would i check if $bar===true ? or just isset($bar ) ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
[EMAIL PROTECTED] wrote: I dont understand, i assume that would check if name was set, i also set functions like function foo($name,$bar = null) { } so therefore bar doesnt need to be inputted, seeing as i set it to null would i check if $bar===true ? or just isset($bar ) ? when you have set $bar in that fashion then isset($bar) will come out as false. And $bar === true will only become true, if $bar is true, look at table K-3 here http://www.php.net/manual/en/types.comparisons.php /Martin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
And $bar === true will only become true, if $bar is true, look at table K-3 here http://www.php.net/manual/en/types.comparisons.php Ok i tested it out function foo($bar = null) { if (isset($bar)) die(yes); } foo(test); will die where foo(); wont -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Robert Cummings wrote: Hmmm, I don't think other programmer's sloppy coding practices are a good argument for having to declare variables or against loose typing. There are crappy coders in whatever language you use. The good coders will do the right thing. If you are concerned about scripts on hotscripts.com where the coder doesn't seem to understand the principles of security or fully understand what is happening with their variables, then perhaps you should be writing the code yourself or hiring someone who does understand the language more completely. Caveat Emptor. Part of PHPs simplicity and attractiveness is that you DON'T need to declare variables, and you DON'T need to do manual casting. Start adding these and you may as well be writing Java or C/C++. I am going to have to strongly disagree with some of what Rob said. I agree that sloppy coding of idiots should have not impact on the design of a programming language. However I see no disadvantages to forcing you to declaring variables. Even if you are going to have loose type conversions you can still declare variables and give their intended type. If this is done the loose type conversion can be done more intelligently. It also goes a long way to documenting your code. A section at the top of a source file with a declaration of all variables and functions used makes provides an easy and consistent way to document your code. Well named functions and variables names alone, when they are all spelled out in a logical order at the top of a source file does a lot to document the code. On top of that there is a HUGE advantage to even the best programmer. Let me give you a recent example. lots of code; $Key = xyz; lots more code here. if($key == xyz){ do this stuff; } lots more code here; I am assuming you found the error pretty quick, but when looking at the whole file and not knowing where the error is it can be very hard. There could be lots of other variable names and assignments and other logic that all could have been effecting my erroneous output. If the language forces variable deceleration you simply get an error variable $key not defined on line x then there is no searching no wondering, you immediately know you made a typo, and you know exactly what line it is on. I challenge anyone to find a disadvantage in forcing decelerations of variables and functions, other than having to type more. I am sure there will be some but I am also reasonably sure I can counter all of them except the extra typing, which I have never found to be a significant factor. Chris W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Chris W wrote: snip I challenge anyone to find a disadvantage in forcing decelerations of variables and functions, other than having to type more. Forcing declaration will create more list traffic... I'm getting the error 'Variable $foo not declared on line 321', somebody fix my code... And Jason will have to spend more time telling posters to RTFM. ;) -- By-Tor.com It's all about the Rush http://www.by-tor.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Wed, 2004-01-21 at 20:19, Chris W wrote: Robert Cummings wrote: Hmmm, I don't think other programmer's sloppy coding practices are a good argument for having to declare variables or against loose typing. There are crappy coders in whatever language you use. The good coders will do the right thing. If you are concerned about scripts on hotscripts.com where the coder doesn't seem to understand the principles of security or fully understand what is happening with their variables, then perhaps you should be writing the code yourself or hiring someone who does understand the language more completely. Caveat Emptor. Part of PHPs simplicity and attractiveness is that you DON'T need to declare variables, and you DON'T need to do manual casting. Start adding these and you may as well be writing Java or C/C++. I am going to have to strongly disagree with some of what Rob said. I Definately not the first *grin*. agree that sloppy coding of idiots should have not impact on the design of a programming language. However I see no disadvantages to forcing you to declaring variables. Even if you are going to have loose type conversions you can still declare variables and give their intended type. If this is done the loose type conversion can be done more intelligently. It also goes a long way to documenting your code. A section at the top of a source file with a declaration of all variables and functions used makes provides an easy and consistent way to document your code. Well named functions and variables names alone, when they are all spelled out in a logical order at the top of a source file does a lot to document the code. On top of that there is a HUGE advantage to even the best programmer. Let me give you a recent example. lots of code; $Key = xyz; lots more code here. if($key == xyz){ do this stuff; } lots more code here; I am assuming you found the error pretty quick, but when looking at the I code with ALL errors enabled. This would fire an error as soon as the code ran. So I have no problem with non declared variables. Generally speaking though, I do declare variables at the top of my functions too, but I picked that up from writing C code. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Wed, 2004-01-21 at 20:19, Chris W wrote: Robert Cummings wrote: May i ask how you handle your error handling Robert ? I'm considering moving my class error handling to pear but then it'll need a custom class or function anyway to handle the errors in different ways for different applications and functions, exiting to the screen just isnt friendly for an end user. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Chris W wrote: $Key = xyz; lots more code here. if($key == xyz){ Notice: Undefined variable: key in \path\to\test.php on line X do this stuff; } lots more code here; If the language forces variable deceleration you simply get an error variable $key not defined on line x then there is no searching no wondering, you immediately know you made a typo, and you know exactly what line it is on. Like that, you mean? error_reporting(E_ALL) does that for you. Of course you then turn it down when your code goes into production, though. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Wed, 2004-01-21 at 21:37, [EMAIL PROTECTED] wrote: On Wed, 2004-01-21 at 20:19, Chris W wrote: Robert Cummings wrote: May i ask how you handle your error handling Robert ? I'm considering moving my class error handling to pear but then it'll need a custom class or function anyway to handle the errors in different ways for different applications and functions, exiting to the screen just isnt friendly for an end user. Well in my php.ini I set the following: error_reporting = E_ALL Other than that I use the InterJinn framework which defines an error handling service. The error handling service has a specific API, and honours the settings in the php.ini for outputting to screen or to log file. I format the errors when they are to be displayed and the InterJinn framework is running in web mode. The nice thing about the error handling service is that it can be extended or completely replaced by any other handler service following InterJinn's error service API and all Core code will automatically use the newly registered error service -- just like every other service in the framework (database, session, profile, url, form, etc, etc.). Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Ok i found something very interesting , i have a session var setup to check for a groupID which is an integer, if ($_SESSION['groupID']==1) { this was working, then when i changed it to if ($_SESSION['groupID']===1) { per recomendation, it does not now ! i was going through my code and changing things, now i fear it may all break ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Ok i found something very interesting , i have a session var setup to check for a groupID which is an integer, if ($_SESSION['groupID']==1) { this was working, then when i changed it to if ($_SESSION['groupID']===1) { per recomendation, it does not now ! i was going through my code and changing things, now i fear it may all break ? I changed it to if ($_SESSION['groupID']==='1') { and it worked, why was that, is what i did before bad practice ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
[EMAIL PROTECTED] wrote: Ok i found something very interesting , i have a session var setup to check for a groupID which is an integer, if ($_SESSION['groupID']==1) { this was working, then when i changed it to if ($_SESSION['groupID']===1) { per recomendation, it does not now ! i was going through my code and changing things, now i fear it may all break ? I changed it to if ($_SESSION['groupID']==='1') { and it worked, why was that, is what i did before bad practice ? I think $_SESSION['groupID'] here is considered a string, which is going to evaluate to 0 (zero) in comparisons. Hence 0 === 1 will return false. However, when you enclose the 1 in quotes, it becomes a string too for comparison, and will evaluate to 0. Someone correct me if I am mistaken. -- By-Tor.com It's all about the Rush http://www.by-tor.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Odd Code Error.
[EMAIL PROTECTED] wrote: Ok i found something very interesting , i have a session var setup to check for a groupID which is an integer, if ($_SESSION['groupID']==1) { this was working, then when i changed it to if ($_SESSION['groupID']===1) { per recomendation, it does not now ! i was going through my code and changing things, now i fear it may all break ? I changed it to if ($_SESSION['groupID']==='1') { and it worked, why was that, is what i did before bad practice ? I think $_SESSION['groupID'] here is considered a string, which is going to evaluate to 0 (zero) in comparisons. Hence 0 === 1 will return false. However, when you enclose the 1 in quotes, it becomes a string too for comparison, and will evaluate to 0. Someone correct me if I am mistaken. I think the first bit of what you're saying is right, but when '1' is used, then a string comparison between '0' === '1' is done since both are now strings (or am I mistaken?). Martin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Odd Code Error.
On Thu, 2004-01-22 at 00:52, Martin Towell wrote: [EMAIL PROTECTED] wrote: Ok i found something very interesting , i have a session var setup to check for a groupID which is an integer, if ($_SESSION['groupID']==1) { this was working, then when i changed it to if ($_SESSION['groupID']===1) { per recomendation, it does not now ! i was going through my code and changing things, now i fear it may all break ? I changed it to if ($_SESSION['groupID']==='1') { and it worked, why was that, is what i did before bad practice ? I think $_SESSION['groupID'] here is considered a string, which is going to evaluate to 0 (zero) in comparisons. Hence 0 === 1 will return false. However, when you enclose the 1 in quotes, it becomes a string too for comparison, and will evaluate to 0. Someone correct me if I am mistaken. I think the first bit of what you're saying is right, but when '1' is used, then a string comparison between '0' === '1' is done since both are now strings (or am I mistaken?). You are wrong :) His test for $_SESSION['groupID']==1 succeeds because someone probably set the session value of 'groupID' to the string '1'. Now that he is doing a comparison which includes type (===) it fails because a string is not equal to an integer. This is why it succeeds when he changes the test to '1'. What he had before $_SESSION['groupID']==1 is perfectly fine as long as the group id is not meant to be a string. For instance '1abcd' == 1, evaluates to true :) But chances are the app only accepts numerical groupIDs and so the loose check would be safe. HTH, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Wednesday 21 January 2004 06:55, Jonathan Pitcher wrote: if ($EA == NFH) When comparing strings either use '===' or one of the string comparison functions. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* The most difficult thing in the world is to know how to do a thing and to watch someone else doing it wrong, without commenting. -- T.H. White */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Odd Code Error.
I've come across this recently too. I think what's happening is that PHP is converting NFH to an integer, which would be zero in this case. Thus zero == zero is true... Try doing type checking too: if ($EA === NFH) Martin I am so frustrated at the moment with coding. I had an odd error with some PHP coding. And in the process I came across this error. Please try this out!! Because for me on my machine it does the first part of the if statement. I can make a work around in my code to fix the error. BUT I would rather know why this is evaluating incorrectly. ?PHP $EA = 0; if ($EA == NFH) { echo brEA is Equal To NFH see $EAbr; } else { echo brEA is NOT EQUAL to NFH see $EAbr; } ? Thanks in advance. Jonathan Pitcher -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Odd Code Error.
i've never had a problem doing if ($var==test) ?? I've come across this recently too. I think what's happening is that PHP is converting NFH to an integer, which would be zero in this case. Thus zero == zero is true... Try doing type checking too: if ($EA === NFH) Martin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Tue, 2004-01-20 at 15:55, Jonathan Pitcher wrote: I am so frustrated at the moment with coding. I had an odd error with some PHP coding. And in the process I came across this error. Please try this out!! Because for me on my machine it does the first part of the if statement. I can make a work around in my code to fix the error. BUT I would rather know why this is evaluating incorrectly. ?PHP $EA = 0; if ($EA == NFH) { echo brEA is Equal To NFH see $EAbr; } else { echo brEA is NOT EQUAL to NFH see $EAbr; } ? Interesting. I wouldn't have expected that. However, that is how it should work. Check out table K-2 on this page: http://us4.php.net/manual/en/types.comparisons.php '==' is a loose comparison. You can use '===' instead which will give you the results you are looking for. if ($EA === NFH) - Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Interesting. I wouldn't have expected that. However, that is how it should work. Check out table K-2 on this page: http://us4.php.net/manual/en/types.comparisons.php '==' is a loose comparison. You can use '===' instead which will give you the results you are looking for. if ($EA === NFH) Hmm after all this time, are you saying its best to use === instead of == in any application ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Tue, 2004-01-20 at 16:13, [EMAIL PROTECTED] wrote: Interesting. I wouldn't have expected that. However, that is how it should work. Check out table K-2 on this page: http://us4.php.net/manual/en/types.comparisons.php '==' is a loose comparison. You can use '===' instead which will give you the results you are looking for. if ($EA === NFH) Hmm after all this time, are you saying its best to use === instead of == in any application ? I would recommend using it whenever you can. It may require a few extra lines of code here and there, but you know for sure what 'type' you are dealing with. This is especially good when dealing with user input. - Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Hmm after all this time, are you saying its best to use === instead of == in any application ? I would recommend using it whenever you can. It may require a few extra lines of code here and there, but you know for sure what 'type' you are dealing with. This is especially good when dealing with user input. Ok i might start, i learn something new even after 4 years. I use this for comparisons too strcmp($this-password, $this-password2) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
Hi, Wednesday, January 21, 2004, 8:55:01 AM, you wrote: JP I am so frustrated at the moment with coding. I had an odd error with JP some PHP coding. And in the process I came across this error. JP Please try this out!! Because for me on my machine it does the first JP part of the if statement. I can make a work around in my code to fix JP the error. BUT I would rather know why this is evaluating incorrectly. JP ?PHP JP $EA = 0; JP if ($EA == NFH) JP { JP echo brEA is Equal To NFH see $EAbr; JP } JP else JP { JP echo brEA is NOT EQUAL to NFH see $EAbr; JP } ? JP Thanks in advance. JP Jonathan Pitcher Try swapping positions if(NFH == $EA) -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
yeah its strange, $test is equal to $test2, $test is equal to $test3 but $test2 is not equal to $test3, $test = string; $test2 = true; $test3 = 0; if($test == $test2){ echo hi; } somone tell me im wrong please? else thats seriously weird -- Luke [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] i've never had a problem doing if ($var==test) ?? I've come across this recently too. I think what's happening is that PHP is converting NFH to an integer, which would be zero in this case. Thus zero == zero is true... Try doing type checking too: if ($EA === NFH) Martin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Tue, 2004-01-20 at 22:39, Luke wrote: yeah its strange, $test is equal to $test2, $test is equal to $test3 but $test2 is not equal to $test3, $test = string; $test2 = true; $test3 = 0; if($test == $test2){ echo hi; } Looks fine to me... $test == $test2: $test is not an empty string and thus evaluates to true. $test == $test3: $test is converted to an integer to compare against $test3. Conversion of $test to integer results in 0, which happens to be equal to $test3 You should read the section on type juggling. http://ca2.php.net/language.types.type-juggling Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
ok, i read the section, but even so if $a == $b and $a == $c then $b should be equal to $c but php is saying otherwise? this sounds confusing i want to try n get my head round it a string equals a integer of zero, and a string equals true, but the reason the bool doesnt equal the int is because when the string and int are compared, the string is zero (because it has no numerical value)? did that make sense? am i right? -- Luke Robert Cummings [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Tue, 2004-01-20 at 22:39, Luke wrote: yeah its strange, $test is equal to $test2, $test is equal to $test3 but $test2 is not equal to $test3, $test = string; $test2 = true; $test3 = 0; if($test == $test2){ echo hi; } Looks fine to me... $test == $test2: $test is not an empty string and thus evaluates to true. $test == $test3: $test is converted to an integer to compare against $test3. Conversion of $test to integer results in 0, which happens to be equal to $test3 You should read the section on type juggling. http://ca2.php.net/language.types.type-juggling Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Tue, 2004-01-20 at 23:00, Luke wrote: ok, i read the section, but even so if $a == $b and $a == $c then $b should be equal to $c No this is not true, since types conversions can be different between a and b, a and c or between b and c. If you want the above logic to be true then you must use === for equality based on type also. but php is saying otherwise? PHPis following the rules of type conversion which are not necessarily commutative (hope that's the right term). this sounds confusing i want to try n get my head round it a string equals a integer of zero, A non-numeric string equals 0 by the rules of string to integer type conversion (that's not entirely true since I think the first numerical portion is the part converted as per atoi()). and a string equals true, but the reason A non empty string equals true according to the rules of string to boolean type conversion, unless (I think) the string is '0'. the bool doesnt equal the int is because when the string and int are compared, the string is zero (because it has no numerical value)? You are talking about bool, string and int with respect to a bool-int equality check. This is confusing, only two values can be compared. From what hat did you pull the string? did that make sense? am i right? You didn't make sense :) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
i think i got it now :) i wont bother trying to explain my understanding, i think that will confuse the matter hehe but i think mainly the usual rule, that if an apple and a banana are fruit, and a pear is the same as a banana, then a pear must be fruit too (AND I think im fruit loops) bah im goin bananas, its time to go to band practise anyways, -- Luke Robert Cummings [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Tue, 2004-01-20 at 23:00, Luke wrote: ok, i read the section, but even so if $a == $b and $a == $c then $b should be equal to $c No this is not true, since types conversions can be different between a and b, a and c or between b and c. If you want the above logic to be true then you must use === for equality based on type also. but php is saying otherwise? PHPis following the rules of type conversion which are not necessarily commutative (hope that's the right term). this sounds confusing i want to try n get my head round it a string equals a integer of zero, A non-numeric string equals 0 by the rules of string to integer type conversion (that's not entirely true since I think the first numerical portion is the part converted as per atoi()). and a string equals true, but the reason A non empty string equals true according to the rules of string to boolean type conversion, unless (I think) the string is '0'. the bool doesnt equal the int is because when the string and int are compared, the string is zero (because it has no numerical value)? You are talking about bool, string and int with respect to a bool-int equality check. This is confusing, only two values can be compared. From what hat did you pull the string? did that make sense? am i right? You didn't make sense :) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd Code Error.
On Wednesday 21 January 2004 12:27, Luke wrote: but i think mainly the usual rule, that if an apple and a banana are fruit, and a pear is the same as a banana, then a pear must be fruit too (AND I think im fruit loops) bah im goin bananas, its time to go to band practise anyways, All non-GM oranges are orange == TRUE All objects that are orange are oranges == FALSE -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* Watch all-night Donna Reed reruns until your mind resembles oatmeal. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php