Search code examples
phphtmlcodeignitercodeigniter-4

codeigniter 4 is returning 1 after include, include_once, require, require_once


When i'm using the above mentioned (include, include_once, require, require_once) methods i'm getting the 1 written at the end of every content where i've used it.

Have a look:

PHP Code:
<!--Footer Section Starts Here-->
    <?= require_once("elements/footer.php"); ?>
<!--Footer Section Ends Here-->

<!--Bottom CSS and JS Starts Here-->
    <?= include_once("elements/bottom_css_and_js.php"); ?>
<!--Bottom CSS and JS Ends Here-->


Html Output
<div>
    <div>
        <p class="copyright">Copyright &copy;<script>document.write(new Date().getFullYear());</script> All rights reserved.</p>
    </div>
    <div>
        <p class="copyright">This template is made with <i class="ion-ios-heart text-red" aria-hidden="true"></i> by <a href="http://localhost:8080/codeigniter4">Localhost</a></p>
    </div>
</div>1

<!--Bottom CSS and JS Starts Here-->
    <script src="http://localhost:8080/codeigniter4/public/assets/js/jquery.min.js"></script>
    <script src="http://localhost:8080/codeigniter4/public/assets/js/popper.js"></script>
    <script src="http://localhost:8080/codeigniter4/public/assets/bootstrap-footer-19/js/bootstrap.min.js"></script>
    <script src="http://localhost:8080/codeigniter4/public/assets/js/main.js"></script>
  </body>
</html>1    
<!--Bottom CSS and JS Ends Here-->

Solution

  • This is to expand on @sergiy T's comment(answer) and the OP's comment, "Nope it's not working..."

    The current code is

    <!--Footer Section Starts Here-->
        <?= require_once("elements/footer.php"); ?>
    <!--Footer Section Ends Here-->
    
    <!--Bottom CSS and JS Starts Here-->
        <?= include_once("elements/bottom_css_and_js.php"); ?>
    <!--Bottom CSS and JS Ends Here-->
    

    Where you are echoing the result of the calls to require_once and include_once etc due to using <?= which is the short code form for <?php echo. So you will see the code being "included" and also the result of the function call which is 1.

    So you need to remove the echo's as performing a require or include puts the code inline to be executed/displayed.

    So you need to change it to...

    <!--Footer Section Starts Here-->
        <?php require_once("elements/footer.php"); ?>
    <!--Footer Section Ends Here-->
    
    <!--Bottom CSS and JS Starts Here-->
        <?php include_once("elements/bottom_css_and_js.php"); ?>
    <!--Bottom CSS and JS Ends Here-->
    

    As always its advisable to read what these functions do in php.net