Re: Re: Preferred Syntax

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jim Lucas wrote:

>I second this example, with one minor change, I would add '{' and '}' around
>variables.
>
>echo <<<HTML
><a style="text-align:left;size:14;font-weight:bold"
>   href="/mypage.php/{$page_id}">{$page_name}</a><br>
>HTML;
>
>This works for $variables, $objects, and variable functions calls.  But doesn't
>work if you try to call functions directly (bummer).

In fact, we are in agreement here :) I was just simplifying for the
example at hand. And as for calling functions directly, also add
constants :(

However, it's easy enough to assign a constant to a variable and embed
it in a HEREDOC, and also easy to wrap a function in a method,
especially when your HEREDOC is within a method itself:

define('MSG', 'My name is');

class X {
  function html($text) {
    return htmlspecialchars($text);
  }

  function output($name) {
    $msg = MSG;
    echo <<<HTML
<p>$msg {$this->html($name)}</p>
HTML;
  }
}

$x = new X();
$x->output('silly "rockstar" name like <&>');

>[...]
>This is why I like heredoc syntax over pretty much everything else.

Concur!
-- 
Ross McKay, Toronto, NSW Australia
"Pay no attention to that man behind the curtain" - Wizard of Oz

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux