Re: Heredoc printing

From: Date: Wed, 16 Aug 2000 04:33:37 +0000
Subject: Re: Heredoc printing
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-11913@lists.php.net to get a copy of this message
John Schuhr writes: > I've been playing with this for a few hours and I have several problems. > First, I'm stubborn and second, I'm lazy. I could re-write this using a > long drawn-out combination of prints and concatenations and/or variable > setting prior to printing. But I'm trying to keep my code as compact and > readable as possible. Ideally, I'd like to be able to do the following: > > print <<<EOT > <tr> > <td>$cursor->Column("contactid")</td> > </tr> > EOT; > > Unfortunately, this prints something like: > > <tr> > <td>("contactid")</td> > </tr> > > instead of printing: > > <tr> > <td>48999</td> > </tr> > > Any ideas? > > --John Since you cannot call a function from within a string, there is no way to do this exactly as you've asked. What is happening is that when you execute the above, PHP knows that since you can't call a function from within a string, $cursor->Column must be the name of a variable (since a parenthesis is not a legal character in a variable name). Thus, PHP replaces the variable it sees with that variable's value. In this case, the variable has no value, and so nothing is placed into the string there. PHP then looks at the rest of the string, sees the ("contactid") as regular text, and skips it for evaluation. This becomes a bit more clear if you thrown an error_reporting(E_ALL); before that point in the script, so you can see PHP complaining about the undefined property Column being referenced. There are various ways around it, but none using the above syntax. Probably the easiest would be: print " <tr> <td>" . $cursor->Column("contactid") . "</td> <td>" . $cursor->Column("lastname") . "</td> <td>" . $cursor->Column("firstname") . "</td> <td>" . $cursor->Column("organization") . "</td> <td>" . $cursor->Column("country") . "</td> </tr> "; Another option is to drop out of PHP mode and do it this way: ?> <tr> <td><?= $cursor->Column("contactid") ?></td> </tr> <?php But that requires that short_open_tags be On; you can set this in your php.ini file. If that's not an option, it gets a bit longer: ?> <tr> <td><?php echo $cursor->Column("contactid") ?></td> </tr> <?php There are probably a zillion ways to accomplish this. Unfortunately, the way you've decided to try it isn't one of them...:( Good luck anyway, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#11913) next »