SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,727
    Mentioned
    43 Post(s)
    Tagged
    0 Thread(s)

    Skip Lines in HTML Source Output

    So I've recently started using an array to generate my HTML output.

    Before I used to have my functions echo the HTML themselves... now I have them returning a variable that'd be echoed by the function's call.

    Example...


    Code PHP:
    <?php
    function writeSomeText()
    	{
    	$output[] = '<div>';
    	$output[] = '	<p>A Heading</p>';
    	$output[] = '	<p>Some text will go here...</p>';
    	$output[] = '</div>';
     
    	return implode('',$output);
    	}
     
     
     
    echo writeSomeText();
    ?>

    I'd like to have the HTML break and skip lines when you view the source, though, because it makes it easier for me to follow what my functions are doing.

    I tried going...


    Code PHP:
    <?php
    	$output[] = '<div>\n';
    	$output[] = '	<p>A Heading</p>\n';
    	$output[] = '	<p>Some text will go here...</p>\n';
    	$output[] = '</div>\n';
    ?>

    and...

    Code PHP:
    <?php
    	$output[] = '<div>'\n;
    	$output[] = '	<p>A Heading</p>'\n;
    	$output[] = '	<p>Some text will go here...</p>'\n;
    	$output[] = '</div>'\n;
    ?>

    ...but both of these return errors or a bunch of N's.

    Can someone suggest how I might achieve my desired effect ??

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The content of single-quoted strings is not parsed; the \n is printed literally instead of converted to an actual newline character. Enclose the strings in double quotes, or simply add the \n to the glue parameter in your implode() call so it's inserted between every item of the array automatically (but also enclose that in double quotes).

    I have to say, though, I've never seen someone use an array to build a string.

    PHP Code:
    function writeSomeText() {
        
    $output  "<div>\n";
        
    $output .= "    <p>A Heading</p>\n";
        
    $output .= "    <p>Some text will go here...</p>\n";
        
    $output .= "</div>\n";

        return 
    $output;
    }

    echo 
    writeSomeText(); 

  3. #3
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,727
    Mentioned
    43 Post(s)
    Tagged
    0 Thread(s)
    haha... Yeah it's something I'm trying and I intend to make it my standard.

    This was all because of some if statements once upon a time...


    Code PHP:
    	if($product_ShowDetails == '1')
    		{
    		$output[] = '<a href="#">Click Here</a>';
    		}

    ...so for uniformity I started doing that throughout.



    Thanks for the reminder about the double quotes... I don't believe I forgot about that.

    It works now.

  4. #4
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,727
    Mentioned
    43 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    <?php
    function writeSomeText()
        {
        $output[] = '<div>';
        $output[] = '   <h1>A Heading</h1>';
        $output[] = '   <p>Some text will go here...</p>';
        $output[] = '</div>';
     
        return implode("\n",$output);
        }
     
     
    echo writeSomeText();
    ?>


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
  •