php include prints 1
echo include "foo.php"
should be
include 'foo.php';
php include prints '1'
Since you probably want the PHP processed and the output stored in the variable, this will do the trick:
ob_start();
include(SUBSCRIBE_USER_BASE_DIR . '/includes/av_subscribe_form.php');
$include = ob_get_clean();
$html = '<div class="subscribe_user"><span id="question1"><a href="#" id="subscribe_link" data-category="'.$category.'">
Subscribe to comments made on '.$category.'</a></span></div>' . $include;
PHP is printing an extra 1 in the webpage
This should work for you:
(I put a view comments to explain whats going on!)
File Structure:
|
| - index.php
| - navigation.php
|
| - templates
| | - page.php
|
| - views
| - skills.php
| - projects.php
index.php:
<?php
//Error reporting only for testing environment
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
<?php
//To save some code lines i put that in a function
function getContent($path) {
ob_start();
require_once($path);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
$pageData = new stdClass();
$pageData->title = "Thomas Blom Hansen: Portfolio site";
$pageData->content = getContent("navigation.php");
$pageData->css = "<link href='css/layout.css' rel='stylesheet' />";
if( !empty($_GET['page'])) {
$fileToLoad = $_GET['page'];
$pageData->content .= getContent("views/$fileToLoad.php");
} else {
$fileToLoad = "skills";
$pageData->content .= getContent("views/$fileToLoad.php");
}
$page = getContent("templates/page.php");
$page = str_replace('%%$pageData->title%%', $pageData->title, $page);
$page = str_replace('%%$pageData->css%%', $pageData->css, $page);
$page = str_replace('%%$pageData->content%%', $pageData->content, $page);
echo $page;
?>
navigation.php:
<nav>
<a href='index.php?page=skills'>My skills and background</a>
<a href='index.php?page=projects'>Some projects</a>
</nav>
projects.php:
<h1>Projects I have worked on</h1>
<ul>
<li>Ahem, this will soon be updated</li>
</ul>
templates/page.php:
<!DOCTYPE html>
<html>
<head>
<title>%%$pageData->title%%</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8' />
%%$pageData->css%%
</head>
<body>
%%$pageData->content%%
</body>
</html>
views/skills.php:
<h1>Skills and educational background</h1>
<p>Read all about my skills and my formal training</p>
<?php
echo "test"; //Little example to show that php gets parsed and that's also why the file extension still *.php is
?>
Output/ source code is:
<!DOCTYPE html>
<html>
<head>
<title>Thomas Blom Hansen: Portfolio site</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8' />
<link href='css/layout.css' rel='stylesheet' />
</head>
<body>
<nav>
<a href='index.php?page=skills'>My skills and background</a>
<a href='index.php?page=projects'>Some projects</a>
</nav>
<h1>Skills and educational background</h1>
<p>Read all about my skills and my formal training</p>
test
</body>
</html>
Side Note:
Here i used require_once
so that if you have a complicated website files doesn't get multiple times included! (See: http://php.net/manual/en/function.require-once.php)
Also added %%
before and after a php variable in the template so that is less likely that you write a text with this name!
PHP Short Open Tag prints 1
Because it returns true. You need to use include_once
without the short open tag, so like this:
<?php include_once 'includes/footer.php';?>
When you write an open short tag, like this;
<?= include_once 'includes/footer.php';?>
You actually write this:
<?php echo include_once 'includes/footer.php';?>
Which results in "1" on your screen.
Include a php file, but return output as a string instead of printing
You can use php buffers like this:
<?php
ob_start();
include('other.php');
$script = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
EDIT: You can abstract this into a function:
function inlcude2string($file) {
ob_start();
include($file);
$output = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
return $output;
}
$str = inlcude2string('other.php');
Include whole content of a file and echo it
Just do:
include("http://www.mysite.com/script.php");
Or:
echo file_get_contents("http://www.mysite.com/script.php");
Notes:
- This may slow down your page due to network latency or if the other server is slow.
- This requires
allow_url_fopen
to be on for your PHP installation. Some hosts turn it off. - This will not give you the PHP code, it'll give you the HTML/text output.
Concatenating strings in PHP using require() without printing 1 as part of the output
I see that your scala-programming-projects/scala-programming-projects-book-info.php
already concat the string. just remove concatenation on index.php
<?php
require("page.php");
$index = new Page();
require("scala-programming-projects/scala-programming-projects-book-info.php");
require("success-habits-dummies-zeller/success-habits-dummies-zeller-book-info.php");
.....
.....
.....
$index->Display();
?>
php print all filenames in context of calling scripts
You can use the get_included_files
function. It will give you an array containing the names of the files that were included so far.
// a.php:
<?php
include('b.php');
var_dump(get_included_files());
==========
//b.php:
<?php
include('c.php');
==========
//c.php:
<?php
// do nothing
Output will be:
array (size=3)
0 => string 'a.php' (length=5)
1 => string 'b.php' (length=5)
2 => string 'c.php' (length=5)
Note that the function works the same if you will put it inside one of the included files. You can put the function inside
c.php
(and not insidea.php
) and the output will not change.
include inside a variable - includes without print the variable
try file_get_contents
<?php
$var = file_get_contents('file.php');
echo $var;
?>
Related Topics
Custom Laravel Validation Messages
Using Square Brackets in Hidden HTML Input Fields
Gmail Fsockopen(): Ssl Operation Failed Error with Codeigniter and Xampp
Why Are Floating Point Numbers Printed So Differently
Separate Space-Delimited Words in a String
How to Handle Double Quotes in String Before Xpath Evaluation
Class 'App\Http\Controllers\Admin\Auth' Not Found in Laravel 5
Warning: MySQLi_Query() Expects Parameter 1 to Be MySQLi, String Given In
Passing JavaScript Variable to PHP Using Ajax
Store Procedures in PHPmyadmin
How to Check If $_Get Is Empty