Jump to content

Archived

This topic is now archived and is closed to further replies.

jseddon001

Coding php

Recommended Posts

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 appreciated

Thanks very much

Jon

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]
<?php

echo "My function will return ". myFunction(19) ."";
?>
[/code]

Hope that helps a bit.

Share this post


Link to post
Share on other sites
Its all personal taste. If the way your doing it works and your comfortable using it, its fine.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites


Gingerroot means something like this:

[code]
<?php

echo '<div style="color: #fff;"> My Section </div>';

// Whereas with " you need to escape them

echo "<div style=\"color: #fff;\"> My Section </div>";

?>
[/code]

Share this post


Link to post
Share on other sites
Thanks very much for your help.

Just replying to the previous post.

is the difference between:-
echo "<div style=\"margin:0 auto\">";
and
echo '<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 " instead
of '  affect the output of the page or just the speed at which the page is displayed?

Share this post


Link to post
Share on other sites
The following two links have the answers:
http://php.net/manual/en/language.types.string.php
http://us3.php.net/manual/en/function.echo.php

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
[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 return

Windows uses \r\n for new lines.
Linux uses \n for new lines.
Mac uses \r for new lines.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.