如何显示数组元素的值或PHP定界符对象属性 [英] How to display the value of an array element or object property in PHP Heredoc syntax

查看:189
本文介绍了如何显示数组元素的值或PHP定界符对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有显示定界符输入字段里的数组值的麻烦。这里是code的一个片段:

I'm having trouble displaying an array value inside a heredoc input field. Here's a snippet of code:

class Snippet{
  protected $_user;
  protected $_class;
  protected $_messages;

  public function __construct(){
    $this->_user = $this->_class = NULL;
    $this->createUser();
  }

  public function createUser(){
    $keys = array('user_login','user_email');
    $this->_user = $this->_class = array_fill_keys($keys, '');
  }

  public function userErrors(){
    //by default give the login field autofocus
    $_class['user_login'] = 'autofocus';
  }


  public function getForm(){

  return <<<HTML
<form id='test' action='' method='post'>
$this->_messages    
  <fieldset>
    <legend>Testing Heredoc</legend>
    <ol>
      <li>
        <label for='user_login'>Username</label>
        <input  id='user_login' name='user_login' type='text' placeholder='Login name' value=$this->_user[user_login] $this->_class[user_login]>
      </li>
    </ol>
  </fieldset>
</form>
HTML;
  }
}

我的输出会产生一个显示为值数组[USER_LOGIN] 标记的输入框。
我可以指定数组给独立的变量,让我在寻找的输出,但是这似乎是一种不必要的浪费。 PHP 5.3.5

My output yields a labeled input box with the value displayed as Array[user_login]. I can assign the array to separate variables and get the output I'm looking for, but this seems like an unnecessary waste. PHP 5.3.5

推荐答案

裹花括号内的数组变量 {} 像这样

Wrap your array variable inside curly braces {} like this

value="{$this->_user['user_login']} {$this->_class['user_login']}"

这里是一个不错的文章这一点。

Here is a nice article on this.

从文章

不幸的是,PHP不提供用于调用任何直接的手段
  功能或HEREDOC字符串输出前pression结果。该
  这里的问题是,除非在大括号的东西开头
  一个美元符号,它不能被PHP的东西被认可
  更换。

Unfortunately, PHP doesn’t provide any direct means for calling functions or outputting expression results in HEREDOC strings. The problem here is that, unless the thing in the curly braces starts with a dollar sign, it can’t be recognized by PHP as something to be replaced.

这篇关于如何显示数组元素的值或PHP定界符对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆