Re: inclosed variable heredoc



hihibibihi wrote:

<?php
$foo="NO";
$text=<<<EO
The variable is named: $foo
EO;

if (TRUE)
{
$foo="YES";
die($text); //how to get $foo="YES" into $text?
}
?>

As Janwillem said, you're doing things the wrong way around. What I think
you're actually asking for is a way to specify the format of the error
message at the top of the code instead of way down the bottom when the
error actually happens. Here, sprintf() is your friend...

<?php
$foo = 'NO';
define('ERRMSG_FOO', 'Foo is now "%s", so I shall die.');

if (1)
{
$foo = 'YES';
die(sprintf(ERRMSG_FOO, $foo));
}
?>

You don't need to use a constant of course -- you could use:

<?php
$foo = 'NO';
$ERRMSG_FOO = 'Foo is now "%s", so I shall die.';

if (1)
{
$foo = 'YES';
die(sprintf($ERRMSG_FOO, $foo));
}
?>

But assuming the message format is not going to change half-way through
your script, a constant seems more sensible.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

.



Relevant Pages

  • Re: Case sensitivity in programming languages.
    ... Every time someone creates a post about case sensitve languages I reserve ... At least not in PHPP, ... Thus I can create different things called 'foo' and reference each ... sensitive languages trying to force case sensitivity into ALL languages. ...
    (comp.lang.php)
  • Re: Getting the names of variables passed to functions
    ... I'd like the function foo to return a string of the variable name ... but as far as I know PHP doesn't support them. ... public function getValue() ... public var $bar; ...
    (comp.lang.php)
  • Re: Case sensitivity in programming languages.
    ... I would rather go by recognized experts - like K & R, the Bjorn Stroustrop, the initial designers of Java at Sun, the developers of PHP, the folks who developed the XML spec, and even the majority of the people who have responded on this thread. ... You don't have to use PHP or any other case sensitive language. ... There is no confusion between foo the constant, $foo the variable, foothe function and $object->foothe class method.. ... It may be a *convention* amongst certain groups of programmers, but it is not a language *requirement*. ...
    (comp.lang.php)
  • Re: how do i avoid typing the keyword "$this->" every time inside a class
    ... Thinking of it, IMHO, this is a big excuse we (PHP community) give ... Or better still, one could write a slightly complex parsing script, say ... One of the important things here is that PHP allows two $foo variables ... public function showfoo{ ...
    (comp.lang.php)
  • Re: unset($this) or maybe unset(&$this)
    ... PHP is not C++, so expecting the same behaviour from two different languages ... It is not wise to delete an object while you ... but the object, which contains a copy of that code in memory, does not, ... obj is an instance of class Foo), the compiler actually translates that to ...
    (comp.lang.php)