我试图弄清楚如何<<<HTML在php中使用时使用已定义的变量.这是我想要实现的一个例子:
<?php
define('TEST','This is a test');
echo <<<HTML
Defined: {TEST}
HTML;
?>
获取定义的"TEST"的适当方法是<<<HTML什么?
编辑:
我做了一个小测试来检查哪一种方法最快.对于我的测试,我在heredoc中使用了20个变量.以下是使用不同方法(以秒为单位)发生的事情:
访问<<< HTML in php中的定义变量似乎是最慢的方法 -  0.00216103.
访问<<< HTML in php中的定义变量更快 -  0.00073290.
访问<<< HTML in php中的定义变量甚至更快 -  0.00052595.
访问<<< HTML in php中的定义变量是最快的 -  0.00011110.
希望这有助于别人:)
下面的示例中有两种不同的语法.一个工作,另一个不工作!实际上我希望它反过来.第二种语法对我来说看起来很糟糕.
<?php
class Vodoo
{
    public $foo = array();
    public function __construct()
    {
        $this->foo = array('one' => 1, 'two' => 2, 'three' => 3);
    }
    public function getFoo()
    {
        $return = <<<HEREDOC
<p>$this->foo[one]</p>      // outputs: "Array[one]"
<p>{$this->foo['two']}</p>  // outputs correct: "2"
HEREDOC;
        return $return;
    }
}
$bar = new Vodoo;
echo $bar->getFoo();
?>
是否可以使用这些花括号并引用HEREDOC内的关联索引?
编辑:花括号内的表达式必须以它出现在字符串外面的方式编写!
我一直收到以下错误:
解析错误:语法错误,第70行/home/a4999406/public_html/willingLog.html中的意外T_SL
在以下代码上(第一行是第70行):
        echo <<<END 
<form action = "willingLog.html" method="post"><pre>
    First       <input type="text" name="first" />
    Last        <input type="text" name="last" />
    Email       <input type="text" name="email" />
    Username    <input type="text" name="user_name" />
                <input type="submit" value="AD RECORD" />
</pre></form>
END;
heredoc似乎没有用.我试过其他的例子.
以下是doctype标题.那里有什么不对吗?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
bash中的以下函数会出现标题中提到的错误.当最终EOF不在行的开头时,通常会出现错误.
EOF是在开始,所以我看不出有什么问题.进一步在脚本(未显示)中还有其他here-docs,它们可以工作.
add_testuser()
{
    kadmin -p admin -q addprinc test
    cat <<EOF > ~/test.ldif
dn: cn=test,ou=groups,dc=${ARRAY[1]},dc=${ARRAY[2]}
cn: test
gidNumber: 20001
objectClass: top
objectClass: posixGroup
dn: uid=test,ou=people,dc=${ARRAY[1]},dc=${ARRAY[2]}
uid: test
uidNumber: 20001
gidNumber: 20001
cn: First_name
sn: Last_name
objectClass: top
objectClass: person
objectClass: posixAccount
objectClass: shadowAccount
loginShell: /bin/bash
homeDirectory: /home/test
userPassword: {CRYPT}*
EOF 
    ldapadd -Qf ~/test.ldif
    kdestroy; kinit test
    klist
    ldapwhoami
}
我继承了一个网站,我需要对其进行一些更改.一如既往,网站开发有很多方法可以做.但是,这个特殊的网站让我很困惑.所有的html都是通过heredoc php脚本提供的.但是,没有任何PHP处理.
例如(仅限结构)index.php:
<?php
$html = <<<html
<html>
    <head></head>
    <body>
        {more simple HTML here.  No php processing to be done}
    </body>
</html>
html;
echo $html;
?>
我理解php,但我无法弄清楚为什么他们以这种方式生成页面,而不是简单地交付它而没有php变量处理.在我假设这个人不知道他们在做什么之前,我想我会问.有任何想法吗?
这是我在StackOverflow上的第一篇文章,所以对论坛礼仪的反馈也很受欢迎.
我已经找到了一些解决方案,但不知道发生了什么......
示例 1:
<?php
echo <<< EOD
test
EOD;
示例 2:
<?php
echo <<< 'EOD'
test
EOD;
输出 1,2:
PHP Parse error:  syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN)
示例 3:
<?php
echo <<< EOD
test
EOD;
?>
示例 4:
<?php
echo <<< 'EOD'
test
EOD;
?>
示例 5:
<?php
echo <<< EOD
test
EOD;
'dummy';
示例 6:
<?php
echo <<< 'EOD'
test
EOD;
'dummy';
输出 3、4、5、6:
test
有谁知道如何在食谱上使用here-document重定向?
test:
  sh <<EOF
  echo I Need This
  echo To Work
  ls
  EOF
我找不到任何解决方案尝试通常的反斜杠方法(基本上以一行中的命令结束).
理由:
我有一组多行配方,我想通过另一个命令代理(例如,sh,docker).
onelinerecipe := echo l1
define twolinerecipe :=
echo l1
echo l2
endef
define threelinerecipe :=
echo l1
echo l2
echo l3
endef
# sh as proxy command and proof of concept
proxy := sh
test1:
  $(proxy) <<EOF
  $(onelinerecipe)
  EOF
test2:
  $(proxy) <<EOF
  $(twolinerecipe)
  EOF
test3:
  $(proxy) <<EOF
  $(threelinerecipe)
  EOF
我希望避免的解决方案:将多行宏转换为单行.
define threelinerecipe :=
echo l1;
echo l2;
echo l3
endef
test3:
  $(proxy) <<< "$(strip …我需要在具有参数化值的交互式程序/实用程序中执行一系列命令。有没有办法在 heredoc 内循环?像下面..不知道eval这里是否有任何帮助。下面的示例似乎不起作用,因为交互式似乎无法识别系统命令。
#!/bin/sh
list="OBJECT1 OBJECT2 OBJECT3"
utilityExecutable << EOF
for i in $list ; do
utilityCommand $i
done
EOF
假设一个文件file有多行。
$ cat file
foo
bar
baz
进一步假设我希望用 while 循环遍历每一行。
$ while IFS= read -r line; do
$   echo $line
$   # do stuff
$ done < file
foo
bar
baz
最后,请假设我希望传递存储在变量中的行而不是存储在文件中的行。如何循环保存为变量的行而不收到以下错误?
$ MY_VAR=$(cat file)
$ while IFS= read -r line; do
$   echo $line
$   # do stuff
$ done < $(echo "$MY_VAR")
bash: $(echo "$MY_VAR"): ambiguous redirect
我想在 docker-compose yaml 文件中嵌入 HEREDOC。
version: "3.7"
services:
  test-cli:
    image: ubuntu
    entrypoint: |
      /bin/sh << HERE
      echo hello
      echo goodbye
      HERE
当我尝试运行此程序时,出现以下错误。
 docker-compose -f heredoc.yml run --rm test-cli
Creating network "dspace-compose-v2_default" with the default driver
/bin/sh: 0: Can't open <<