0

My question is related to php include prints 1. In https://github.com/jaimemontoya/jaimemontoya.com/blob/dev/page.php I'm using this:

public function Display()
{
   echo "<!DOCTYPE html>\n\t<head>\n";
   $this -> DisplayTitle();
   $this -> DisplayFavicon();
   $this -> DisplayMetaKeywords();
   $this -> DisplayMetaDescription();
   $this -> DisplayMetaViewport();
   $this -> DisplayStyles();
   echo "\t</head>\n\t<body>\n";
   $this -> DisplayHeader();
   echo "\t\t<div class=\"container\">\n";
   echo $this->content;
   echo "\t\t</div>\n";
   $this -> DisplayFooter();
   echo "\t</body>\n</html>\n";
}

The problem happens in echo $this->content;. In https://github.com/jaimemontoya/jaimemontoya.com/blob/dev/index.php I'm using this:

<?php
  require("page.php");
  $index = new Page();
  $index->content .=
  require("scala-programming-projects/scala-programming-projects-book-info.php");
  require("success-habits-dummies-zeller/success-habits-dummies-zeller-book-info.php");
  .....
  .....
  .....
  $index->Display();
?>

The problem is the '1' that is printed because the include() method, after successfully including the desired file, returns a TRUE value that when echo'ed out is '1'.

enter image description here

Do you have specific suggestions to refactor my code to get rid of that '1' or maybe php.ini or some configuration can help in this case? Thank you.

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103
  • 1
    Does this answer your question? [php include prints 1](https://stackoverflow.com/questions/5086695/php-include-prints-1) – Progman Nov 25 '21 at 21:11
  • @Progman No, it doesn't because simply not using `echo` and only using `include()` without the `echo` is not working for me. I need the `echo` to print out the variable where have the string that has concatenated all values that come from different `require()` elements. – Jaime Montoya Nov 25 '21 at 21:22
  • 1
    The problem is you have `$var .= require ...;`, you are adding the return value of `require`, which is usually the boolean value `true`. In a context of strings, this get translated to `"1"`, which gets added to the variable (in your case `$index->content`). This is what the linked question is talking about. – Progman Nov 25 '21 at 21:42

2 Answers2

1

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();
?>

Hery Kurniawan
  • 344
  • 2
  • 8
0
$index->content .=
  require("scala-programming-projects/scala-programming-projects-book-info.php");
  require("success-habits-dummies-zeller/success-habits-dummies-zeller-book-info.php");

Instead of doing this ,you should get the output of the require calls into a variable using output buffering. as an example ,

ob_start();
require('bla.php');
require('blabla.php');
 $index->content .= ob_get_clean();

because require calls directly echoes the output at the place where you call them so there is no meaning in assigning it to a variable, which is your mistake here and require returns 1 which is getting printed as well