Require/Include into Variable

require/include into variable

I've also had this issue once, try something like

<?php
function requireToVar($file){
ob_start();
require($file);
return ob_get_clean();
}
$test=requireToVar($test);
?>

php include into a variable then echo that variable

Use file_get_contents or ob_get_clean, like so:

ob_start();
include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
$eyecatcher = ob_get_clean();

how to pass a variable through the require() or include() function of php?

require() and include() will open the file corresponding to the path/name they receive.

Which means that, with your code, you would have to have a file called diggstyle_code.php?page=1 on your disk. That's obviously not the case, so it fails.

Quoting the Variable scope page of the PHP Manual:

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.

In your case, you don't need to pass the variable. If you have a variable in your current script, it will also exist in the script you include, outside of functions, which have their own scope.

In your main script, you should have:

$page_no = 10;
require 'diggstyle_code.php';

And in diggstyle_code.php:

echo $page_no;
// Or work with $page_no the way you have to

Remember that including/requiring a file is exactly the same as copy-pasting its content at the line it's required.

Undefined variable with include or require

I think you have variables defined in global scope but you try to use it in local scope.

To confirm naming scope issue try to:

require 'accounts.php';
function guestConnection() {
global $host, $database, $guestAccount, $guestPassword;
try {
$conn = new PDO("pgsql:host=$host;dbname=$database", $guestAccount, $guestPassword);
...

This should work if your guestConnection() is in global scope not under any namespace or class.

or do move require out of try to get errors if any (file does not exist?):

function guestConnection() {
require('accounts.php');
try {
$conn = new PDO("pgsql:host=$host;dbname=$database", $guestAccount, $guestPassword);
...

How to call variable inside function while using require or include outside that function in php

The use of include/require is actually not relevant for your question. An include or a require will only "merge" the contents of other files to your code in order to become available.

As others have mentioned you need to use the global keyword. In many other programming languages the global variables are visible inside functions by default. But in PHP you need to explicitly define which variables that should be visible in each function.

Example:

<?php  
$a = "first";
$b = "second";
$c = "third";

function test() {
global $a, $b;
echo $a . " " . $b . " " . $c;
}

test(); // This will output only "first second" since variable $c is not declared as global/visible in the test-function
?>

However, if the variables should be treated as static constants I would recommend you to define them as constants. Constants have a "real" global scope and you do not need to use the global keyword.

<?php  
define("A", "first");
define("B", "second");
define("C", "third");

function test() {
echo A . " " . B . " " . C;
}

test(); // This will output "first second third". You don't need to use the global keyword since you refer to global constants
?>

passing parameters to php include/require construct

There isn't a way to pass parameters to include or require.

However the code that is included joins the program flow at the point where you include it, so it will inherit any variables that are in scope. So for example if you set $myflag=true immediately before the include, your included code will be able to check what $myflag is set to.

That said, I wouldn't suggest using that technique. Far better for your include file to contain functions (or a class) rather than code that gets run straight off. If you've included a file containing functions then you can call your functions with whatever parameters you want at any point in your program. It's much more flexible, and generally a better programming technique.

Hope that helps.

Include file content in the variable

include and require are used to load PHP code into your application.

To get the content of a file and store it in a variable use file_get_contents instead.

Do note that given path to the file is relative to the entry script. This might give unexpected results when your script is included from somewhere else. To counter this, use the __DIR__ magic constant.

Final code would look like:

$response['outputHTML'] = file_get_contents(__DIR__ . '/table-html.php');

If you want to parse PHP however

First of all, you might want to look at a templating engine like Twig, and save yourself some trouble.

If you want to do it yourself, the only thing you need to change in your code is to return a variable at the end of table-html.php. I'd suggest using Heredoc to create readable and easy templating.

Example

<?php
return <<<EOT
<table>
<tr><td>$firstname</td><td>$lastname</td></tr>
</table>
EOT;

How to pass the require_once output to a variable?

Is it possible?

Yes, but you need to do an explicit return in the required file:

//test.php

<? $result = "Hello, world!";
return $result;
?>

//index.php

$test = require_once('test.php'); // Will contain "Hello, world!"

This is rarely useful - check Konrad's output buffer based answer, or adam's file_get_contents one - they are probably better suited to what you want.

Require/Include and Undefined Variables in PHP

The answer had nothing to do with the problem in the question. The code in the question is correct.



Related Topics



Leave a reply



Submit