ckosloff wrote:PS. This is my first post in this forum, so go easy on me, OK?
rubs paws together Mmm... fresh meat. :evilgrin:
.. Just kidding. Welcome to PHPBuilder!
ckosloff wrote:I have seen in many scripts that after double equals == the author enters single quotes with a numeric value '1'
If the value in question is truly numeric and not a string (e.g. all POST/GET data is a string - even if the value is the digit 1), then I would say that author is getting a little too string-happy. Then again, 90% of the time this won't cause too many problems - more about that below.
ckosloff wrote:My understanding is that double or single quotes denote strings
That is correct. It's the same way with SQL queries, too, which I tend to see mixed up far more often than in PHP scripts.
For example, any numeric column in SQL should be given numeric data, thus a query like "UPDATE users SET active='1'" isn't 100% right, IMHO (though again, 90% of the time this won't be a problem).
ckosloff wrote:On the other hand, it could be that single quotes are always mandated after double equals, not sure about that.
Definitely false. The loose comparison operator (double equal sign) can compare two values/entities of any data type - doesn't have to be a string (thus, you don't have to use quotes just because you're using the comparison operator).
Now, about that "90% of the time this won't be a problem" bit above. For starters, note that statistical figures like that one are generated on-the-spot approximately 72.6% of the time.
Also, note that PHP is a very loosely-typed language. When declaring a variable, you don't (read: can't) explicitly declare it's type. You can compare variable types, but only if you use the strict comparison operator - the triple equal sign. That's why things like the following are true:
1 == '1'
TRUE == 1
12345 == TRUE
0 == FALSE
If you changed the operators above to the triple equal sign, however, all of the statements would then be false.
SQL servers (this applies at least to MySQL - most likely other DBMS's as well) can do similar type-casting to make the data fit. That's why using a quoted numeric value will/should result in the correct data being placed into a numeric column.
Some people take this for granted and think this is all nonsense, though, and I can't help but think that they don't have any real experience with lower-level programming, handling raw binary data, etc. etc. For example, '1' consists of the byte 0x31, while 1 consists of the byte 0x01.
And this marks the end of my long-winded tangent/explanation. :p
EDIT:
jkurrle wrote:Also, putting a number within single quotes can change it from a numeric value to a string value, thereby turning it into a value of 0. This catches people sometimes, making them wonder why their formulas are off....
Can you show us a short example of what you're talking about?
As I said/hinted at above, PHP is very good at type-casting data into whatever form it needs it to be in. Example:
$var = "3.14" + "2.7";
var_dump($var);
Your statement above suggests that the result might be 0, but it's actually: float(5.84).