ansharma Posted February 15, 2011 Share Posted February 15, 2011 Hi, Can this PHP code be valid: $4bears = $bears->getFirst4();.. Can you please explain this ..Since the Answer i got is "Yes". Thanks Quote Link to comment https://forums.phpfreaks.com/topic/227727-can-this-php-code-be-valid-4bears-bears-getfirst4/ Share on other sites More sharing options...
silkfire Posted February 15, 2011 Share Posted February 15, 2011 Yes it's valid. The -> notation specifies that you're using a function, getFirst4(), belonging to a class ($bears is a copy of this class), and that you want to assign the result of that function to your variable $4bears. Quote Link to comment https://forums.phpfreaks.com/topic/227727-can-this-php-code-be-valid-4bears-bears-getfirst4/#findComment-1174469 Share on other sites More sharing options...
trq Posted February 15, 2011 Share Posted February 15, 2011 getFirst4(), belonging to a class ($bears is a copy of this class) $bears is an object, not a class. There is a difference. No it is not valid. Variables cannot begin with a number. See $4bears Quote Link to comment https://forums.phpfreaks.com/topic/227727-can-this-php-code-be-valid-4bears-bears-getfirst4/#findComment-1174471 Share on other sites More sharing options...
ansharma Posted February 15, 2011 Author Share Posted February 15, 2011 is it possible to define a variable name starting with number? like this $4xyz=5; Quote Link to comment https://forums.phpfreaks.com/topic/227727-can-this-php-code-be-valid-4bears-bears-getfirst4/#findComment-1174473 Share on other sites More sharing options...
trq Posted February 15, 2011 Share Posted February 15, 2011 No, did you not just read my previous post? Quote Link to comment https://forums.phpfreaks.com/topic/227727-can-this-php-code-be-valid-4bears-bears-getfirst4/#findComment-1174475 Share on other sites More sharing options...
ignace Posted February 15, 2011 Share Posted February 15, 2011 is it possible to define a variable name starting with number? like this $4xyz=5; Yes but only in the form: ${4xyz} = 5; You also need to access it in the same way: print ${4xyz}; As a side note: You should avoid writing variables like that. Quote Link to comment https://forums.phpfreaks.com/topic/227727-can-this-php-code-be-valid-4bears-bears-getfirst4/#findComment-1174479 Share on other sites More sharing options...
salathe Posted February 15, 2011 Share Posted February 15, 2011 is it possible to define a variable name starting with number? like this $4xyz=5; It's a valid variable name, in the sense that a variable can exist called "4xyz". However, it is not a valid label (the part that comes after $ to denote a variable). In other words, you cannot define a variable name like $4xyz but you can define it using the $GLOBALS superglobal variable ($GLOBALS['4xyz']) or a variable-variable ${'4xyz'}. But... don't. As a side note: You should avoid writing variables like that. Mostly because it will generate a parse error. Quote Link to comment https://forums.phpfreaks.com/topic/227727-can-this-php-code-be-valid-4bears-bears-getfirst4/#findComment-1174482 Share on other sites More sharing options...
ignace Posted February 15, 2011 Share Posted February 15, 2011 This question raised my curiosity as to Why can't variable names start with numbers? Apparently because: It's likely a decision that came for a few reasons, when you're parsing the token you only have to look at the first character to determine if it's an identifier or literal and then send it to the correct function for processing. So that's a performance optimization. Use of a digit to begin a variable name makes error checking during compilation or interpertation a lot more complicated. Because if it did, it would be called COBOL. Quote Link to comment https://forums.phpfreaks.com/topic/227727-can-this-php-code-be-valid-4bears-bears-getfirst4/#findComment-1174491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.