Hi peeps

I wonder if someone can tell me about having includes inside of includes.

Basically I have a $code_block =. "some small content"

but inside this small content, I need to refer to another include, something like this:

"page.php"

if (...){
    $display_block = "text text";
}else{
    include('../code_block.php');
    $display_block = $code_block;
}

then
code_block.php

html html...
but here i need an include[/COLOR]
 html html... 
and here i need an include 
 html html... 

So to recap I need to call an include inside an include?

Dani AI

Generated

Nested includes are perfectly fine — PHP simply evaluates the included file where the include happens. That means an included file runs in the same scope as the include statement (so includes inside functions run in the function scope). If an included file needs to provide a chunk of HTML back to the caller, two robust patterns are safer than relying on fragile relative paths or global variables.

Use absolute/anchored paths so nested includes don't break when the calling script lives in a different folder. The magic constant __DIR__ is handy for composing reliable paths; also prefer require_once/include_once for files that must not be pulled in more than once. See the manual for include behavior and the __DIR__ constant for details:

If you want an included file to return a string instead of setting a variable, have the file return that string and capture it at the include site. Another common approach for templates is output buffering: include a template that echoes HTML, capture the output, and then append or reuse that captured HTML where needed. Example patterns:

$snippet = include __DIR__ . '/partials/snippet.php'; // snippet.php ends with: return '...html...';
ob_start();
include __DIR__ . '/templates/menu.php';
$menuHtml = ob_get_clean();

Troubleshooting tips: turn on error reporting while you debug, check file permissions, use realpath() when in doubt about paths, and watch variable collisions when many files define similarly named symbols. Good instincts, — and was right to flag folder placement. Using __DIR__ (or absolute paths), include_once/require_once, and the return or buffering patterns will make nested includes predictable and maintainable.

Recommended Answers

All 2 Replies

So I believe I have figured this out....

for the html I wish to include in the include
I set it to a

$var (.)= "<div's ... </div>" called bit_with_var.php

then the important bit is call it here

if (...){
    $display_block = "text text";
}else{
    include('../bit_with_var.php');
    include('../code_block.php');
    $display_block = $code_block; <----------------($... = $var will be inside this)
}

then I can use it here

$display_block = "<div seperate> (an inner div ends in last disp block)
";

$display_block .= $var; (a menu)

$display_block .= "<div><div>
</div></div></div>";

Little bit of a chicken and the egg, all very confusing but hopefully it might help someone and I know I sure wont forget this. I think the key to this is that the:
(1) include content is set to a $var in its own .php,
(2) then the include .php is called in the main php page,
(2) but the echo or $disp_block .= $var is in the intermediary block.

....just contact me if you find this - if I can help more I will

most importantly I have done this with 0 error clean validating html

1) If code_block.php is in parent folder then only use include('../code_block.php') if both files are at same folder then use include('code_block.php')
2) You need to create $code_block variable in code_block.php in order to use it in page.php

<? $code_block = 'html html...
    but here i need an include
    html html...
    and here i need an include
    html html...';
    ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.