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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.