以下代码始终显示错误消息:
注意:未定义的索引:p C:\ xampp \ htdocs \ school \ content.php 在 2 行< 注意:未定义索引:p C:\ xampp \ htdocs \ school \ content.php 2 行< 注意:未定义的索引:p C:\ xampp \ htdocs \ school \ content.php 6
<?php
if($_GET['p'] == "home" || !$_GET['p']){
echo "<title>Welcome to Homepage</title>";
}elseif($_GET['p'] == "about"){
echo "<title>ABOUT | $schoolname</title>";
}if($_GET['p'] == "contact"){
echo "<title>CONTACT US | $schoolname</title>";
}
?>
我添加了isset,但效果不佳。因为错误消息消失了,但是..
<?php
if(isset($_GET['p']) and $_GET['p'] == "home" || !isset($_GET['p']) and !$_GET['p'] ){
echo "<title>Welcome to Homepage</title>";
}elseif(isset($_GET['p']) and $_GET['p'] == "about"){
echo "<title>ABOUT | $schoolname</title>";
}if(isset($_GET['p']) and $_GET['p'] == "contact"){
echo "<title>CONTACT US | $schoolname</title>";
}
?>
出现在标题栏中的是 http:// localhost / school ..
我希望这样,如果它在主页上:&#34;欢迎来到主页&#34;。
... Thaanksss
答案 0 :(得分:2)
因为您希望在$_GET['p']=='home'
或!isset($_GET['p'])
上触发主页,所以您可以这样做:
$title = "Welcome to the Homepage";
if (isset($_GET['p'])) {
if ($_GET['p'] == "about")
$title = "About | $schoolname";
elseif ($_GET['p'] == "contact")
$title = "CONTACT US | $schoolname";
}
echo "<title>$title</title>";
这会将默认页面设为主页,并且仅在$_GET['p']
设置为about
或contact
时才会更改
答案 1 :(得分:1)
使用OR时要小心,最好用括号条件括起你的术语,但是代码更清晰如下
<?php
if(isset($_GET['p'])){
if( $_GET['p'] == "home" ){
echo "<title>Welcome to Homepage</title>";
}elseif($_GET['p'] == "about"){
echo "<title>ABOUT | $schoolname</title>";
}if( $_GET['p'] == "contact"){
echo "<title>CONTACT US | $schoolname</title>";
}
}else{
echo "<title>Welcome to Homepage</title>";
}
?>
或
<?php
$message=array("hombe"=>"Welcome to Homepage","about"=>"ABOUT | $schoolname","contact"=>"CONTACT US | $schoolname");
if(isset($_GET['p'],$message[$_GET['p']])){
echo "<title>".$message[$_GET['p']]."</title>";
}else{
echo "<title>Welcome to Homepage</title>";
}
?>
答案 2 :(得分:1)
<?php
$pgdata = array();
$pgdata['home'] = array(
'title'=>'Title for Home page',
);
$pgdata['about'] = array(
'title'=>'Title for About Me page',
);
$pgdata['contact'] = array(
'title'=>'Title for Contact',
);
$pgname = isset($_GET['pg']) ? trim(strip_tags($_GET['pg'])) : 'home';
$title = $pgdata[$pgname]['title'];
header('Content-type: text/html; charset=utf-8');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title><?php echo $title; ?></title>
</style>
</head>
<body>
<header id="header">
<h1><?php echo $title; ?></h1>
</header>
<section id="posts">
<nav id="menu">
<ul>
<li><a href="home.php" title="Home page">Home</a></li>
<li><a href="home.php?pg=about" title="About Me">About</a></li>
<li><a href="home.php?pg=Contact" title="Contact">Contact</a></li>
</ul>
</nav>
</section>
</body>
</html>
答案 3 :(得分:0)
使用您的方法,每个新标题都会有一个新的elseif
语句。只需将标题存储在数组中或从数据库中获取它们,如下所示:
$titles = [
'default' => '<title>Welcome to Homepage</title>',
'about' => "<title>ABOUT | $schoolname</title>",
'contact' => "<title>CONTACT US | $schoolname</title>",
];
之后,您只需使用以下代码即可显示任何标题:
if (!array_key_exists('p', $_GET) || !array_key_exists($_GET['p'], $titles)){
echo $titles['default'];
} else {
echo $titles[$_GET['p']];
}
答案 4 :(得分:0)
你的if条件可能不符合你的期望。其他人提供了解决方案,我将向您展示x and y || a and b
的结果:此代码
function tt($msg, $b=true) {
echo 'Call to tt ',$msg,' returning ', ($b)?'True':'False', "\n";
return $b;
}
if (tt('isset1') and tt('value1') || tt('isset2') and tt('value2'))
{echo "Expression is true\n";} else {echo "Expression is false\n";}
结果
Call to tt isset1 returning True
Call to tt value1 returning True
Call to tt value2 returning True
Expression is true
因此跳过or运算符后面的表达式。