Jump to content

variable returns 0


russthebarber

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/267469-variable-returns-0/
Share on other sites

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
}

Link to comment
https://forums.phpfreaks.com/topic/267469-variable-returns-0/#findComment-1371739
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/267469-variable-returns-0/#findComment-1371742
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.