所以我正在整理一个简单的php页面,它会根据放入的url显示不同的内容.出于某种原因,它似乎通过整个if语句而没有做任何事情.
<?php
if(array_key_exists('offer', $_GET)) {
if( $_GET == "ncp" ){
$title = 'Exclusive Offer from NCP';
} elseif($_GET == "rt") {
$title = 'Exclusive Offer from RT';
} elseif($_GET == "oo") {
$title = 'Exclusive Offer from OO';
}
} else {
$title = 'Check Out These Exclusive Offers!';
}
$title = strip_tags($title);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $title; ?></title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script type="text/javascript">
</script>
</head>
Run Code Online (Sandbox Code Playgroud)
你应该使用$_GET['offer'] == "foo"而不是$_GET == "foo"
更重要的是,使用比较严格,今后,以确保您比较同一类型(在这种情况下字符串)===.这是因为PHP倾向于强制转换类型,这既有用又烦人(参见http://php.net/manual/en/language.types.type-juggling.php):
$foo = "0"; // $foo is string (ASCII 48)
$foo += 2; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)
Run Code Online (Sandbox Code Playgroud)
$_GET:http://php.net/manual/en/reserved.variables.get.php
比较运算符:http://php.net/manual/en/language.operators.comparison.php