Computing.Net > Forums > Web Development > _END php usage

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

_END php usage

Reply to Message Icon

Name: dsarosh
Date: September 21, 2009 at 05:26:19 Pacific
OS: Vista Ultimate
CPU/Ram: E6550, 4GB DDR2
Product: Assembled / ASSEMBLED
Subcategory: PHP
Tags: php, _END
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Radix-64
Date: September 21, 2009 at 08:59:18 Pacific
Reply:

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.


0

Response Number 2
Name: shutat
Date: September 21, 2009 at 13:03:15 Pacific
Reply:

<?php $waist = 44; ?>

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

:P


1

Response Number 3
Name: dsarosh
Date: September 21, 2009 at 21:05:46 Pacific
Reply:

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


0

Response Number 4
Name: Michael J (by mjdamato)
Date: September 23, 2009 at 22:19:54 Pacific
Reply:

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


1

Response Number 5
Name: dsarosh
Date: September 24, 2009 at 01:49:13 Pacific
Reply:

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


1

Related Posts

See More



Response Number 6
Name: Michael J (by mjdamato)
Date: September 24, 2009 at 06:53:29 Pacific
Reply:

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


2

Response Number 7
Name: dsarosh
Date: September 24, 2009 at 07:25:40 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: _END php usage

php n00b www.computing.net/answers/webdevel/php-n00b/265.html

PHP - saving a file www.computing.net/answers/webdevel/php-saving-a-file/2142.html

php help: submitting txt fields www.computing.net/answers/webdevel/php-help-submitting-txt-fields/2871.html