We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Strange EE/PHP behavoir

Development and Programming

chealey's avatar
chealey
95 posts
14 years ago
chealey's avatar chealey

I’m trying to get data from EE into a variable and then play with it using PHP, however I find that the only thing I can do with data I grab from EE is output it with PHP. If I try to do anything else, like use it in a conditional statement or perform arithmetic, it fails.

Here is some sample code on a template with PHP enabled on output:

<?php
$temp = "{member_id}";
echo "Temp is equal to: ".$temp.".";
$temp = $temp + $temp;
echo "Temp is now equal to: ".$temp.".";
?>

This is the output I get:

Temp is equal to: 1. Temp is now equal to: 0.

This is what I would expect to get:

Temp is equal to: 1. Temp is now equal to: 2.

Please advise on why this is happening, and any possible workarounds.

[Mod Edit: Moved to the Development and Programming forum]

       
chealey's avatar
chealey
95 posts
14 years ago
chealey's avatar chealey

I’m getting around this issue by moving my PHP to an EE plugin and using that throughout the site. This is less than ideal for some instances, but I’ll manage to put up with it. Regardless, I would really like to understand what’s going on.

After doing some var_dumps, I noticed that dumping the first version of $temp results in string(11), while the second temp results in int(0). It’s almost like PHP thinks the {member_id} is a string, even though EE ninja-replaces it with an integer. Apparently, this means PHP can use it to output just fine, but as soon as it tries to work with it, it sees it’s now the wrong data type and freaks out…

Does that logic make sense? Any workarounds aside from creating a plugin?

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team
After doing some var_dumps, I noticed that dumping the first version of $temp results in string(11), while the second temp results in int(0). It’s almost like PHP thinks the {member_id} is a string, even though EE ninja-replaces it with an integer.

Technically, all of EE’s values parsed through EE tags will be interpreted by PHP as strings. To do math you’d have to throw a type cast in front of the value.

       
chealey's avatar
chealey
95 posts
14 years ago
chealey's avatar chealey

Right, I did that, but I still had issues. Here’s another example:

<?php
$temp = "{member_id}";
echo "Example 1: ".$temp."."; //Outputs 1
$temp = (int)$temp;
echo "Example 2: ".$temp."."; //Outputs 0, even though you would expect 1
?>

See the confusion?

In addition, PHP automatically type casts strings to integers (or at least it tries to) when you perform arithmetic, so that couldn’t have been the problem…

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team
After doing some var_dumps, I noticed that dumping the first version of $temp results in string(11)…

I believe this is indicative of PHP being set to Input mode. Check your template settings and put PHP processing on Output so that EE’s tags are parsed first, then PHP can do it’s thing.

       
chealey's avatar
chealey
95 posts
14 years ago
chealey's avatar chealey

It was definitely set to output as well (although I tried input too, with no luck.) I’m going to dig around in the settings and add-ons for a bit to see if I can find a culprit…

       
the3mus1can's avatar
the3mus1can
426 posts
14 years ago
the3mus1can's avatar the3mus1can

Is this:

<?php
$temp = "{member_id}";
echo "Example 1: ".$temp."."; //Outputs 1
$temp = (int)$temp;
echo "Example 2: ".$temp."."; //Outputs 0, even though you would expect 1
?>

the exact template code?

       
chealey's avatar
chealey
95 posts
14 years ago
chealey's avatar chealey

Yes, I just tried it again with a blank file set to parse php on output, and the results are:

Example 1: 1. Example 2: 0.

Scratching my head here…

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
14 years ago
Pascal Kriete's avatar Pascal Kriete

The member id is a global, they are done dead last.

If you drop in an exit; after your second echo you’ll see what it’s actually trying to evaluate.

To grab the current member id use the session library’s userdata method:

<?php
$temp = $this->EE->session->userdata('member_id');
echo "Temp is equal to: ".$temp.".";
$temp = $temp + $temp;
echo "Temp is now equal to: ".$temp.".";
?>
       
chealey's avatar
chealey
95 posts
14 years ago
chealey's avatar chealey
The member id is a global, they are done dead last. If you drop in an exit; after your second echo you’ll see what it’s actually trying to evaluate. To grab the current member id use the session library’s userdata method:
<?php
$temp = $this->EE->session->userdata('member_id');
echo "Temp is equal to: ".$temp.".";
$temp = $temp + $temp;
echo "Temp is now equal to: ".$temp.".";
?>

I ended up doing it that way and it worked. Thanks for the response.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.