_END php usage

Assembled / ASSEMBLED
September 21, 2009 at 05:26:19
Specs: Vista Ultimate, E6550, 4GB DDR2
Hi,
This is the piece of PHP code I wrote.

<?php
$waist=44;
$info=<<<_END
<p style="background-color:lightgrey; border:2px thick;">His waist is $waist inches</p>
_END;
echo $info;
?>

I am able to get the outputs as below:
His waist is 44 inches
His waist is44 inches
His waist is 44.inches

But what I want is
His waist is 44inches
i.e. no space between 44 and inches.

Is there a special backspace character in php or html that I am not yet aware of?
Thanks for the help.

Sarosh


See More: _END php usage

Report •


#1
September 21, 2009 at 08:59:18
Hi, I'm thinking that it's the paragraph parameter ( </p>...</p>) for formatting in HTML that might be injecting the space. The paragraph formatting parameter will put a space between items to break paragraphs apart and some editors will put </p> in places to add more space.


Report •

#2
September 21, 2009 at 13:03:15
<?php $waist = 44; ?>

<p style="background-color:lightgrey; border:2px thick;">His waist is <?php echo $waist;?>inches</p>

:P


Report •

#3
September 21, 2009 at 21:05:46
Thanks for the replies, its appreciated.

I used to move in and out of the <?php... ?> all the time too like how shutat suggested.

But then I learned about the <<<_END ...._END construct and I thought the purpose of this construct was to specifically avoid moving in and out of <?php ...?> mode.

So do the experienced users feel it is a better option to move in and out of <?php ...?> to output html, rather than use constructs like <<<_END?

Sarosh


Report •

Related Solutions

#4
September 23, 2009 at 22:19:54
There is another solution to your problem, use curly braces {}. Specifically the problem, as I think you are aware, is that when combining the variable with other text the PHP parser cannot determine where the variable ends. You could use this as well:
<?php
$waist=44;
$info=<<<_END
<p style="background-color:lightgrey; border:2px thick;">His waist is {$waist}inches</p>
_END;
echo $info;
?>

Which tells the PHP parser explicitly what the variable name is and the curly braces are not included int he output.

however, to address your last question, I separate the PHP logic from the output. But, in the output I use small php code sections to output the results of the logic

<?php
$waist=44;
?>

<p style="background-color:lightgrey; border:2px thick;">His waist is <?php echo $waist; ?>inches</p>

This gives you several advantages. For example, the code is easier to debug and maintain. Plus, it allows you to change the output without having to chnage any of the logic.

Michael J


Report •

#5
September 24, 2009 at 01:49:13
Micheal J,
Thanks for the hint. I couldn't find this advice in any of the tutorials or even in the book I am learning php from currently. So thanks for the valuable hint.

In the book that I am reading, the author made it sound like the <<<_END .... _END tags are the perfect answer to moving in and out of php code. This is obviously not the case.

After successfully getting the out put of 44inches as you suggested, I tried the following piece of code for experimentation:
<?php
$waist=44;
$line=2;
$a=<<<_END
<p style="color: brown; border: 1px thick;">His waist is $waist+$line inches</p>
_END;
echo $a;
?>

I tried {$waist+$line}, I tried {$waist}+{$line}, etc, etc.

I think the author was mistaken in suggesting the use of <<<_END... _END construct because there seems to be more complications while using php variables while using the END construct.

Sarosh


Report •

#6
September 24, 2009 at 06:53:29
The "construct" you are referring to is called the heredoc syntax.

http://us.php.net/manual/en/languag...

The heredox syntax behaves exactly like text that is delimited with double quotes. It will evaluate a PHP variable in the string, but it will not perform any operations on it. The only difference/benefit of using the heredoc method as opposed to double quotes is that if the text includes double quotes you do not need to escape them with a backslash.

Personally, I try to always separate my logic (the PHP code) from the presentation (HTML/CSS) as much as possible. So, I typically have very specific HTML being generated in my logic. So, I don't use the heredoc method.

For example, if I need to present the sum of two values, then I would do the logic (the adding) in the PHP logic and save the value to a variable. Then in the HTML I would include the variable.

It may seem easier/quicker to just put ($a + $b) inline with the HTML, but you lose flexibility with your code.

Michael J


Report •

#7
September 24, 2009 at 07:25:40
I'm gong to follow your advice and do it your way by breaking in and out of <?php....?>. I used the same method while I was learning until I read about the heredoc construct.

This was helpful discussion and proves that experience is more helpful than reading books.

Hopefully I'll be posting more questions as I keep learning stuff in my free time. Thanks very much again.

Sarosh


Report •


Ask Question