You are here: Re: Unix-style echo? « PHP Programming Language « IT news, forums, messages
Re: Unix-style echo?

Posted by Chris Hope on 05/09/07 00:44

Gilles Ganault wrote:

> Hello
>
> I remember a shortcut in Unix scripting that looks somewhat
> like this, but I can't remember what it's called, or what it really
> looks like:
>
> ---- CODE -----------
> print <<
> all
> this
> is
> sent
> as
> is
> <<
> ---- CODE -----------
>
> Does someone know what I'm talking about? I think I saw the same thing
> in a PHP script once.
>
> The reason I ask, is that I'd like to find a lighter alternative to
> this:
>
> ---- CODE -----------
> print "<html>\r\n";
> print "<head>\r\n";
> print "\t<meta http-equiv=\"content-type\" content=\"text/html;
> charset=iso-8859-1\">\r\n";
> print "\t<link rel=\"stylesheet\" href=\"/display.css\">";
> print "\t<meta http-equiv=\"Pragma\" content=\"no-cache\">";
> print "\t<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">";
> print "\t<title>My title</title>\r\n";
> print "</head>\r\n";
> print "<body>\r\n";
> ---- CODE -----------
>
> Thanks!

It's called heredoc syntax and in PHP works like this:

print <<<END_OF_TEXT
all
this
is
sent
as
is
END_OF_TEXT;

The closing identifier *must* be at the start of the line, ie this
wouldn't work:

print <<<END_OF_TEXT
all
this
is
sent
as
is
END_OF_TEXT;

The identifier itself (END_OF_TEXT in my example) can be anything you
want, following the naming rules as any other label in PHP: it must
contain only alphanumeric characters and underscores, and must start
with a non-digit character or underscore.

There's more information here:
http://php.net/heredoc#language.types.string.syntax.heredoc

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация