View Full Version : ECHOing: Losing contents of
marain
05-13-2012, 02:44 PM
Here is $quotations [4]:
"It is wrong to expect a reward for your struggles. The reward is the act of struggle itself, not what you win. Even though you can't expect to defeat the absurdity of the world, you must make that attempt. That's morality. That's religion. That's art. That's life.",
Here is $quotations [5]:
"-Phil Ochs",
Here is my PHP code:
echo $quotations [4];
echo "<br /><br /><div align='right'>";
echo $quotations [5];
echo "</div><br />";
echo "The PHP End";
Here is the generated page source:
It is wrong to expect a reward for your struggles. The reward is the act of struggle itself, not what you win. Even though you can't expect to defeat the absurdity of the world, you must make that attempt. That's morality. That's religion. That's art. That's life.<br />-Phil Ochs<br />The PHP End
I am losing a <br />, as well as the <div...> and </div> coding, do not understand why.
Help please?
A.
is this the actual HTML source (i.e., plain text),
or is it the browser's generated source (i.e., what you see when you click on "inspect element" or similar in your browser)?
Also, while I understand that every PHP tutorial in the world demonstrates
echo
echo
echo
echo
echo, don't do it that way. It's absurd. It wastes time, makes your code very difficult to manage, and harder to debug.
You might try more like so:
<?php
$quotations = array( /* all your quotes go here */ );
/*
the following string format (for $output) is called a HEREDOC.
a HEREDOC starts with <<<WHATEVERTEXTYOUCHOOSE
anything you like goes inside.
I use them for large chunks of HTML
because you can use "quotes" and $variables inside the string
without having to worry about escaping them.
a HEREDOC ends with
WHATEVERTEXTYOUCHOOSE
;
*/
$output = <<<HTML
<div>
{$quotations[4]}
<div style="text-align: right; margin: 1em 0;">
{$quotations[5]}
</div>
</div>
The PHP End
HTML
;
// don't echo _anything_ at all,
// until _everything_ is ready to be echoed.
echo $output;
exit;(I modified the HTML a little, too - use margins instead of <br> to control space between lines; and the "align" attribute is deprecated/obsolete)
p.s.
Please use the forum's bbcode tags and indent your code to make it more readable:
for php code............
<?php /* code goes here */ ?>
for html...............
<!-- markup goes here -->.....
for js/css/other.......
code goes here................
results in:
<?php /* code goes here */ ?>
<!-- markup goes here -->
code goes here
marain
05-13-2012, 08:45 PM
Traq,
1. Thank you for the suggestions on my posting queries more readably. I shall endeavor to comply.
2. Answering your question, it is the browser's generated source, i.e. what I get when I select view--->page source.
3. As one who programmed extensively in COBOL several generations ago, I did (and do) appreciate elegance (no, not an oxymoron) and optimization. For me, however, my own optimal learning sequence takes the progression of first mastering the basic rules of the language, and then exploring optimization and elegance.
4. Since the way that I coded it does not work, I'll have to try your method! (But I still do not understand why
echo "<br /><br /><div align='right'>";
drops data.)
A.
... But I still do not understand why
echo "<br /><br /><div align='right'>";
drops data.
I had suspected that, perhaps, it was not dropping the data - rather, the browser was removing/"correcting" it for some reason. It happens sometimes, especially if your page is being rendered in quirks mode for some reason.
If you're using ->view source, however, then that should be the actual PHP output...
Could you post your entire script, and maybe a link to the output page?
first mastering the basic rules of the language, and then exploring optimization and elegance.Of course.
If you want to avoid the HEREDOC, basic concatenation will produce exactly the same string:
$output = '<div>'.$quotations[4].'<div style="text-align: right; margin: 1em 0;">'.$quotations[5].'</div></div>The PHP End';However, I would still recommend not using echo (print, ?>, etc.) until _everything_ is ready to be output.
IMO, it's not "optimization" or "elegance" or an "advanced technique" - it's a very important basic concept that is consistently overlooked and/or taught wrong in the name of simplicity. :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.