I would like to call a PHP page from a PHP page, and return the output of the page I called as a single variable.
require
would not suit my purposes as I need the output stored as a variable for later use, rather than outputting it immediately.
IE:
page1.php
<?php
echo 'Hello World!<br>';
$output = call_page('page2.php');
echo 'Go for it!<br>';
echo $output;
?>
page2.php
<?php
echo "Is this real life?<br>";
?>
Would output:
Hello World!
Go for it!
Is this real life?
Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.
PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters: <html> <body>
Echo In PHPIn order to output one or more strings, we can use echo statement. Anything that can be displayed to the browser using echo statement, such as string, numbers, variables values, the results of expressions, etc. It is required to use parenthesis if you want to use more than one parameter.
To clear up some confusion when using file_get_contents()
, note that:
Using with the full url will yeild the html output of the page:
$contents = file_get_contents('http://www.example-domain.com/example.php');
While using it with file path will get you the source code of the page:
$contents = file_get_contents('./example.php');
require
is actually exactly what you want (in combination with output buffering):
ob_start();
require 'page2.php';
$output = ob_get_clean();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With