PHP 无法识别我在 index.view 中的'<del>'标记.php并将其从输出的 html 中删除



如果completed为true,我正试图让index.view.PHP文件中的PHP代码在任务描述中加上一个strike。我一直在学习Laracasts上的教程,向我介绍课程,他使用<strike></strike>,但这不起作用,我在w3schools上看到HTML5不再支持该标签,建议使用<s></s><del></del>。然而,这对我来说并不奏效

我试过用火狐浏览器代替谷歌浏览器,但是结果完全一样。这个问题仍然存在。

以下是我的index.php文件:

<?php
class Task {
public $description;
public $completed = false;
public function __construct($description)
{
$this->description = $description;
}
public function complete()
{
$this->$completed = true;
}
public function isComplete()
{
return $this->$completed;
}
}
$tasks = [
new Task("Go to the store"),
new Task("Finish my screencast"),
new Task("Finish PHP Course")
];
$tasks[0] -> complete();
require 'index.view.php';

以下是我的index.view.php文件:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Document</title>
</head>
<body>
<ul>
<?php foreach ($tasks as $task) : ?>
<li>
<?php if ($task->completed) : ?>
<del><?= $task->description; ?></del>
<?php else: ?>
<?= $task->description; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>

以下是在Chrome上输出的HTML:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Document</title>
</head>
<body>
<ul>

<li>
Go to the store            
</li>

<li>
Finish my screencast            
</li>

<li>
Finish PHP Course            
</li>

</ul>
</body>
</html>

您的get和set方法是错误的。你的美元符号太多了。这是你应该做的。

public function complete() { 
$this->completed = true; 
} 
public function isComplete() {
return $this->completed; 
}

此外,还有一个提示:如果编写getter,请尝试使用isComplete()getter,而不是访问completed属性。然后你也可以让财产私有化。

最新更新