当前位置: 动力学知识库 > 问答 > 编程问答 >

Variables from PHP class to html

问题描述:

This question already has an answer here:

  • how do use php variable in html? [closed]

    7 answers

网友答案:

To display php in a html you'll need to use one of the following depending on the structure of your php:

In your php file, you may want to include a constructor and a public function to call to retrieve your stored data.

class Restourant{
    private $id;
    private $name;
    private $deliveryEnd;
    function __constructor($id, $name, $deliveryEnd){
        $this->id = $id;
        $this->name = $name;
        $this->deliveryEnd = $deliveryEnd;
    }
    public function toHTML(){
        $html = "<span>ID: ". $this->id ."</span>";
        $html .= "<span>Name: ". $this->name ."</span>";
        $html .= "<span>Delivery End: ". $this->deliveryEnd ."</span>";
        return $html;
    }
}

In your html file you may use your php like this. There are many ways to display php in html.

<?php require "path_to_php_file.php";?>
<!DOCTYPE html>
<head></head>
<body>
<?php
$phpVar = new Restourant($id, $name, $deliveryEnd);
echo $phpVar->toHTML();
?>
</body>
</html>
网友答案:

you can just echo it out anywhere within html tags

for example

<?php
  class Restourant{

  public $id;
  public $name;
  public $deliveryEnd;
?>

<html>
  <body>
    the variable id is <?php echo $id; ?>
    the variable name is <?php echo $name; ?>
    the variable deliveryEnd is <?php echo $deliveryEnd; ?>
  </body>
</html>
分享给朋友:
您可能感兴趣的文章:
随机阅读: