在 PHP 中,可以使用各種方法動態生成網頁內容。以下是一些常見的方法:
<?php
echo "Hello, World!";
print "Hello, World!";
?>
<?php
$name = "John";
$age = 30;
echo "My name is " . $name . " and I am " . $age . " years old.";
?>
<?php
$name = "John";
$age = 30;
echo <<<EOT
My name is $name and I am $age years old.
EOT;
?>
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Content</title>
</head>
<body>
<?php
$name = "John";
$age = 30;
?>
<p>My name is <?php echo $name; ?> and I am <?php echo $age; ?> years old.</p>
</body>
</html>
<?php
$fruits = array("apple", "banana", "cherry");
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>
<?php
function display_message($message) {
echo $message;
}
display_message("Hello, World!");
?>
這些方法可以根據需要組合使用,以實現更復雜的動態網頁內容生成。