russthebarber Posted August 23, 2012 Share Posted August 23, 2012 I'm having problems with a php variable I am getting from a CMS program (Contao). echo $myPageVar // returns 17 echo gettype($myPageVar); //returns string This seems OK but whenever i convert to an integer I get a value of 0. When I try to use the variable in a condition it also returns false. if ($myPageVar == "17") { //returns false } How do I convert $myPageVar to a useable variable with value = 17? Quote Link to comment https://forums.phpfreaks.com/topic/267469-variable-returns-0/ Share on other sites More sharing options...
spiderwell Posted August 23, 2012 Share Posted August 23, 2012 how are you converting to int? Quote Link to comment https://forums.phpfreaks.com/topic/267469-variable-returns-0/#findComment-1371737 Share on other sites More sharing options...
aliento Posted August 23, 2012 Share Posted August 23, 2012 I'm having problems with a php variable I am getting from a CMS program (Contao). echo $myPageVar // returns 17 echo gettype($myPageVar); //returns string This seems OK but whenever i convert to an integer I get a value of 0. When I try to use the variable in a condition it also returns false. if ($myPageVar == "17") { //returns false } How do I convert $myPageVar to a useable variable with value = 17? if ($myPageVar == 17) { //returns false } Quote Link to comment https://forums.phpfreaks.com/topic/267469-variable-returns-0/#findComment-1371739 Share on other sites More sharing options...
aliento Posted August 23, 2012 Share Posted August 23, 2012 or $myPageVar = (int) $myPageVar; Quote Link to comment https://forums.phpfreaks.com/topic/267469-variable-returns-0/#findComment-1371740 Share on other sites More sharing options...
aliento Posted August 23, 2012 Share Posted August 23, 2012 or or $myPageVar = (int) $myPageVar{0}. $myPageVar{1}; Quote Link to comment https://forums.phpfreaks.com/topic/267469-variable-returns-0/#findComment-1371741 Share on other sites More sharing options...
russthebarber Posted August 23, 2012 Author Share Posted August 23, 2012 Hi, thanks all for the help. I tried various ways of converting to an INT with no success. Now I know the problem. My variable, although it outputs "string" when type tested, is actually an array starting with two curly braces and that is what is confusing php. I've got it sorted now. In case this helps any Contao users trying to write php with contao variables..... Don't do this: $myPageVar = '{{page::id}}';//gives you something php doesn't understand Do this: $myPageVar = $this->replaceInsertTags( '{{page::id}}' ); //for a php friendly variable Quote Link to comment https://forums.phpfreaks.com/topic/267469-variable-returns-0/#findComment-1371742 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.