2016-06-21 86 views
-3

我有PHP類這樣的:變量的HTML

<?php 
class Restourant{ 

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

XXXXXX.php文件。我需要在html文件中顯示它。有任何想法嗎?我想我必須使用var_dump

+3

內'回聲$這個 - >名;' – samayo

+1

永遠不要用var_dump顯示數據的用戶。它只是用於調試目的。 – Phiter

+1

顯示「它」?您的類屬性或完整的類結構中的值? –

回答

1

要在HTML顯示PHP,你需要使用取決於你的PHP的結構如下條件之一:

在你的PHP文件,你可能要包括一個構造函數和公共函數調用檢索您存儲的數據。

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; 
    } 
} 

在你的html文件中,你可以使用你的php這樣。有很多方法可以在html中顯示php。

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

你可以只回聲它的任何地方HTML標籤

例如

<?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>