警告:explode()期望参数2为字符串 - 错误

警告:explode()期望参数2为字符串 - 错误

问题描述:

I need help understanding this error, just starting out ;)

Could this part of code cause the error in the subject?

$var = explode(',', $var);

I'm guessing exploding $var would need it's own variable, for example:

$myvar = explode(',', $var);

I need to understand as the first code did work on my server without warning, but my co-worker is getting the above warning.

我需要帮助理解这个错误,刚开始;) p>

可能 这部分代码导致主题中的错误? p>

  $ var = explode(',',$ var); 
  code>  pre> 
  
 

我猜测爆炸$ var需要它自己的变量,例如: p>

  $ myvar = explode(',',$ var); 
   code>  pre> 
 
 

我需要理解,因为第一个代码在没有警告的情况下在我的服务器上运行,但是我的同事正在收到上述警告。 p> DIV>

Your initial concern that you can not overwrite $var with explode(',',$var) is wrong since PHP is able to change the type of $var. This code adjustment however should lead you closer to where your problem is.

<?php

if(!is_string($var)) {
  echo "
$var contains:";
  var_dump($var);
  die('as you can see, var is not a string');
}
elseif(stripos($var,',') == FALSE) {
  echo "
$var contains:";
  var_dump($var);
  die('var is a string but has no commas');
}
else {
  $var = explode(',', $var);
  echo "
now $var contains:";
  var_dump($var);
  die();
}

Also, as mentioned, RTM and check out how explode works.