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]
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?
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.
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…
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.
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.".";
?>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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.