tags:

views:

218

answers:

5

Hi, I know this is an elementary question for you php people out there:

I put p tags around in some php by doing this:

echo "<p>";
echo $VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE') . '<br />';
echo "</p>";

It is a silly way to do it. So how can I put the p tags within the one 'echo'?

+6  A: 
echo "<p>" . $VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE') . '<br /></p>';

or

echo "<p>" , $VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE') , '<br /></p>';

The later is only possible with echo (not print) and theoretically saves some computation time, as the string don't need to be concatenated together. Probably won't mater 99% of the time, but it's nice to know about.

The first says

  1. Attach (concatenate) "<p>" to $VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE')
  2. Attach the rusult of 1 to '<br /></p>'
  3. echo out the result of 2

While the later says

  1. echo out "<p>"
  2. echo out $VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE')
  3. echo out '<br /></p>';

A single concatenation will almost always take less processing power than an echo

Schotime
Curses! Beaten by 13 seconds! ;-)
Kyle Cronin
haha. You gotta be quick here at the flow! ;)
Schotime
thanks Schotime, that did the trick! and you taught me some php :)
morktron
You might also throw in some \n stuff where you want the lines to break in the source code when you do "view source" in the browser..
Tim
+4  A: 

You can concatenate <p> and </p> on to the ends:

echo '<p>'.$VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE').'<br /></p>';

The dot ('.') is PHP's string concatenation operator and can be used to combine several string literals, variables, and string-returning functions. However, there is an alternative:

echo "<p>{$VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE')}<br /></p>";

This uses variable parsing to create the desired string.

Kyle Cronin
thanks for your help, these things are really useful to know and I appreciate it :), going to take some time out from work now to assimilate...
morktron
+2  A: 

I don't think your way is that "silly", it may even be more readable with a big variable name like that. But here is how I'd do it:

echo '<p>', $VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE'), '<br /></p>';

You could also do this:

echo "<p>{$VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE')}<br /></p>";
yjerem
+2  A: 

Yet another way...

<?
// Existing code block

// Using the <?= operator below is a short form "echo" for variables
?>

<P><?=$VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE');?><BR /></P>


<?
// continue php code
?>
Eddy
A: 

Because nobody has mentioned it before, I'll add this one for the sake of completeness (in most cases I wouldn't want to use it but there are cases where it might come in handy):

printf('<p>%s<br /></p>', $VM_LANG->_('PHPSHOP_COUPON_ENTER_HERE');
Stefan Gehrig