PHP代码正在生成一个额外的字符串
我正在尝试使用PHP编写扑克骰子程序。详情如下:PHP代码正在生成一个额外的字符串,php,html,Php,Html,我正在尝试使用PHP编写扑克骰子程序。详情如下: //Please ignore the session variable and calculation parts of the program...its still incomplete because it does not affect the user interface in this case. <?php session_start() ?> <!DOCTYPE html> <
//Please ignore the session variable and calculation parts of the program...its still incomplete because it does not affect the user interface in this case.
<?php session_start() ?>
<!DOCTYPE html>
<html>
<head>
<title>My Own Poker Dice</title>
</head>
<body>
<center>
<?php
print "<h1>Poker Dice</h1>";
if(!filter_has_var(INPUT_POST, "doNext")){
startGame();
} else{
playGame();
}
function startGame(){
$cash=100;
$_SESSION["cash"]=100;
firstPass();
}
function playGame(){
$doNext=filter_input(INPUT_POST, "doNext");
if($doNext=="firstPass"){
firstPass();
} else if($doNext=="secondPass"){
secondPass();
}
}
function firstPass(){
print <<<HERE
<fieldset>
<legend>
<h3>First pass</h3>
</legend>
HERE;
for($i=0;$i<5;$i++){
$die[$i]=rand(1,6);
print <<<HERE
<form method="post" action="">
<img src="$die[$i].png"/>
<input type="checkbox" name="keepIt[$i]" value="$die[$i].png"/> //keepIt is not directly an array. its a part of making an array. its only an array after you extract it and assign it to an array name.
HERE;
}
print <<<HERE
</fieldset>
<p>
<input type="hidden" name="doNext" value="secondPass"/>
<input type="submit" name="submit-firstPass" value="GO!"/>
</p>
</form>
HERE;
}
function secondPass(){
if(filter_has_var(INPUT_POST, "keepIt")){
$formData=filter_input_array(INPUT_POST);
$keepIt=$formData["keepIt"];
for($i=0;$i<5;$i++){
if(empty($keepIt[$i])){
$keepIt[$i]=0;
}
}
} else{
$keepIt=array(0,0,0,0,0);
}
for($i=0;$i<5;$i++){
if($keepIt[$i]==0){
$die[$i]=rand(1,6);
} else{
$die[$i]=$keepIt[$i];
}
}
print <<<HERE
<fieldset>
<legend>
<h3>Second pass</h3>
</legend>
HERE;
for($i=0;$i<5;$i++){
print "<img src='$die[$i].png'/>";
}
print <<<HERE
</fieldset>
<form>
<p>
<input type="hidden" name="doNext" value="firstPass"/>
<input type="submit" name="submit-secondPass" value="GO!"/>
</p>
</form>
HERE;
}
?>
</center>
</body>
</html>
我需要您的帮助来找出导致此问题的原因。这是一个肮脏的解决方案,但为什么不更换
for($i=0;$i<5;$i++){
print "<img src='$die[$i].png'/>";
}
对于($i=0;$i您的问题是:
<input type="checkbox" name="keepIt[$i]" value="$die[$i].png"/>
你应该准备好了。OP解释说他是一个初学者,只是在学习。我们不应该教他肮脏的把戏。我说这是一个肮脏的解决方案,但没有错,因为它只是剥去了它。他只通过了2次,所以图像会很好。所以这是一个正确的解决方案。这是错误的。这是非常错误的。事实是有效的,并不意味着它不是w这甚至是一个可怕的肮脏黑客,在trim('foop.png','.png')这样的情况下都会失败
。trim
的第二个参数可能不会像你认为的那样。像这样的解决方案唯一可行的时候是,如果你有一个昂贵的截止日期,并且不知道在哪里可以找到真正的问题。即使这样,你也应该在有时间的时候修复它。提示:PHP不会随机添加字符串,它总是一个bug在你的代码中…已经有一个否决票了。1就够了。请不要再多了。不要担心否决票,只要试着问一些聪明的问题(阅读)并继续尝试。好吧,巴特,我会读的。你能把我的问题投上去吗?我会从现在开始试着问一些更聪明的问题。谢谢deceze的提示。谢谢。我会试试这个。
for($i=0;$i<5;$i++){
print "<img src='" . trim($die[$i],'.png') . ".png'/>";
}
<input type="checkbox" name="keepIt[$i]" value="$die[$i].png"/>
<input type="checkbox" name="keepIt[$i]" value="$die[$i]"/>