Home » Php » php – How to return value to index page from an included script page?

php – How to return value to index page from an included script page?

Posted by: admin February 25, 2020 Leave a comment

Questions:

index.php

<?php   
$surl = "http://myweb.com/myfolder/include.php";
$me_arr  = array(array('method'=>'x', 'param1'=> 5, 'param2'=> 3),array('method'=>'y', 'param1'=> 2, 'param2'=> 2));
$json_data = json_encode($me_arr);
$strResult = include($surl."?me_Name=".$json_data);

echo $strResult; 
?>

include.php

<?php
$method = (isset($_GET['me_Name']))?json_decode($_GET['me_Name'],true):'array()';

if(isset($method))
{
foreach (($method) as $extMethod)
{
$func = $extMethod['method'];
$param1 = $extMethod['param1'];
$param2 = $extMethod['param2'];

$func($param1,$param2,$z);
}
}   
function x($x, $y, $z)
{
$z=$y+$x;
echo $z;
}         
function y($x, $y, $z)
{
$z=$y+$x;
echo $z;
}
?>

This code works perfectly fine but I want to return the value of z to index.php not echo in include page . But return $z; doesn’t work here, just give me the output 1 in index.php. Any way to solve this problem ?? Thanks in advance.

How to&Answers:

In you index.php page after inclusion you can use $z variable as like

<?php   
$surl = "http://myweb.com/myfolder/include.php";
$me_arr  = array(array('method'=>'x', 'param1'=> 5, 'param2'=> 3),array('method'=>'y', 'param1'=> 2, 'param2'=> 2));
$json_data = json_encode($methodarr);
$strResult = include($surl."?me_Name=".$json_data);

echo $z; // Using variable from include page
?>