【问题标题】:Uninitialized string offset in phpphp中未初始化的字符串偏移量
【发布时间】:2015-08-07 11:54:00
【问题描述】:

我查看了其他看起来像我的问题,但找不到好的答案。所以

$machines = get_machine($platform);
$options = array() ;
$options[0] = "please select";
foreach( (array)$machines as $machine_){
    $options[$machine_[0]] = $machine_[1] ;
    array_push($temp,$machine_[0]); 
}

//print_r($options);

$form->addElement(new Element\Select("Existing machines :", "machine", array("onchange" => "this.form.submit()", "value" => $machine)));
if ( !in_array( $machine, $temp ) )
    $machine = 0;

$form->addElement(new Element\Textbox("Add new/Edit machine:", "new_machine", array("placeholder" => "new machine", "shortDesc" => "Add new machine or edit the existing one", "value" => get_machine( $machine ))));

它说“机器”未定义和未初始化 offset 。 这里定义:

if ( isset($_POST['machine']) ) $mask = $_POST['machine']; else $machine = 0;

我有与其他变量完全相同的代码,它并没有给我这种性质的错误。我敢肯定,没有错别字。

【问题讨论】:

  • 你可以在你的if条件上面添加这个$mask = $machine = "";
  • 谢谢,这对未定义的部分有所帮助。

标签: php arrays string


【解决方案1】:

我相信这会让我再次投反对票,但一种帮助我使我的陈述保持简短和可读的方法(无需一直使用isset($_POST['...']))将预期值的初始化放在顶部我的页面和直接在其下方一个extract($_POST) 命令,例如:

$amount=$mask=$machine=0; $flag1=$flag2=$flag2=false;
extract($_POST);

是的,这个会把已经发布的一切变成我页面上的一个php变量,但是...

  1. 首先应该是posted(这样可以缩小范围)
  2. 它要么被忽略(如果它是一个未询问的变量),要么稍后在我自己的代码中重置为正确的值。

好处是在这个初始化之后我不需要再为任何isset()s 操心了。我只是直接使用预期的变量。它将以初始化形式(值为 0 或 false)或 extract() 语句的结果出现。

【讨论】:

    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    相关资源
    最近更新 更多