Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include not working as expected

Tags:

php

This has me pretty baffled, so I have a simple include line in my php but it for some reason does not always work right.

Basically the include is loading my main content section...

if $page['view'] is list my page will not load correctly...

the php I have is

include ('lib/views/' . $page['view'] . '.php');

However the page does not load and nothing below this point loads including the footer...

This works as expected

echo 'lib/views/' . $page['view'] . '.php';

My apache2 error log spits out the following error

include(): Failed opening 'lib/views/.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/site/www/lib/includes/main.php on line 3

When I try this, the footer reappears but still same error

$view = 'lib/views/' . $page['view'] . '.php';
include ($view);

This is pretty baffling to me, especiall considering that if $page['view'] is avatar it then works

EDIT 1

As suggested by @philwc I have added this to my code

if (isset($page['view'])) {
        var_dump($page['view']);
}

Which outputs as expected list

like image 979
Chris James Champeau Avatar asked Mar 05 '26 14:03

Chris James Champeau


2 Answers

Try this:

include ("lib/views/{$page['view']}.php");

As you stated it will give you a different error, just mark this correct so that it doesn't sit in limbo :)

like image 189
Darren Avatar answered Mar 08 '26 04:03

Darren


It looks like $page['view'] is not set correctly. Try wrapping it with an isset like

if(isset($page['view'])){
    include ('lib/views/' . $page['view'] . '.php');
}else{
    echo 'Error!';
}
like image 34
philwc Avatar answered Mar 08 '26 04:03

philwc