doutuan6158 2013-07-19 05:00
浏览 34
已采纳

将PHP函数与HTML属性连接并且不能将类型对象用作数组

I have 2 questions

<< 1.>>

I have php print code like this.

echo '<label class="control-label" for="'.to_string($dt->name_comp).'">'.$dt->name_comp.'</label>';

to_string is function that produce My Name to my_name, and it works, clear, with $dt->name_comp produce the value from database. So.. that syntax must be html syntax like it: <label class="control-label" for="my_name">Name Component</label>

I wanna ask, why code above can't work? Why my_name not get into for?

I've tried this way:

echo '<label class="control-label" for="'. <?php to_string($dt->name_comp); ?>.'">'.$dt->name_comp.'</label>';

But, it same. The result is always like this: <label class="control-label" for=" ">Name Component</label> when I check it in firebug. So my_name appear outside form, doesn't enter into the form. Wonder why..

This is to_string() function:

function to_string($string) {
  $string = preg_replace('/[\'"]/', '', $string);
  $string = preg_replace('/[^a-zA-Z0-9]+/', '_', $string);
  $string = trim($string, '_');
  $string = strtolower($string);
  echo $string; 
}

<< 2>>

I have code for textinput like this:

echo '<input type="text" name="'.to_string($dt->name_comp).'" id="'.to_string($dt->name_comp).'" placeholder="'.$dt->name_comp.'"  value="'.$dt['/name_comp/'].'">';

But, there is an error: Fatal error cannot use object of type workspace_mockup_2\Models\Component as array What should I enter there? Need some advice.. Thank's.

  • 写回答

1条回答 默认 最新

  • douju1968 2013-07-19 05:31
    关注

    The last line of to_string() should be:

    return $string;
    

    When you do:

    echo "something".to_string(something_else);
    

    The sequence of events is this:

    1. to_string(something_else) is called.
    2. to_string echoes the string it creates.
    3. to_string returns undefined.
    4. "something" is concatenated with undefined, which results in "something".
    5. "something" is echoed.

    This is why to_string's result is echoes outside the label, because it is echoed in step 2, while the label is echoed in step 5.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统