How do I put a php file inside a div class?

Viewed 373

Hi good day! I don't really know how to post here since its my first time here. Please don't hesitate to teach me the basics. Anyways here is my code.

body{
    margin: 0;
    padding: 0;
    background: url(bg.jpg) no-repeat;
    background-size: cover;
  }
  .box{
    width: 450px;
    background: rgba(0, 0, 0, 0.4);
    padding: 40px;
    text-align: center;
    margin: auto;
    margin-top: 5%;
    color: white;
    font-family: 'Century Gothic',sans-serif;
  }
  .box-img{
    border-radius: 50%;
    width: 200px;
    height: 200px;
  }
  .box h1{
    font-size: 40px;
    letter-spacing: 4px;
    font-weight: 100;
  }
  .box h5{
    font-size: 20px;
    letter-spacing: 3px;
    font-weight: 100;
  }
  .box p{
    text-align: justify;
  }
  ul{
    margin: 0;
    padding: 0;
  }
  .box li{
    display: inline-block;
    margin: 6px;
    list-style: none;
  }
  .box li a{
    color: white;
    text-decoration: none;
    font-size: 60px;
    transition: all ease-in-out 250ms;
  }
  .box li a:hover{
    color: #b9b9b9;
  }
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="https://use.fontawesome.com/d1341f9b7a.js"></script>
    <link rel="stylesheet" href="style.css">
    <title>Personal WebSite</title>
</head>
<body>
<div class="box">
    <img src="profile.jpg" alt="" class="box-img">
    <h1>DarkCode</h1>
    <h5>Web Devloper - Web Designer</h5>
    <?php include("menu.php"); ?>
    <ul>
        <li><a href="#"><i class="fa fa-facebook-square" aria-hidden="true"></i></a></li>
        <li><a href="#"><i class="fa fa-twitter-square" aria-hidden="true"></i></a></li>
        <li><a href="#"><i class="fa fa-google-plus-square" aria-hidden="true"></i></a></li>
    </ul>
</div>
</body>
</html>

This is the php file that I want to include in the box (menu.php)

  <?php

class Fruit{
    public $name;
    public $color;

    function __construct($name, $color){
        $this->name = $name;
        $this->color = $color;
    }
    function get_name(){
        return $this->name;
    }
    function __destruct(){

        echo  "<h1>The fruit is {$this->name} and the color is {$this->color}.</h1>";
    }
}

$apple = new Fruit("Apple", "red");
//echo $apple->get_name();
?>

Running this code will display the echo statement outside of the box. How do I put that statement inside the div class?

This is the screenshot of the results of my code

2 Answers

Your current method should display the statement inside <div class="box"> </div>. But if the contents are out of the box it may be due to some problem in your CSS.

Could your share your CSS code and maybe a screenshot of how your content appears.

================== After Edit ======================

The first fix I made was: I used <?php include "./menu.php"; ?> instead of <?php include("menu.php"); ?>

The second is when you echo it gets Correctly displayed within the div. The __destruct() function, is automatically called at the end of the script, and the contents of that are only displayed out of the box. uncommented echo $apple->get_name();

enter image description here

Wrapping up, __destruct() is used for recovering the heap space allocated during the lifetime of an object, closing file or database connections, and releasing network resources.

If you really want to display the H1 I made this change.

<?php

    $des=""; 

    class Fruit
    {
        public $name;
        public $color;

        function __construct($name, $color)
        {
            $this->name = $name;
            $this->color = $color;
        }
        function get_name()
        {
            return $this->name;
        }
        function __destruct()
        {

            global $des;
            $des = "<h1>The fruit is {$this->name} and the color is {$this->color}.</h1>";
            
        }
    }

    $apple = new Fruit("Apple", "red");
    echo $apple->get_name();
    unset($apple);
    echo $des;    
?>

Updated Image

Helpful links: PHP unset

You shuld try following source code

<div class="box">
<img src="profile.jpg" alt="" class="box-img">
<h1>DarkCode</h1>
<h5>Web Devloper - Web Designer</h5>
<?php include("menu.php"); ?>
<ul>
    <li><a href="#"><i class="fa fa-facebook-square" aria-hidden="true"></i></a></li>
    <li><a href="#"><i class="fa fa-twitter-square" aria-hidden="true"></i></a></li>
    <li><a href="#"><i class="fa fa-google-plus-square" aria-hidden="true"></i></a></li>
</ul>
<ul><li><?php echo $apple->get_name();?></li></ul>

And, you can add <?php echo $apple->get_name();?> somewhere else also. Just add <h4><?php echo $apple->get_name();?></h4> You can add whereever you want to add it. <label><?php echo $apple->get_name();?></label>

Related