我在PHP對象使用如下代碼5個屬性:格式的HTML表格回聲輸出
<?php
class Person
{
private $gender, $race, $height, $weight, $eyes_color;
public function start ($gender,$race,$height, $weight, $eyes_color)
{
$this->gender=$gender;
$this->race=$race;
$this->height=$height;
$this->weight=$weight;
$this->eyes_color=$eyes_color;
}
public function show_attributes()
{
return sprintf("%s, %s, %s, %s, %s", $this->gender, $this->race, $this->height, $this->weight,$this->eyes_color);
}
}
$person=new person();
?>
我用下面的HTML代碼調用這個類現在
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Class Person</title>
</head>
<body>
<?php
require_once("Person.php");
$person->start("Male","Latin","1.83 cm","85 kg","Brown");
echo $person->show_attributes();
?>
</body>
</html>
,將打印出類似這樣
Male, Latin, 1.83 cm, 85 kg, Brown
但我想打印出類似這樣
--------------------------------------
|Male | Latin | 1.83 cm | 85 kg | Brown|
--------------------------------------
使用HTML表格。
我已經嘗試了幾件事,但是我不能讓它發生。
有沒有辦法迫使
echo $person->show_attributes();
只顯示一個屬性,所以我可以從一個HTML細胞表裏面打電話了嗎?
謝謝。
你是否試過循環'$ person-> show_attributes()'並在表格中附加一個新的''? – pdoherty926