Gary from Leatherhead emailed to say that he's an ASP programmer trying to learn PHP, and has run into a couple of problems with the equals sign.
'First, I'm confused by the .= operator, and how it differs from = ?'
Well Gary .= is a shortcut, whose general syntax will be familiar to C programmers, but may seem alien to someone raised on a diet of Visual Basic. How many times have you added something to the end of a string in VB by using code such as:
myStr = myStr & " whassup?"
and thought how clumsy the syntax was? Well PHP provides the more elegant:
$myStr .= " whassup?"
so it's much like the += and similar operators available in C and other languages.
Gary's second question is 'What on earth is === all about? I did a bit of C at college so I understand using a double equals sign to test for equality, but what does a triple equals sign mean?'
You have to remember that a variable has both a value and a type, and in most languages when two variables are compared the language run-time will convert both of them to comparable types and then test the results of the conversion for equality. With ===, however, the expression will only return true if the variables are absolutely identical; that is, if they both have the same value and type. So while both:
$tst = (6==6.0) ? ('TRUE') : ('FALSE') ; $tst = (6=='6') ? ('TRUE') : ('FALSE'') ; will return 'TRUE', these two: $tst = (6===6.0) ? ('TRUE') : ('FALSE') ; $tst = (6==='6') ? ('TRUE') : ('FALSE') ; will both return 'FALSE'.
For those raised on VB or other weakly typed languages, it can seem quite strange to think that two variables may be equal but not identical. Don't worry though Gary, we've never found a use for === here, and if you ever think of a situation where it might be useful, please let us know.
All articles Copyright CST Group Limited © 1997-2021
…provided us with extremely dedicated support over the years, Very responsive & knowledgeable.
— Khyati Patel, Malibu-Kahlúa International