PHP 变量在设置值后显示为空
PHP variables seem null after values set
这些 PHP 值在我将空变量设置为值并将它们发布到页面上后输出空数据。有谁知道为什么?
这是我的代码:
.PHP:
<?php
$name = $_POST['query'];
$cap = null;
$ugly = "http://25.media.tumblr.com/tumblr_m3v5kj4ERf1qijcxeo1_500.gif";
$pretty = "http://upload.wikimedia.org/wikipedia/commons/e/e8/Bebe_Phoque_de_Weddell_Baby_Weddell_Seal.gif";
$hot = "http://hosting01.hotchyx.com/adult-image-hosting-42/466laugh.gif";
$weird = "http://d2tq98mqfjyz2l.cloudfront.net/image_cache/1308355337507544.gif";
$choice = null;
$little = null;
$adj1 = "walrus devouring";
$adj2 = "cat canoodling";
$adj3 = "voluptuous machine of ghettoness and stupidity";
$adjch = null;
$i = rand(1,4);
$w = rand(1,3);
if(w == 1)
{
$adjch = $adj1;
}
elseif (w == 2)
{
$adjch = $adj2;
}
elseif (w == 3)
{
$adjch = $adj3;
}
if(i == 1)
{
$choice = $ugly;
$little = "You should probably see a doctor.";
$cap = "$name, You're uglier than all of One Direction * 2 + 100. That's pretty bad.";
}
elseif (i == 2)
{
$choice = $weird;
$cap = "$name, you're weird.";
$little = "I wish I didn't know you and you didn't exist you creepy, $adjch diddly do gooder.";
}
elseif (i == 3)
{
$choice = $pretty;
$cap = "$name, you're the prettiest $adjch person on earth.";
$little = "ilu so much";
}
else if(i == 4)
{
$choice = $hot;
$cap = "$name, help the homeless. Take me home with you.";
$little = "You're hot. Really hot.";
}
?>
.HTML:
<html>
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="index.html">Home</a></li>
<li><a href="blog.html">Blog</a></li>
<li><a href="about.html">About</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class='push'></div>
<p><img src="<?php echo $choice;?>"</p>
<h2 align='center'><strong><?php echo $cap;?></strong></h2>
<p><?php echo $little;?></p>
<p><a href="index.html" class='btn'><i class="icon-search"></i> Go back </a></p>
</body>
<div class='footerpush'></div>
<hr>
<div align='center'>
<a href='about.html'>about</a>
|
<a href='blog.html'>blog</a>
|
<a href='twitter.com'>twitter</a>
|
<a href='notice.html'>our policy</a>
</div>
<p>© bleh blah</p>
</html>
谢谢大家(附言,我是PHP菜鸟,很抱歉)
if(w == 1)
? if(i == 1)
?什么是w
?什么是i
?您只声明了$w
和$i
.
只是为了让您知道您可以使用数组大大简化此代码。
<?php
$name = $_POST['query'];
$adjs = array(
1 => "walrus devouring",
2 => "cat canoodling",
3 => "voluptuous machine of ghettoness and stupidity"
);
$adjch = $adjs[rand(1, 3)];
$choices = array(
1 => "http://25.media.tumblr.com/tumblr_m3v5kj4ERf1qijcxeo1_500.gif",
2 => "http://d2tq98mqfjyz2l.cloudfront.net/image_cache/1308355337507544.gif",
3 => "http://upload.wikimedia.org/wikipedia/commons/e/e8/Bebe_Phoque_de_Weddell_Baby_Weddell_Seal.gif",
4 => "http://hosting01.hotchyx.com/adult-image-hosting-42/466laugh.gif"
);
$littles = array(
1 => "You should probably see a doctor.",
2 => "I wish I didn't know you and you didn't exist you creepy, $adjch diddly do gooder.",
3 => "http://upload.wikimedia.org/wikipedia/commons/e/e8/Bebe_Phoque_de_Weddell_Baby_Weddell_Seal.gif",
4 => "You're hot. Really hot."
);
$caps = array(
1 => "$name, You're uglier than all of One Direction * 2 + 100. That's pretty bad.",
2 => "$name, you're weird.",
3 => "ilu so much",
4 => "$name, help the homeless. Take me home with you."
);
$i = rand(1, 4);
$choice = $choices[$i];
$cap = $caps[$i];
$little = $littles[$i];
?>
用以下命令更改所有IF:
if ($w == ...)
和if ($i==...)
,当然还有如何使用 switch 语句
相关文章:
- 单击“JavaScript显示变量”
- Cartodb信息窗口未在地图上显示变量
- 如何更新和显示变量
- 如果项目不匹配,则显示变量
- 在 HTML 中显示变量的好做法是什么?
- 在 Javascript 中,值不来,而是显示变量名称
- 缩短角度显示变量
- 如何使用Tinybox显示变量
- 在不同条件下显示变量的不同值
- console.log() 在变量值实际更改之前显示变量的更改值
- 引导程序引导盒在消息中显示变量
- 仅显示变量内的一段文本
- 我想使用 innerHTML 显示变量的值,但我的代码不起作用
- 使用带有按钮的JS函数来显示变量
- 无法使用车把正确在我的“模板”中显示变量.js
- 选中选择框选项时显示变量
- 如何使用显示变量url的iframe打开引导模式
- 我想显示变量'b[i]'而不是'x[i].childNodes[0].nodeValue'
- 数字框jeasyui没有显示变量中的值
- 如何在AngularJS网站中显示变量的名称