简体   繁体   English

对于循环变量,对单引号和双引号感到困惑

[英]For loop variable, confused about single and double quotes

I'd like to make the following code a for loop to make everything read better, but I can't seem to get the quotes right and end up with a blank page 我想将以下代码设置为for循环,以使所有内容更好地阅读,但我似乎无法正确地引用引号并以空白页结尾

if ($_POST['week'])
{
  $week = $_POST['week'];
}
//or check for a value submitted by the week menu
elseif ($_POST["user_week1"] == "week1") {
  $week = "1";
}
elseif ($_POST["user_week2"] == "week2") {
  $week = "2";
}
else if ($_POST["user_week3"] == "week3") {
  $week = "3";
}
else if ($_POST["user_week4"] == "week4") {
  $week = "4";
}
else if ($_POST["user_week5"] == "week5") {
  $week = "5";
}
else if ($_POST["user_week6"] == "week6") {
  $week = "6";
}
else {
  $week = "1";
}

I tried to do: 我试着做:

if ($_POST['week'])
{
  $week = $_POST['week'];
}
for ($i = 1; $i<7; $i++)
{
  else if ($_POST["user_week'.$i.'"] == "week'.$i.'") {
    $week = $i;
}
}
else {
  $week = "1";
}

But that didn't work out too well, I tried using double quotes instead of single around the variables, plus '" and "' to no avail. 但这并不能很好地解决问题,我尝试在变量周围使用双引号而不是单引号,但加上“”和“”无济于事。

Can anyone help with this, or point me towards a good resource on single and double quotes for variables? 任何人都可以帮助解决这个问题,或者为我提供有关变量单引号和双引号的良好资源?

Are you all igrnoring the fact that the else if mustn`t be there!? 你们都忽略了一个事实,那就是else if不存在的话!

What you should do is the following 您应该做的是以下几点

$week = "1";
if ($_POST['week']) {
  $week = $_POST['week'];
} else {
  for ($i = 1; $i < 7; $i++) {
    if ($_POST["user_week" . $i] == "week" . $i) {
      $week = $i;
      break;
    }
  }
}
  elseif ($_POST['user_week' . $i] == 'week' . $i) 

以上更正应该起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM