jseddon001 Posted August 16, 2006 Share Posted August 16, 2006 Hello;This is my first post, and the question may be a little obvious.I am designing and coding my first website - a completely intereactive php mysql website. Every page uses the databse in some way and is scripted in php.When coding webpages in php, what are the the standards for [b]echo[/b] and [b]print[/b] lines that will appear as the html in the source code? I am currently writing it as this.echo "<div><div> asdf asdf asdf </div><h1>asdf asd f</h1><p>asdf asdf </p></div>":Basically, I open an echo tag and put all the coding in between and then close the echo tag.If there is a php variable to include I just include it as echo "<div><div> asdf asdf asdf </div><h1>asdf asd f</h1><p>asdf asdf my php variable [b]$variable[/b]</p></div>":This seems to work on the browser and doesn't affect the output.I read that there are specific ways to write the echo lines in php using ". or '. or "'. etc for variables and \n. for line breaks etc.The current method I'm using works, but I want to make sure that the website is properly coded.Please could someone provide a quick run down of the proper php coding when it comes to using "echo" in amongst the php code.It would be greatly appreciatedThanks very muchJon Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/ Share on other sites More sharing options...
effigy Posted August 16, 2006 Share Posted August 16, 2006 If there is a lot of non-PHP to output, get out of the php tags and re-enter:[code]<?php ... ?>lots of html goes here...<?php ... ?>[/code]If you need to insert variables into large amounts of HTML, use heredoc:[code]<?php echo <<<OUT ...lots of HTML, plus a $variable...OUT;?>[/code]For small things use echo, not print. echo is less characters to type and store, and it does not return a value like print.If you're echoing a lot of stuff, use commas instead of dots. dots require string conversion and creation, while commas simply separate the arguments to echo. Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75747 Share on other sites More sharing options...
SharkBait Posted August 16, 2006 Share Posted August 16, 2006 I use echo as well.[code]<?php$name = "SharkBait";echo "This is my text line, my name is {$name}";?>[/code]I use curley braces around my varibles when I use an echo. Helps me seperate my variables from the actual text. If I need a function within the echo I then have to do this:[code]<?phpecho "My function will return ". myFunction(19) ."";?>[/code]Hope that helps a bit. Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75748 Share on other sites More sharing options...
trq Posted August 16, 2006 Share Posted August 16, 2006 Its all personal taste. If the way your doing it works and your comfortable using it, its fine. Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75749 Share on other sites More sharing options...
GingerRobot Posted August 16, 2006 Share Posted August 16, 2006 Also, if you are echoing all html, e.g. no variables, then it is better to enclose the echo in single quotes: echo 'html goes here but no variables';This is because PHP does not attempt to parse any contents inside of the singe quotes and is therefore quicker. Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75750 Share on other sites More sharing options...
SharkBait Posted August 16, 2006 Share Posted August 16, 2006 Gingerroot means something like this:[code]<?phpecho '<div style="color: #fff;"> My Section </div>';// Whereas with " you need to escape themecho "<div style=\"color: #fff;\"> My Section </div>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75753 Share on other sites More sharing options...
jseddon001 Posted August 16, 2006 Author Share Posted August 16, 2006 Thanks very much for your help.Just replying to the previous post.is the difference between:- echo "<div style=\"margin:0 auto\">";andecho '<div style="margin:0 auto">';just the extra two bytes of info with the backslashes, or can it affect the output?I currently escape everything with a \.As another quick query, When the server reads the webpage and parses the php and echo statements will using . instead of , and and " insteadof ' affect the output of the page or just the speed at which the page is displayed? Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75755 Share on other sites More sharing options...
Ninjakreborn Posted August 16, 2006 Share Posted August 16, 2006 Also you don't code php, it's not a coding language, you program in php. Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75756 Share on other sites More sharing options...
czambran Posted August 16, 2006 Share Posted August 16, 2006 The following two links have the answers:http://php.net/manual/en/language.types.string.phphttp://us3.php.net/manual/en/function.echo.php Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75759 Share on other sites More sharing options...
jseddon001 Posted August 16, 2006 Author Share Posted August 16, 2006 Learning lots thank you. Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75760 Share on other sites More sharing options...
effigy Posted August 16, 2006 Share Posted August 16, 2006 See the user notes [url=http://us3.php.net/manual/en/language.operators.string.php]here[/url] for more information on concatenation (.). Not having to escape ' and " is more for clean coding than speed, although, if you don't use the right tools for the right job, PHP will have to do more work. As aforementioned, don't use "" for your echo when you don't need to interpolate variables; however, if you're outputing a string that has a lot of single quotes, use [tt]"'''''"[/tt] as opposed to [tt]'\'\'\'\'\''[/tt]. If it's a toss up between speed and clean, self-documenting code, I usually go with the latter.You can also think of the . v.s. , this way: If you are echoing strings, do you think it is best to create one large string, then pass it to echo, or pass the parts to echo and let it do its work? This [tt]echo 'My variable x is ' . $x . "\n";[/tt] says put together these three strings, then send them to echo; while this [tt]echo 'My variable x is ', $x, "\n";[/tt] says "Hey, send these pieces to echo." Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75763 Share on other sites More sharing options...
Daniel0 Posted August 16, 2006 Share Posted August 16, 2006 [quote author=jseddon001 link=topic=104511.msg416888#msg416888 date=1155744444]and \n. for line breaks etc.[/quote]A little more explanation:\n = line feed\t = tab\r = carriage returnWindows uses \r\n for new lines.Linux uses \n for new lines.Mac uses \r for new lines. Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-75778 Share on other sites More sharing options...
jseddon001 Posted August 17, 2006 Author Share Posted August 17, 2006 Thank you very much for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/17752-coding-php/#findComment-76454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.