Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder Karen S. Garvin's Avatar
    Join Date
    Nov 2006
    Location
    Maryland
    Posts
    227
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Line breaks don't work as expected

    Hello, I am new to PHP. I wrote this small bit of code for a college class, but the instructor offered no feedback. Our textbook offered nothing and the teach yourself book I bought had typos in it.

    I don't understand why the linebreaks in the php code didn't work properly. I ended up putting them at the beginnings of the next line, rather than the end of the previous line. I think that's sloppy; I want to undertand why the line break worked on the first line but not subsequent lines. I expect it's something semantic, quotes or such.

    Please bear in mind I am a beginner with PHP.

    Here is the XHTML file:
    -------------------------


    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
    <title>Assignment 8 - PHP Script</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    
    <style type="text/css">
    <!-- The CDATA tag prevents the browser from reading the following CSS as XHTML -->
    /* <![CDATA[ */
    
    * {margin:0; padding:0;}
    h1 {font-size: 2em; font-weight:bold; color:#006600;}
    h2 {font-size: .75em; font-weight:bold; font-style:italic; color:#006600; padding-left: 150px;}
    h3 {font-size: .75em; font-weight:bold; font-style:italic; color:#006600; padding-left: 150px;}
    h4 {font-size: 1.1em; font-family:verdana, arial, sans-serif; font-weight:normal; font-style:normal; color:#006600; margin-top:.4em; margin-bottom:.6em; padding: .5em 1em 0 .8em;}
    
    p {font-size: .9em; font-family:verdana, arial, sans-serif; font-weight:normal; padding: 0 1em 0 1em;}
    
    body {background-color:ccffcc; margin:0px; padding:0px; font: .8em verdana,
    arial, sans-serif;}
    
    div#head {width:100%; background-color:#ccffcc; padding-left:60px; padding-top:15px; float:left;}
    
    div#quote {width:100%; float:left; margin-top:-100px; margin-left:40px;}
    
    div#contentarea {width:100%; background-color:#ccffcc; padding-top:2em;}
    
    div#col1 {width:60%; margin: 0 .75em 0 1em; background-color:#ccffcc; float:left;}
    
    div#col2 {width:30%; margin: 0 .5em 0 .5 em; padding: 1em;
    background-color:#ffff99; border-width: 4px; border-color:#cc0000; border-style:dotted;  float:left;}
    
    div#foot {width:100%; padding-top: 1em; padding-left: 4em; background-color:#ccffcc; border-top-width: 3px; border-top-color:#006600;
    border-top-style:dashed; }
    div#w3c {width:100%; background-color:#ccffcc; padding: 0 3em 0 4em;}
    div.clearfloats {clear:both;}
    
    /* ]]> */
    </style>
    </head>
    
    <body>
    
    <div id="head">
    <h1>PHP</h1>
    </div>
    
    
    <div id="contentarea">
    
    <div id="col1"><h4>What is PHP?</h4>
    <p>PHP is a server-side scripting language that returns plain HTML pages to the user. Because it runs on the web server, there are not the kinds of compatability problems with browsers that JavaScript may run into. In addition, a user cannot "turn off" PHP scripts the way they can disable JavaScript. PHP is not intended to replace JavaScript, but will work with it.<br /><br />
    
    PHP can do a lot, from web design to database access. PHP can be used to build a breadcrumb navigation trail, create dynamic navigation menus, create dynamic database access objects, load an Excel database, create login systems, and much more. In addition, PHP is open-source software, which means you can get it for free and start coding immediately.<br /><br />
    </p>
    
    <h4>What the Code Looks Like</h4>
    <p>The first line of a PHP document needs to be:<br /><br />
    
    &#35;&#33;&#47;usr&#47;local&#47;bin&#47;php<br /><br />
    
    Within the body of the document, the php script is called out like this:<br /><br />
    
    &#60;&#63;php <br /><br />
    and ends like this<br />
    <br />
    ?&#62;<br /><br />
    
    
    Here's my entire code:<br /><br />
    
    
    &#60;&#63;php <br />
    
    &#47;*
    php date script 
    *&#47;<br />
    
    
    $txt1="Welcome to Karen Garvin's PHP page.&#60;br&#62;&#60;br&#62;"&#59;<br />
    
    $txt2="Today's date is " . date("l, F j, Y.")&#59;<br />
    
    $txt3="&#60;br&#62;&#60;br&#62;The current time is " . date("g:i:s a.")&#59;<br />
    
    $txt4="&#60;br&#62;&#60;br&#62;Have a Nice Day!"&#59;<br />
    
    
    echo $txt1&#59;<br />
    
    echo $txt2&#59;<br />
    
    echo $txt3&#59;<br />
    
    echo $txt4&#59;<br />
    
    ?><br /><br />
    
    </p>
    <p>I had some issues with line breaks and could only get them to work properly on the $txt1= line. PHP didn't seem to like &#60;br /&#62; so I used &#60;br&#62; instead. Since I couldn't get the breaks to work at the end of the code lines, I placed them at the beginning of the next line of code. Perhaps not elegant coding, but it works.
    </p>
    </div>
    
    
    
    <div id="col2"><p><a href="http://nova.umuc.edu/cgi-bin/cgiwrap/~ct386a21/php8.php">Want to see the code in action?</a><br />
    </p>
    </div>
    
    <div class="clearfloats"></div>
    </div> <!-- end of content area-->
    
    
    <div id="foot">
    <a href="http://nova.umuc.edu/~ct386a21/vectoring.html">Return to vectoring page</a><br />
    <br />
    <script type="text/javascript">
    <!-- Engage Cloaking Device
    var date = new Date(document.lastModified);
    document.write("Last Update: " + date);
    //  Drop Cloak -->
    </script><br /><br />
    </div>
    
    <!-- XHTML sticker here -->
    <div id="w3c">
    
    </div>
    
    
    </body>
    </html>
    and here is the php code:

    PHP Code:
    #!/usr/local/bin/php
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <title> Karen's PHP Page </title>
    <meta name="Generator" content="Alleycode HTML Editor" />
    <meta name="Keywords" content="PHP Assignment 8" />
    </head>
    <body>
    <?php
    /*
    php date script 
    */

    $txt1="Welcome to Karen Garvin's PHP page.<br><br>";
    $txt2="Today's date is " date("l, F j, Y.");
    $txt3="<br><br>The current time is " date("g:i:s a.");
    $txt4="<br><br>Have a Nice Day!";

    echo 
    $txt1;
    echo 
    $txt2;
    echo 
    $txt3;
    echo 
    $txt4;
    ?>

    </body>
    </html>
    ----------------------------------------------
    Karen S. Garvin
    http://ksgarvin.wordpress.com

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    so what did you have originally? This should be working fine
    PHP Code:
    <?php
    /*
    php date script 
    */

    $txt1 "Welcome to Karen Garvin's PHP page.<br />";
    $txt2 "Today's date is " date("l, F j, Y.") . "<br />";
    $txt3 "The current time is " date("g:i:s a.") . "<br />";
    $txt4 "Have a Nice Day!";

    echo 
    $txt1;
    echo 
    $txt2;
    echo 
    $txt3;
    echo 
    $txt4;
    ?>
    Usually one would add a newline character in addition to the <br />. This doesn't really affect the rendering in your browser, but only for better appearance when you choose to view the source:
    PHP Code:
    <?php
    /*
    php date script 
    */

    $txt1 "Welcome to Karen Garvin's PHP page.<br />\n";
    $txt2 "Today's date is " date("l, F j, Y.") . "<br />\n";
    $txt3 "The current time is " date("g:i:s a.") . "<br />\n";
    $txt4 "Have a Nice Day!\n";

    echo 
    $txt1;
    echo 
    $txt2;
    echo 
    $txt3;
    echo 
    $txt4;
    ?>


  •  

    Posting Permissions

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