Th"/>
首页 \ 问答 \ 使用PHP将变量插入文本的最短方法是什么?(What is the shortest way of inserting a variable into text with PHP?)

使用PHP将变量插入文本的最短方法是什么?(What is the shortest way of inserting a variable into text with PHP?)

我想知道在PHP中插入文本是否有更短的方法

<?php
$city = "London";
?>
This website is a funky guide to <?php print $city; ?>!!!

例如,在轨道上使用ruby,我可以设置

city = 'London'

在代码中的某个地方,在我的.erb文件中,我可以做到

This website is a funky guide to <%= city %>!!!

我确实读过可以使用{$city}地方,但是我试过了,但事实并非如此。 那么是否有一个比<?php print $var; ?>更短的形式<?php print $var; ?> <?php print $var; ?>


I'm wondering if there is a shorter way of inserting text in PHP than

<?php
$city = "London";
?>
This website is a funky guide to <?php print $city; ?>!!!

For example, using ruby on rails, I could set

city = 'London'

somewhere in the code, and in my .erb file I could do

This website is a funky guide to <%= city %>!!!

I did read somewhere that {$city} could be used, but I tried it and it didn't. So is there a shorter form than <?php print $var; ?> ?


原文:https://stackoverflow.com/questions/1219008
更新时间:2023-06-07 18:06

最满意答案

这听起来像是脚本内部存在一些环境依赖性 - 本质上,它假定了一些关于它运行的环境的信息,当你手动运行它时是正确的,但是当launchd运行它时不会。 如果不知道脚本的任何内容,很难指出这可能是什么,但我可以建议几件事情来看看:

  • sudo launchctl并不是一个更强大的launchctl版本,它做的事情明显不同。 你需要弄清楚你想要哪一个,并使用它。

    当您以普通用户身份运行launchctl (例如, launchctl load )时,它会与您的用户启动实例进行交互,以管理启动代理 - 在您的用户会话中以您的用户身份运行的项目。

    以root身份运行launchctl (例如sudo launchctl load )时,它会与launchd的系统实例进行交互,以管理启动守护进程 - 以root用户身份在系统上下文中运行的项目。

    根据脚本的作用,你必须决定哪一个是合适的。

  • 检查system.log(可以使用Console实用程序查看它,或者tail -f /var/log/system.log )并查看它是否包含任何内容以指示脚本失败的原因。

  • 将条目添加到启动的.plist中以记录脚本的输出,并查看是否包含任何错误消息或其他错误指示​​:

    <key>StandardOutPath</key>
    <string>/tmp/turtle.out</string>
    <key>StandardErrorPath</key>
    <string>/tmp/turtle.err</string>
    

    编辑脚本以添加调试输出可能会有所帮助,因此您可以详细了解它的工作方式(/不工作)。

  • 脚本是否依赖于特定的工作目录和/或环境变量? 如果是这样,请将相应的WorkingDirectory和/或EnvironmentVariables项目添加到.plist。


It sounds like there's some environment dependence inside the script -- essentially, it assumes something about the environment it's running in that's correct when you run it by hand, but not when launchd runs it. Without knowing anything about the script, it's hard to point at what this might be, but I can suggest a few things to look at:

  • sudo launchctl isn't a more powerful version of launchctl, it does something significantly different. You need to figure out which one you want, and use it.

    When you run launchctl as a normal user (e.g. launchctl load), it interacts with your user instance of launchd to manage Launch Agents -- items that run in your user session, under your user identity.

    When you run launchctl as root (e.g. sudo launchctl load), it interacts with the system instance of launchd to manage Launch Daemons -- items that run in system context, as root.

    You'll have to decide which is appropriate, based on what this script does.

  • Check system.log (you can use the Console utility to view it, or tail -f /var/log/system.log) and see if it includes anything to indicate why the script is failing.

  • Add entries to the launchd .plist to record the script's output, and see if that includes any error messages or other indications of what's going wrong:

    <key>StandardOutPath</key>
    <string>/tmp/turtle.out</string>
    <key>StandardErrorPath</key>
    <string>/tmp/turtle.err</string>
    

    It may help to edit the script to add debugging output, so you can tell more about how it's working (/not working).

  • Does the script depend on having a particular working directory and/or environment variables? If so, add appropriate WorkingDirectory and/or EnvironmentVariables items to the .plist.

相关问答

更多