PHP Query

Posted: 1st July 2001 | Filed under: Press Articles, Technical
Author: Paul Ockenden

This article was originally published in PC Pro and is reproduced with permission.

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-2024

Let's Talk

Whether you have a new or existing project, we’d love to hear from you.

Our experienced team thrive on problem solving and working with your business goals in mind.

Get in touch

Cookie preferences

We'd like to know more about the pages you visit on our site to help improve it. If you're happy for us to collect this information please click accept all. Our full Privacy policy can be found here.