subesc Posted May 29, 2009 Share Posted May 29, 2009 Hey everyone, I'm studying for my Zend PHP 5 Certification Exam and I came across a question that I was hoping I could get an explanation for. The question lists a few choices and asks which of the choices are valid PHP variables. The options are: [*]@$foo [*]&$variable [*]${0x0} [*]$variable [*]$0x0 I know what the answers are, but I don't know why ${0x0} can be a variable. Doesn't this evaluate to 0? If so, then why would it allow me to create a variable that starts with a number? Is the only way that I can call it using the syntax ${0x0} or is there something else I'm missing? Simply curious. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/160099-creating-variables/ Share on other sites More sharing options...
BobcatM Posted May 29, 2009 Share Posted May 29, 2009 Don't quote me, and I am sure I will get corrected, but I do not think ${0x0} is a valid variable. Must start with a letter or underscore. Quote Link to comment https://forums.phpfreaks.com/topic/160099-creating-variables/#findComment-844673 Share on other sites More sharing options...
subesc Posted May 29, 2009 Author Share Posted May 29, 2009 I created a script using it and received no errors. Quote Link to comment https://forums.phpfreaks.com/topic/160099-creating-variables/#findComment-844675 Share on other sites More sharing options...
BobcatM Posted May 29, 2009 Share Posted May 29, 2009 Well after trying it myself, did a little searching, I found this. http://cowburn.info/2008/01/12/php-vars-curly-braces/ Should answer your question. Quote Link to comment https://forums.phpfreaks.com/topic/160099-creating-variables/#findComment-844678 Share on other sites More sharing options...
salathe Posted May 31, 2009 Share Posted May 31, 2009 Is the only way that I can call it using the syntax ${0x0} or is there something else I'm missing? Other ways to get at the same variable would include: ${0}, ${00}, ${11 - 6 - 5}, ${substr('120', -1)} (ie, anything that results eventually in string value of "0"). You can't go via the $GLOBALS array as the key ($GLOBALS["0"]) is cast as an integer and $GLOBALS[0] does not exist and of course you cannot use $0 as that is an invalid variable name and will throw a parse error. Well after trying it myself, did a little searching, I found this... Good to see my blog getting a search engine visitor. Quote Link to comment https://forums.phpfreaks.com/topic/160099-creating-variables/#findComment-846243 Share on other sites More sharing options...
mise_me_fein Posted May 31, 2010 Share Posted May 31, 2010 Is &$variable the one that isn't a variable? Why is @$foo a variable? Quote Link to comment https://forums.phpfreaks.com/topic/160099-creating-variables/#findComment-1065910 Share on other sites More sharing options...
salathe Posted June 1, 2010 Share Posted June 1, 2010 Number 5 is invalid, the others are OK. &$variable is just a normal variable, using the reference operator to get a reference for it. @$foo is again a normal variable just with the error control operator in front. Quote Link to comment https://forums.phpfreaks.com/topic/160099-creating-variables/#findComment-1066154 Share on other sites More sharing options...
mise_me_fein Posted June 1, 2010 Share Posted June 1, 2010 Thanks...I couldn't be sure on which was what. Quote Link to comment https://forums.phpfreaks.com/topic/160099-creating-variables/#findComment-1066356 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.