Jump to content


Best Practice on PHP syntax


6 replies to this topic

#1 artcoder

    Young Padawan

  • Members
  • Pip
  • 109 posts
  • Location:Planet Earth
  • Interests:Collecting videos at FunAndEducationalVideos.com

Posted 25 September 2006 - 12:26 PM

There are many way to do the same thing in PHP. Which of the following should I use (according to best practices) and why? Or is it simply a matter of personal preference. I want to start getting into good habits.

1)
<?= $desc ?>

2)
<? echo $desc ?>

3)
<? print $desc ?>

4)
<?php print $desc>

Big question is, should I use ...

<?  ?>

or

<?php  ?>


#2 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 25 September 2006 - 12:38 PM

Question 1:
<?php echo $desc ?>

Why? The "=" technique is a optional extra, and while most servers are configured to use it, you must always consider the option it may not be. Why use echo? Because it is around 50% faster than print (from memory). Why use full php opening tags? Read below :P

Question 2:
<?php  ?>

Why? Because php.net says so, enough said lol.

Edited by Matthew., 25 September 2006 - 12:39 PM.


#3 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 25 September 2006 - 02:04 PM

Quote

Question 2:
CODE
<?php ?>


Why? Because php.net says so, enough said lol.


I didn't know that. I always use <? - guess I better chance my habits ^_^

#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 25 September 2006 - 03:33 PM

Mostly is just personal preferenced, but echo does work faster than print in most cases.

And to pick up where Matt left off, the reason you should use full <?php ?> tags is because some servers aren't configured for the <? ?> shortcut. Also, I believe I read somewhere that the <? ?> tags can also be used for ASP, or some other language, and thus would confuse the parser or something.

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 26 September 2006 - 12:00 AM

The perks of knowing the php manual like the back of my hand...
http://php.net/manua....short-open-tag

As for the print/echo thing. It's a VERY common question, google will basically yield the same results no matter what you search for (i found one really good article once, but i can't remember where it was). The basic concensus is that echo is faster than print. Unless you're creating some really huge script, or have a very popular site (i.e. p2l), then it technically doesn't matter which you use (i prefer echo).

#6 artcoder

    Young Padawan

  • Members
  • Pip
  • 109 posts
  • Location:Planet Earth
  • Interests:Collecting videos at FunAndEducationalVideos.com

Posted 26 September 2006 - 08:34 AM

Great replies. So I'll get into the habit of using "echo" and "php". Thanks.

#7 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 26 September 2006 - 04:29 PM

Ah, I was close! It was XML. >.<
ASP was completely different, lol.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users