I posed anther thread about variable variables. I person responded but his code is not working. I asked him to clarify and he never responded. So I'm posting a follow up question.
I have the below code. When I put the dynamic $ct in the code it fails and says undefined variable. But when I remove the $ct, it works fine. Would someone clarify how I can add the $ct variable to the end of my variable so that I can have dynamic variables?
$ct = 1;
${"var".$ct} = "String";
echo $var1;
The above fails.
$ct = 1;
${"var"} = "String";
echo $var1;
This works fine.
So something is not working right with adding the dynamic $ct.
He said it should work fine. I have also tried $$ using a variable then setting that variable and still fails the same.
/////////Edit
so my code looks like this:
$ct=1;
foreach ($Emp1 as $e){
if ($e['Status']!="Primary"){
${"B1_E1_Info$ct"} = $e['Employer_Name']." ".$e['Address_Unit']." ".$e['City']." ".$e['State']." ".$e['Zip'];
$B1_E1_SE.$ct = (strpos($e['Self_Employed'], "| Y") !== false) ? true : false;
$B1_E1_YrsOn.$ct = $e['Years_on_Job'];
$B1_E1_YrsIn.$ct = $e['Years_in_Line_of_Work'];
$B1_E1_Pos.$ct = $e['Position_Title'];
$B1_E1_Ph.$ct = $e['Business_Phone'];
}
$ct= $ct+1;
}
echo $B1_E1_Info1;
When called iside the loop it doesn not work. If i take it out of the loop it works. Likewise if i change the code inside the loop to not inlcude the $ct it works as well. The $ct just doesn't work in the loop.
$var1!!Notice: Undefined variable: var1${"var$ct"} = "String";