Advanced Search

Results 1 to 4 of 4

Thread: ECHOing: Losing contents of

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    130
    Thanks
    49
    Thanked 0 Times in 0 Posts

    Default ECHOing: Losing contents of

    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.&nbsp; Even though you can't expect to defeat the absurdity of the world, you must make that attempt.&nbsp;That's morality.&nbsp; That's religion. That's art.&nbsp; 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.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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)?

    Edit:

    Also, while I understand that every PHP tutorial in the world demonstrates
    PHP Code:
    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 Code:
    <?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] <?php /* code goes here */ ?> [/php]
    for html...............[html] <!-- markup goes here -->.....[/html]
    for js/css/other.......[code] code goes here................[/code]


    results in:
    PHP Code:
     <?php /* code goes here */ ?>
    HTML Code:
     <!-- markup goes here -->
    Code:
     code goes here
    Last edited by traq; 05-13-2012 at 05:48 PM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  3. #3
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    130
    Thanks
    49
    Thanked 0 Times in 0 Posts

    Talking

    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.
    Last edited by marain; 05-13-2012 at 08:47 PM. Reason: Message entry ended prematurely.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by marain View Post
    ... 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?
    Quote Originally Posted by marain View Post
    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:
    PHP Code:
    $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.
    Last edited by traq; 05-13-2012 at 11:20 PM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •