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

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




akohanAsked:
Who is Participating?
 
iskumtumtumCommented:
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;
}
?>
0
 
b0lsc0ttIT ManagerCommented:
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
0
 
akohanAuthor 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
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
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.

bol

bol
0
 
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.

thanks,
ak


0
 
phpintheusaCommented:
Have you tried enabling error reporting and setting it to 2047 ? Also, post some code for best answers.
0
 
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.

bol
0
 
gobanCommented:
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

0
 
gobanCommented:
Please ignore the comment on line 6, that was supposed to have been deleted, the rest of the example should be ok.
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.