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 13 of 13
  1. #1
    ivy
    ivy is offline
    Regular Coder
    Join Date
    Jun 2002
    Location
    London / Glasgow / Liverpool
    Posts
    177
    Thanks
    1
    Thanked 0 Times in 0 Posts

    php echoed code in view source all on one line - why?

    Hi there

    I have been 'playing' with php code for some time now and feel that I am doing OK...

    However, my problem is this.

    I have php pages, e.g. index.php etc...

    I include within these pages php scripts.

    The included php scripts echo out results from, for example, a mysql query.

    When I view source of the index.php page, the echoed results from the included script are all on one line!


    When I look at other web pages that clearly use php scripts within them, the page source is nicely formatted (and not all on one line!)



    Where am I going wrong? ...in the php script?

    - Just something that has bugged me for a while now and I just can't figure it out!

    Thanks!
    See ya

    Ivy.

    MAC Codes

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    This special char means new line in text editros \n, while <br /> is only new line in HTML

    PHP Code:
    echo "this is still in first line\nand this goes to second line"
    if you do a view source on this you will se this line in two lines, but in browser you will se only one line.. you can do this also

    PHP Code:
    echo "this is still in first line<br />\nand this goes to second line"
    Now you will get new lines both in browser and view source

  • #3
    ivy
    ivy is offline
    Regular Coder
    Join Date
    Jun 2002
    Location
    London / Glasgow / Liverpool
    Posts
    177
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi matak

    Excellent!

    I knew about the new line character, but did not think it applied in this scenario!

    Thing is, I use single quotes in most of my php, e.g.

    PHP Code:
    echo 'Hello World'
    So i then have to make it:

    PHP Code:
    echo 'Hello World'."\n"
    or else change all single quotes to double quotes!

    So that I can conform a little better, should I start to use double quotes all the time so that I can add a line break at the end of each echo? Or, does it really matter that the code isn't particularly well formatted on theview source?

    Just wondering what you and others do?

    Thank you for enlightening me!
    See ya

    Ivy.

    MAC Codes

  • #4
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Personaly i think single quote is a bad habit , and you should always use double quotes for echoing, and everything. Yes it's annoying when you have to escape special chars like " when you write html, but you can always use Replace All, which almost every text editor has (just write html in separate file, use replace all " with \" and paste it to your script).

    If you use single quotes then this is the proper solution

    PHP Code:
    echo 'this is still in first'."\n".'line and this goes to second line'

  • #5
    ivy
    ivy is offline
    Regular Coder
    Join Date
    Jun 2002
    Location
    London / Glasgow / Liverpool
    Posts
    177
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you! I shall ponder over this for a while!
    See ya

    Ivy.

    MAC Codes

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Personally i think single quote is a bad habit
    Would you share why with us ?
    I'm living in a French speaking area, and th single quotes is used in the "every day" language here.
    I admit that I tend to use more ' than ", but more because I don't need to hands to compose it (Swiss-French keyboard is shift+2 for " and shift+[next to 0] for ' )

    As I type with my ten fingers, I usually use more my right hand, thus the ' is simpler, faster and more "comfy".

  • #7
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    I had oportunity to use French keyboard, and i agree, it's rather silly. Especialy using shift+number for writing numbers..lol

    Also that was my personal opinion of using " instead of '. If you disagree, then ok, it's not gonna change my opinion about it, couse it's personal opinion based on my habbit of coding
    Last edited by matak; 08-07-2007 at 04:05 PM.

  • #8
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Double quotes is also better IMO because it will parse variables automatically.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #9
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    It all depends on what you're outputting. When you're using special characters in your echo, it's easier to use single quotes so that they are taken as literal. Also, I find that placing variables directly in double quotes can be a bit misleading while reading the code and also sometimes the PHP processor will interpret incorrectly.

    Example:
    PHP Code:
    echo  'My file path: C:\Documents and Settings\sjackson\Desktop'
    Using double quotes above would force you to escape all the backslashes and make it less readable.

    Another example:
    PHP Code:
    echo "You have lots of $items in your cart"
    Now, if you had two variables, one called $item and the other called $items, which would be used? You're running a risk of the processor using the wrong variable. Also, arrays often do not work when put inside double quotes, so you have to stop the flow anyway.

    Anyway, I believe using single quotes or double quotes all depends on what information you are dealing with. Single quotes has the distinct advantage of making sure all your characters are taken literally.

    Also, I think putting your variables outside of your quotes is a far better practice since it makes sure the variable is used correctly and it's more readable. The only downside is that it makes the code a bit more lengthier, but the advantages heavily outweigh that one downside.

    -Shane

  • #10
    ivy
    ivy is offline
    Regular Coder
    Join Date
    Jun 2002
    Location
    London / Glasgow / Liverpool
    Posts
    177
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey thanks for you comments - glad i got a discussion going on this...

    I personally use single quotes for the same reasons as TheShaner - I can read the code far more easily when variables are outside the echo. And there is no escaping to do - however, as per my original post here, it does mean that you do not get a pretty view cource unless you add the
    PHP Code:
    ."\n"
    to the end.

    It's good to hear that people use the way that suits them - I'm not breaking any conventions then!!!
    See ya

    Ivy.

    MAC Codes

  • #11
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,025
    Thanks
    2
    Thanked 314 Times in 306 Posts
    Go with the method that works best for you. I prefer going with whatever is the least complex/least number of different elements. Typing a mix of single-quoted/concatenated/double-quoted string, as in the case of 'some string' ."\n" is definitely more work than "some string\n".

    Interestingly, there is a current thread, where a complex quoted/concatenated line of code contained a syntax error - Problem with my code... that would have been avoided had the least complex syntax been used -

    Which would you rather type, read, and troubleshoot, this -
    PHP Code:
    $message "From: " $FirstName " \n\n Comment:  " $Comment "\n\n Contact Information: \n\n" .  $Name "\n" $Email "\n" $Phone " \n\n" $Street1 " " $Street2 "\n" $City " " $State " " $Zip " \n\n"
    or this -
    PHP Code:
    $message "From: $FirstName\n\nComment: $Comment\n\nContact Information:\n\n$Name\n$Email\n$Phone\n\n$Street1 $Street2\n$City $State $Zip\n\n"
    This is a similar issue to switching out of PHP mode to output a little HTML which contains some PHP and repeatedly switching into and out of PHP to echo variables when it is much simpler and less error prone (from a syntax typing standpoint) to just stay in PHP mode and have any HTML as part of the string along with any variables you are echoing.

    Go with the simplest syntax that works in the context of what you are doing at the time.

    Edit: The only time I break out of a string and use concatenation within one string is if I use PHP functions inline in the string to produce parts of the string. When just embedding PHP variables in a string, I always use a double-quoted string.
    Last edited by CFMaBiSmAd; 08-07-2007 at 07:50 PM. Reason: fixed wording
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #12
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    The proper way to get nicely formatted source is to use a template engine so you can write the HTML naturally instead of piecing it together with 'echo's.

  • #13
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    I don't know if this is a good practice or not, but you could also just use normal new lines in your PHP echo. For example:
    PHP Code:
    echo 'This is text.
    I skipped a line without typing "\n".'

    And I prefer single quotes. Not just because it's easier/faster (as opposed to shift + quotation mark), but also because I don't have to escape anything. I also keep variables outside quotes. And when I do have to use a new line or tab character, I double quote that and still single quote everything else. I single quote programming (JS and PHP) and double quote other coding (HTML and CSS). Obviously XHTML requires double quotes, but I do that and single quote PHP because I find that it's a lot easier.

    It's just personal preference. Everyone has their own style.
    Last edited by thesmart1; 08-09-2007 at 11:54 PM.


  •  

    Posting Permissions

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