Jump to content

What does "->" mean in PHP?


ilyakar

Recommended Posts

Close. :)In C, the datatypes called struct and union are a lot like objects. Member are normally accessed with dot notation, the way JavaScript does it. So a typical assignment could look like this:my_struct.my_element = 5;Because C is "closer to the metal" than PHP or JavaScript, it also lets you designate variables as pointers: a number that represents a memory address. You can create a pointer like this:my_struct_pointer = &my_struct;The & is the address operator. Prefixed to my_struct, it says "return the memory location of my_struct."To access my_element from my_struct_pointer, we have to use -> instead of the dot:my_struct_pointer->my_element = 5;For anyone who's followed this far and did not know already, you can see how the dot, & and -> symbols have inherited their meanings in JS and PHP from their common ancestor.I'm not sure why PHP chose the dot for the concatenation operator (anyone?) but it's not as weird as it might seem. C itself has no concatenation operator. Strictly speaking, it doesn't even have strings. What we call a string is an array of characters in C, and you need a function (or at least a routine) to concatenate character arrays. Funky, huh?But maybe now it makes sense why, if you have a string like this:s = "hello";s[1] returns "e"

Link to comment
Share on other sites

Yeah, the + operator is much more logical for concatenation. Speaking of C, I downloaded the PHP source code today just to see how they mathematically can find the square root of a number. (I've never figured out how they do it without having tables of squares/square roots) And guess what? I found out that the PHP sqrt() function is just based on the C sqrt function. What a letdown! I was hoping to find some algorithm. I guess I'll just do a search on the internet for it. I actually like the -> better than a period. It is more self explanatory, even if it is more to type.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...