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


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?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

b0lsc0ttIT ManagerCommented:

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.

akohanAuthor Commented:

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

Any advice?

b0lsc0ttIT ManagerCommented:
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.


Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

akohanAuthor 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.


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:
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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
b0lsc0ttIT ManagerCommented:
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.

Make sure your code follows this working example:
// Page1.php
//echo square(4); // outputs '16'.
$sendvalue = "foo";
echo $sendvalue . "<br>";
$returnvalue = yourfunction($sendvalue);
echo $returnvalue;
// Page2.php
function yourfunction($sendvalue)
   //your function code goes here
   $example = $sendvalue . "bar";
   return $example;

Open in new window

Please ignore the comment on line 6, that was supposed to have been deleted, the rest of the example should be ok.
akohanAuthor 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.