Jump to content


Photo

Coding php


  • Please log in to reply
12 replies to this topic

#1 jseddon001

jseddon001
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationBeautiful sunny, crisp, and scenic leeds

Posted 16 August 2006 - 04:07 PM

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 echo and print 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 $variable</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



#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 16 August 2006 - 04:13 PM

If there is a lot of non-PHP to output, get out of the php tags and re-enter:
<?php ... ?>
lots of html goes here...
<?php ... ?>

If you need to insert variables into large amounts of HTML, use heredoc:
<?php
 echo <<<OUT
   ...lots of HTML, plus a $variable...
OUT;
?>

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.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 16 August 2006 - 04:14 PM

I use echo as well.

<?php
$name = "SharkBait";

echo "This is my text line, my name is {$name}";

?>

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:

<?php

echo "My function will return ". myFunction(19) ."";
?>

Hope that helps a bit.

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 August 2006 - 04:14 PM

Its all personal taste. If the way your doing it works and your comfortable using it, its fine.

#5 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 August 2006 - 04:16 PM

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.

#6 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 16 August 2006 - 04:23 PM



Gingerroot means something like this:

<?php

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

// Whereas with " you need to escape them

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

?>


#7 jseddon001

jseddon001
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationBeautiful sunny, crisp, and scenic leeds

Posted 16 August 2006 - 04:24 PM

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?

#8 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 16 August 2006 - 04:25 PM

Also you don't code php, it's not a coding language, you program in php.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#9 czambran

czambran
  • Staff Alumni
  • Advanced Member
  • 52 posts
  • LocationFairfield,IA

Posted 16 August 2006 - 04:28 PM

The following two links have the answers:
http://php.net/manua...ypes.string.php
http://us3.php.net/m...nction.echo.php
Chris
Using undefined variables in PHP is not an error, just sloppy coding practice. PHP5 Power Programming BOOK
--------------------------------------------------------------------------
PHP F.A.Q.
Linux User #394154

#10 jseddon001

jseddon001
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationBeautiful sunny, crisp, and scenic leeds

Posted 16 August 2006 - 04:30 PM

Learning lots thank you.


#11 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 16 August 2006 - 04:35 PM

See the user notes here 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 "'''''" as opposed to '\'\'\'\'\''. 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 echo 'My variable x is ' . $x . "\n"; says put together these three strings, then send them to echo; while this echo 'My variable x is ', $x, "\n"; says "Hey, send these pieces to echo."

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#12 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 August 2006 - 05:03 PM

and \n. for line breaks etc.


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.

#13 jseddon001

jseddon001
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationBeautiful sunny, crisp, and scenic leeds

Posted 17 August 2006 - 09:06 PM

Thank you very much for all your help.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users