how to assign a repeated variable into different variables using php?

  defined, get, php, variables

I have a file with a variable name $myCODE with some values. The current code prints $myCODE and I have assigned different values to it after that which is being printed.
Now I want to read the file perhaps using get_defined_vars() and want to assign first instance of the code as $myCODE1 and the 2nd instance to $myCODE2
I have the following code

code.php file

<?php
$myCODE="Hello";
print $myCODE;//prints Hello
$myCODE="How are you?";
print $myCODE;//prints How are you?
?>

Test.php file

<?php
ob_start();//
include "code.php";
$myCODE_result= array_key_exists('myCODE', get_defined_vars());
if($myCODE_result){
    $myCODE1 = $myCODE;
}
ob_end_clean();
print $myCODE1;// this prints the contents

I reads the variable LAST instance and prints it but NOT the first instance of $myCODE.

Can you help? Please offer another solution. I have over 300 files to bread and extract the contents of $myCODE variable.

Source: Ask PHP

LEAVE A COMMENT