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!
08-07-2007, 12:46 PM
This special char means new line in text editros \n, while <br /> is only new line in HTML
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
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
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.
echo 'Hello World';
So i then have to make it:
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!
08-07-2007, 02:06 PM
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
echo 'this is still in first'."\n".'line and this goes to second line';
Thank you! I shall ponder over this for a while!
08-07-2007, 04:14 PM
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".
08-07-2007, 05:02 PM
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
08-07-2007, 05:09 PM
Double quotes is also better IMO because it will parse variables automatically.
08-07-2007, 06:08 PM
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.
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.
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.
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 ."\n"; to the end.
It's good to hear that people use the way that suits them - I'm not breaking any conventions then!!!
08-07-2007, 08:42 PM
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 - http://www.codingforums.com/showthread.php?t=120818 that would have been avoided had the least complex syntax been used -
Which would you rather type, read, and troubleshoot, this -
$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 -
$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.
ralph l mayo
08-08-2007, 12:27 AM
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.
08-09-2007, 03:36 AM
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:
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.