We help IT Professionals succeed at work.

How to pass a value from a function in one page to another page?

akohan
akohan asked
on
Hello,

I have a function in page2.php which returns a number. How can I return this value in page1.php?

it returns an interger. I have included it in page1.php as include_once('page2.php');
but doesn't work.

any comments?

Thanks,
ak




Comment
Watch Question

b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
akohan,

What is the relation of page2 to page1?  Did page2 run the function, get the integer and you want it (the value or variable) available to page1 or do you just want to run the function with something in page1?

Let me know if you have any questions or need more information.

b0lsc0tt

Author

Commented:

Page2.php has nothing but a function as func() which returns an integer. The caller must be in Page1.php

Any advice?

Thanks,
ak
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
What problem do you have using it as an include then?  You shouldn't even need include_once unless page1 may be used as an include.  Look for an error and let us know if you get one.  Make sure you have some error reporting on.

If page2.php is just that function then there is no way it can pass the integer.  Including the file is what you need to do.  If you still have a problem then let us see some code.

bol

bol

Author

Commented:

Maybe I didn't use a right term. What I mean is returning a value from a  function in page2.php
and page1.php just calls the function .

I'm looking at the function maybe there is something wrong in there.

thanks,
ak


Have you tried enabling error reporting and setting it to 2047 ? Also, post some code for best answers.
could you post your code please?  both the function call in page1 and the function definition in page2.

Your calling page should be written like so:
<?php
include "functions.php"; // this file contains your function definition
myvar = myfunction("true"); // call the function that is found in the included file
echo(myvar);  // this will display the returned result on your web page
?>

The functions file should be written like so:
<?php //remember to include the php tags
function myfunction(input_value) {
  if (input_value == "true") {
    return 1;
  }
  return 0;
}
?>
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
I think I understood you correctly; thanks for restating it just to be sure though.  The problem is most likely in the function, the include or the way you are calling the function.  As all 3 of us have asked; please post the code if you don't see the problem.  If the code is long or sensitive then making sure you have error reporting, as 2 of us have said, may give you the key to the problem.

bol
CERTIFIED EXPERT
Commented:
Make sure your code follows this working example:
<html>
<body>
<?php
// Page1.php
require("Page2.php");
//echo square(4); // outputs '16'.
$sendvalue = "foo";
echo $sendvalue . "<br>";
$returnvalue = yourfunction($sendvalue);
echo $returnvalue;
?>
</body>
</html>
 
 
<?php
// Page2.php
function yourfunction($sendvalue)
{
   //your function code goes here
   $example = $sendvalue . "bar";
   return $example;
}
?>

Open in new window

CERTIFIED EXPERT

Commented:
Please ignore the comment on line 6, that was supposed to have been deleted, the rest of the example should be ok.

Author

Commented:

Sorry for being late. No, the code is a simple code and it uses some Facebook functions nothting special about it that I won't post but so far I have got the point.

Thanks to all.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.