Mixing HTML into PHP Classes

2 views
Skip to first unread message

Cecil Champenois

unread,
Aug 25, 2010, 2:20:36 PM8/25/10
to PHP Group
In the book, PHP and MySQL, the authors show code where HTML is inserted into their classes to build their headers and footers for each page throughout the sample web files.
 
I read somewhere that this is not a good idea, but I am not sure why.
 
Can someone explain if it is good or not to mix in HTML with PHP classes?
 
Cecil

Heath Aiken

unread,
Aug 25, 2010, 2:29:39 PM8/25/10
to professi...@googlegroups.com
Mixing HTML into PHP classes / files is a big no-no.  It used to be an accepted practice, but with the advent of MVC (and the tons of MVC frameworks), it became generally accepted practice to separate the two.  There are varying degrees to separating the two, but at the very least, you should not be outputting HTML from PHP. Think of PHP as the Model / Controller and the HTML as the View.  Your PHP should be completely unaware of HTML and only output values to be used within the HTML.

If you think of it from a developer (PHP) / designer (HTML / CSS) standpoint, you don't want your designer monkeying around in PHP classes to adjust mark-up.  Even if you are a one-man band, it's good to keep it separated.

It sounds like your book might be a little dated (also judging form the .inc question), so I would advise you to skim through to get the concepts and maybe look to more current material for a (well...) more current approach.

--Heath

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
 
For information or project assistance please visit :
http://www.360psg.com
 
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP

Robert Gonzalez

unread,
Aug 25, 2010, 2:32:47 PM8/25/10
to professi...@googlegroups.com
You're starting to get into design questions now, which is a good thing. However, a decision like this is often based on your overall architecture and how you build your apps.

For me, I tend to lean toward an MVC type architecture in most of my apps now, so all of my HTML lives in separate template files that are consumed by a front controller and receive their data from a model by way of a page controller. The only other place HTML lives in my code is in my view helpers, and only to the extent that a method needs it, like in the case of presenting an anchor tag to a view:

<?php
class View_Helper_Anchor extends View_Helper {
  public function anchor($url, $text) {
    return '<a href="' . $url . '">' . $text . '</a>';
  }
}
?>

I can't stand to see HTML sprinkled amidst my PHP unless it's in a view. But that is purely personal opinion speaking here.


Jack Timmons

unread,
Aug 25, 2010, 5:32:40 PM8/25/10
to professi...@googlegroups.com
On Wed, Aug 25, 2010 at 1:32 PM, Robert Gonzalez
<robert.anth...@gmail.com> wrote:
>> Can someone explain if it is good or not to mix in HTML with PHP classes?

Depends on the purpose of the class.

Outputting HTML? Yes.

Anything else? No, and you'll die in a bright fire the burns away
every pound of flesh as you scream "My eyes, why did my eyes go
first!?"

--
Jack Timmons
@_Codeacula
Feel free to contact me on GTalk.

David Dyess

unread,
Aug 26, 2010, 2:12:59 AM8/26/10
to professi...@googlegroups.com
There is technically nothing wrong with mixing PHP and HTML, it was what PHP was intended to do and eventually does do no matter how you code it.   <--- Yup, I said that

If you look at an old PHP project, you will likely notice very few templates and HTML is mixed with PHP throughout.  The main reason almost everyone has moved to a pattern that separates HTML from PHP is maintainability. It's easier to fix something that isn't showing up correctly in your HTML by going through templates and mainly seeing HTML. Same goes for fixing something in PHP classes, it's cleaner and far better reading. Granted, some have taken it to the extreme, but that's a different question altogether ;) 

In the end, though, you are still mixing PHP and HTML, they are just in different files or some how separated by design until they merged together via include or return, etc.

Jack Timmons

unread,
Aug 26, 2010, 7:15:19 AM8/26/10
to professi...@googlegroups.com
On Thu, Aug 26, 2010 at 1:12 AM, David Dyess <david...@gmail.com> wrote:
> There is technically nothing wrong with mixing PHP and HTML, it was what PHP
> was intended to do and eventually does do no matter how you code it.   <---
> Yup, I said that

Kathy Lee Gifford will find you tonight to devour your soul.

> If you look at an old PHP project, you will likely notice very few templates
> and HTML is mixed with PHP throughout.  The main reason almost everyone has
> moved to a pattern that separates HTML from PHP is maintainability. It's
> easier to fix something that isn't showing up correctly in your HTML by
> going through templates and mainly seeing HTML. Same goes for fixing
> something in PHP classes, it's cleaner and far better reading. Granted, some
> have taken it to the extreme, but that's a different question altogether ;)
> In the end, though, you are still mixing PHP and HTML, they are just in
> different files or some how separated by design until they merged together
> via include or return, etc.

Of course there's going to be HTML and PHP.

Should there be HTML in classes like the OP asked? I stand by my point.

I think you're going off in a different direction, though? I'm (and I
assume the OP) is strictly just talking about classes. Of course, I'm
also of the opinion there should be one class definition per file
(-maybe- two or three if the classes are all closely related, and I
mean like Siamese twins).

Javier Montani

unread,
Aug 26, 2010, 8:25:33 AM8/26/10
to professi...@googlegroups.com
I don't know if this is still an issue but in old php versions, if you mixed php and html, the server had to go back and forth through the corresponding interpreters causing performance issues.

2010/8/26 Jack Timmons <code...@gmail.com>

Javier Montani

unread,
Aug 26, 2010, 8:30:06 AM8/26/10
to professi...@googlegroups.com
Rober gave you a very good example on how to use it.
One I use to explain the difference to my students is the following:

Don't do this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title>
   <?php echo 'TITLE'; ?>
   </title>

  </head>
  <body>

  </body>
</html>


Do this:
<?php
echo '

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title>
  TITLE
  </title
>
  </head>
  <body>

  </body>
</html>
';
?>


2010/8/26 Javier Montani <jmon...@gmail.com>

Jack Timmons

unread,
Aug 26, 2010, 8:38:30 AM8/26/10
to professi...@googlegroups.com
On Thu, Aug 26, 2010 at 7:30 AM, Javier Montani <jmon...@gmail.com> wrote:
> Do this:
> <?php
> echo '
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
>   <head>
>   <meta http-equiv="content-type" content="text/html; charset=windows-1250">
>   <meta name="generator" content="PSPad editor, www.pspad.com">
>   <title>
>   TITLE
>   </title>
>   </head>
>   <body>
>
>   </body>
> </html>
> ';
> ?>

That is what I hate. I hate it.

Do you want to know why?

Because now you're going to have to escape every ' in that echo statement.

The first method is much more preferable.

Javier Montani

unread,
Aug 26, 2010, 9:02:16 AM8/26/10
to professi...@googlegroups.com
I know, but this is just a simple example.
Model-View-Controller is the best technology you can use in these cases.
If you want to learn more, download Sitellite (www.sitellite.org). It's a CMS based on php, mysql and MVC technology.

2010/8/26 Jack Timmons <code...@gmail.com>

Heath Aiken

unread,
Aug 26, 2010, 9:16:28 AM8/26/10
to professi...@googlegroups.com
Javier, are you sure you didn't get your "do this" / "don't do this" reversed?  At 8:30AM EST, I swore I felt disturbance in the force...

I'm going to assume that you mixed to two up because it will help me get through my day easier, but in case you didn't, for the sake of your students: http://en.wikipedia.org/wiki/PHP_syntax_and_semantics

The article really explains it better than I could.

--Heath

Cecil Champenois

unread,
Aug 26, 2010, 9:19:24 AM8/26/10
to professi...@googlegroups.com
Is there any real consensus to separate HTML and PHP code, meaning to not make HTML templates that are called as PHP classes?
 
I am hearing conflicting "opinions".
 
Personally, I like the idea of separating the HTML and PHP. However, it's also kind of cool having a PHP class handle all the HTML details. Not having had a lot of experience in this, I look for others to present their arguments one way or the other.
 
I do like the MVC idea too.
 
Cecil



From: David Dyess <david...@gmail.com>
To: professi...@googlegroups.com
Sent: Wed, August 25, 2010 11:12:59 PM
Subject: Re: [Pro. PHP Dev.] Mixing HTML into PHP Classes

Jack Timmons

unread,
Aug 26, 2010, 9:34:23 AM8/26/10
to professi...@googlegroups.com
On Thu, Aug 26, 2010 at 8:16 AM, Heath Aiken <heath...@gmail.com> wrote:
> Javier, are you sure you didn't get your "do this" / "don't do this"
> reversed?

That would explain the whole thing.

Heath Aiken

unread,
Aug 26, 2010, 9:47:00 AM8/26/10
to professi...@googlegroups.com
Let us set MVC aside because it is really a design pattern that also happens to utilize (if you do it correctly) the best of code separation fundamentals.

I think that when you keep saying "...having a PHP class handle all of the HTML details..." you are sending folks into a tizzy.  The very idea of OOP in PHP is a relatively recent (in the last few years) evolution from "procedural" code, which is where the root of the mixing HTML and PHP argument comes from.  I think that when most PHP folks utilize OOP, they have already accepted and embraced the concept of completely separating PHP from HTML. If that book that you've been reading is preaching handling HTML within PHP classes, you should burn it (dramatic I know, but what if a child comes along and picks it up, do you want that on your conscience?).

When you talk about handling HTML output from within PHP classes are you talking about actually building chunks of HTML within the class, assigning it to a variable, and then outputting it? (*shudders*)

Example:
--------------------------------------------------------------
//wrongway.php
Class WrongWay {

public function output(){
  $html = "<........>";
  return $html;
}
}//class end
--------------------------------------------------------------
//page.php
<?php require_once('wrongway.php');
<?php $page = new WrongWay; ?>

<?php echo $page->output(); ?>
---------------------------------------------------------------

Or are you simply talking about putting HTML into a file with a PHP extension (not an actual PHP class)?

If you go to the link I provided earlier: http://en.wikipedia.org/wiki/PHP_syntax_and_semantics

And you see the code: 


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
 <title><?=$page_title;?></title>
 </head>
 <body>
 <p>Hello</p>
 </body>
</html>



They don't explain that this is likely in a .php file, not an .html file, but this is not a php class.



Maybe I'm misunderstanding?

--Heath

Javier Montani

unread,
Aug 26, 2010, 9:50:22 AM8/26/10
to professi...@googlegroups.com
I double-checked and I wrote it right. But is up to you if you want to mix html and php or write everything under php using the method I purposed.
I personally like parsing templates files (lets say html) from php as Sitellite do.


2010/8/26 Jack Timmons <code...@gmail.com>

Jack Timmons

unread,
Aug 26, 2010, 10:00:33 AM8/26/10
to professi...@googlegroups.com
On Thu, Aug 26, 2010 at 8:50 AM, Javier Montani <jmon...@gmail.com> wrote:
> I double-checked and I wrote it right. But is up to you if you want to mix
> html and php or write everything under php using the method I purposed.
> I personally like parsing templates files (lets say html) from php as
> Sitellite do.

Example of why I hate using echos like that in PHP:

http://pastebin.com/FP6L8Mdz

Heath Aiken

unread,
Aug 26, 2010, 10:00:29 AM8/26/10
to professi...@googlegroups.com
Okay, not trying to pick a fight (we're all friends here, right?), but if you have a personal preference for forcing the PHP interpreter to parse HTML, that's fine; it's your prerogative.  My question is how do you justify it to your students?  I mean, you teach them your way by saying "do this, don't do that", how do you justify your stance?  Furthermore, what happens when you students are invariably unleashed upon the world and they go out looking for PHP jobs?  I don't hire PHP programmers at this point, but if I did I wouldn't hire one that thought it was okay to parse HTML (in 2010).

--Heath

Heath Aiken

unread,
Aug 26, 2010, 10:03:42 AM8/26/10
to professi...@googlegroups.com
Heh, I just had a nightmare flashback of sifting through echo statements trying to figure out where I screwed up a quote...  ugh...

--Heath


Jack Timmons

unread,
Aug 26, 2010, 10:07:33 AM8/26/10
to professi...@googlegroups.com
On Thu, Aug 26, 2010 at 9:03 AM, Heath Aiken <heath...@gmail.com> wrote:
> Heh, I just had a nightmare flashback of sifting through echo statements
> trying to figure out where I screwed up a quote...  ugh...
>
> --Heath

That's live code.

I shit thee not.

Cecil:
Do what you think is best. Honestly, the best way to do PHP is in a
way that gets the job done so you get paid.

Unless you're like me and have that annoying perfectionist voice
screaming in your head when you're half-assing it.

Javier Montani

unread,
Aug 26, 2010, 10:30:34 AM8/26/10
to professi...@googlegroups.com
I think I was misunderstood.
Take a look here because this is what I meant [ Pay special attention to line 30: echo template_simple ('list.spt'$list);  ]

http://simian.dojolearning.com/course-contents-action/id.114/chapter.6



2010/8/26 Jack Timmons <code...@gmail.com>

bradleylamar

unread,
Aug 26, 2010, 12:16:04 PM8/26/10
to Professional PHP Developers
Javier, you're very wrong about this. Server's don't interpret html at
all. They just serve it up and the clients web browser interprets it.
But if apache knows to use the file extension index.php as PHP then
the entire file will be ran through the php parser. You are also
completely backwards on your example as several people pointed out.
I'm not sure who you're teaching, but you might need to go back and
correct that.

Cecil, the advice Robert, Jack, & Heath have given given here is sound
for real world php development. Lots of projects use a templating
engine to help separate html from php. I've used Smarty (http://
www.smarty.net/) on a couple large projects, and it works fairly well.
Ultimately if this is a job where you're a one man crew and doing
everything yourself, it's not really a big deal. But if you're working
in a large team, where you have roles split out for programmers,
developers, and designers then it really matters. I wouldn't ever
really want a designer seeing/messing with my php code. So with
something like smarty, I can do all my coding and assign variables to
the smarty engine in my controller that a developer/designer can then
use in their html templates.


On Aug 26, 7:25 am, Javier Montani <jmont...@gmail.com> wrote:
> I don't know if this is still an issue but in old php versions, if you mixed
> php and html, the server had to go back and forth through the corresponding
> interpreters causing performance issues.
>
> 2010/8/26 Jack Timmons <codeac...@gmail.com>
>
>
>
> > On Thu, Aug 26, 2010 at 1:12 AM, David Dyess <david.dy...@gmail.com>

Javier Montani

unread,
Aug 26, 2010, 12:29:10 PM8/26/10
to professi...@googlegroups.com
OK guys.... [Pro. PHP Dev.] group 1..... Javier....0

As I always say..."It's better to be wrong with the majority than to be right alone" !!!

;)




2010/8/26 bradleylamar <bradle...@gmail.com>

Robert Gonzalez

unread,
Aug 26, 2010, 12:35:16 PM8/26/10
to professi...@googlegroups.com
That raises a whole new issue of using templates that are developed in an entirely different language (like Smarty for example). That raises a whole new layer of complexity and performance drag that in my opinion is totally not worth it.

The truth is you can create very simple systems that allow you to keep your presentation and business logic separate, even if you don't want to use a full blown MVC solution. As a simple example, you could easily set up something like (assume a two page site for now, for simplicity):

/
index.php
controllers/
  home.php
  about.php
  notfound.php
models/
views/
  default/
    home.php
    about.php
    notfound.php

Your index.php file could act as a simple bootstrap/route/dispatch handler using GET requests (a la a simple URL rewriter):
<?php
// Bootstrap file: index.php

// Get our requested page
$page = !empty($_GET['p']) ? strtolower($_GET['p']) : 'home';
$file = $page . '.php';

// Now see if we have a controller and a view for it
if (!canRender($file)) {
  // Either a controller or view, or both, is missing, so try a not found page
  $nf = 'notfound.php';
  // No not found, so we need to die hard here
  if (!canRender($nf)) {
    die('Neither the requested page ' . $page .' nor an acceptable error page could be loaded.');
  }
  // Reset the requested page file to the not found file
  $file = $nf;
}

// Bring in both the controller and the view for this request
// The controller sets variables and values...
require_once 'controllers/' . $file;

// The view renders them. ALL HTML GOES HERE
require_once 'views/default/' . $file;

function canRender($file) {
  return file_exists('controllers/' . $file) && file_exists('views/default/' . $file);
}
?>

Once your bootstrap is in place all you need to do is create your code to handle each individual request. In testing this out, I started with the notfound page:
<?php
// Not found page, for pages requested that don't exist
// Since there is nothing for this page to do we don't have any code here

Then built the not found view:
<html>
<head>
<title>Page Not Found</title>
</head>
<body>
<h1>Page not found</h1>
<p>The page you requested, <?php echo $page ?>, could not be found.</p>
</body>
</html>

I loaded this setup in my browser and lo and behold, I had a site. Just like that! With all of my PHP logic and presentation separated. Just like that! All you would have to do from here is create your HOME page and view, the ABOUT page and view then any other page/view pairs you want.

Yes, it is that easy. No, this isn't even remotely close to a good way to do it. But it can be done as simple as this. And with the presentation separated from the business logic.

And for the record, that whole issue of using PHP to echo enormous blocks of HTML wrapping variables, like in the "DO IT THIS WAY" example... reminds me of my earliest code I wrote in 2002. The "DON'T DO IT THIS WAY" example is how all of my code looks now. Hmmm.

And before you ask about throwing a DEFAULT in the view path, I did that for the cases where you may want to load different templates based on user agent or things like that. This decision would be in the bootstrap file so as to encapsulate those things that change. Wait, did I just use an Object Oriented Programming mantra?

David Dyess

unread,
Aug 26, 2010, 1:02:35 PM8/26/10
to professi...@googlegroups.com
Haha...   I set you up on that one ;)   I know the OP was asking about classes, but I didn't want to just say "No! never do that!" and leave it at that. I was trying to explain why I wouldn't. Now I see I wasn't very clear, but I was trying to show there is a difference between having PHP within HTML and having HTML within PHP (as in the echoed stuff Javier posted).

I hope Kathy Lee can dodge 45 cal bullets...because I still stand by my previous statement there is nothing TECHNICALLY wrong with mixing PHP and HTML.  Do what works for the job at hand.

Jack Timmons

unread,
Aug 26, 2010, 1:29:55 PM8/26/10
to professi...@googlegroups.com
On Thu, Aug 26, 2010 at 12:02 PM, David Dyess <david...@gmail.com> wrote:
> Haha...   I set you up on that one ;)   I know the OP was asking about
> classes, but I didn't want to just say "No! never do that!" and leave it at
> that. I was trying to explain why I wouldn't. Now I see I wasn't very clear,
> but I was trying to show there is a difference between having PHP within
> HTML and having HTML within PHP (as in the echoed stuff Javier posted).
> I hope Kathy Lee can dodge 45 cal bullets...because I still stand by my
> previous statement there is nothing TECHNICALLY wrong with mixing PHP and
> HTML.  Do what works for the job at hand..

Everyone knows she's an undead temptress with regeneration powers to
make Wolverine seem like he bruises easily.

And no, there's nothing TECHNICALLY wrong with it. Just like there's
nothing technically wrong with building a mosque at ground zero. Or
there's nothing technically wrong with taking advantage of tax
loopholes to get out of paying more than you might otherwise. Or
there's nothing technically wrong about hitting that retarded teenager
in the head with a board. But people will still argue with you about
it. Especially in the tech community.

I do like the idea of the difference. Put PHP in your HTML, not HTML
in your PHP.

--
Jack Timmons
@_Codeacula

David Dyess

unread,
Aug 26, 2010, 5:57:39 PM8/26/10
to professi...@googlegroups.com
Practices are practices and they are always changing. 10 years from now people may look at last year's code and say "You mean they put everything in the global namespace?!?!" or maybe not... who knows!

Practices are also why I stopped buying books about PHP and just started using Google with a dash of PHP.net.


--

Gaurav Kumar

unread,
Aug 27, 2010, 3:52:33 PM8/27/10
to professi...@googlegroups.com
Everyone will certainly have there own opinion regarding the separation of HTML and PHP code in an architecture.

Most of it depends upon the project requirement and architecture you tend to follow.
Conventionally the HTML is separated from PHP code to an extend that a Designer can easily modify the HTML code (or fit to new design) without interfering with the PHP script logic.

In a real time example (not talking about any specific framework) you certainly would like to keep your header, footer and middle page html code separate from PHP code. But may keep certain logic to display user action menus in a class functions so that these functions can be called on the basis of user permissions.

Something like below-

//View Class
class viewProductPage
{
    public function showEditPage()
   {
   
       $link = '';
       if($userHasPermission == "edit")
      {
          $link = "<a href=edit.php>Edit</a>";
       }
     
      return $link;
   }
}

//HTML Page which is architect to be include in some master view class or in above view class itself; Includes only HTML code

<div id="menu">
$this->showEditPage();
<div>

 
Thanks,

Gaurav Kumar

http://www.UncleCode.Com (Download Scripts, Resources For Free)
http://www.unclecode.com/2010/08/how-to-send-an-attachment-in-an-email-using-php-php-send-email-with-attachment/
http://www.unclecode.com/2009/10/jquery-php-crop-image-and-save-image-thumbnail-on-the-fly/
http://www.unclecode.com/2009/09/php-email-using-jquery-ajax/


On Wed, Aug 25, 2010 at 11:50 PM, Cecil Champenois <cecilch...@yahoo.com> wrote:
In the book, PHP and MySQL, the authors show code where HTML is inserted into their classes to build their headers and footers for each page throughout the sample web files.
 
I read somewhere that this is not a good idea, but I am not sure why.
 
Can someone explain if it is good or not to mix in HTML with PHP classes?
 
Cecil

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
 
For information or project assistance please visit :
http://www.360psg.com
 
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP



--


Cecil Champenois

unread,
Aug 26, 2010, 1:55:51 PM8/26/10
to professi...@googlegroups.com
Sounds good to me.
 
Okay one last technical question: What is an OP?
 
Cecil



From: Jack Timmons <code...@codeacula.com>
To: professi...@googlegroups.com
Sent: Thu, August 26, 2010 10:29:55 AM

Subject: Re: [Pro. PHP Dev.] Mixing HTML into PHP Classes

nickW+

unread,
Aug 30, 2010, 12:44:10 AM8/30/10
to Professional PHP Developers
Cecil,

I've seen you on here for a while and I see your doing your best to
follow best practices.

I have learned a lot about PHP through experience and I had guidance
from some decent PHP programmers when I started.

As others mentioned HTML can 'technically' be mixed into a CLASS.

I do everything in my POWER to not use HTML in classes. Usually a
function from a class is called from a page where you want content to
load. In my findings the most efficient way to do this is get a
function to return an ARRAY containing all the values and then add
them to the page with your code of choice. I like shortcodes because
it makes the code look cleaner, others will argue it makes it harder
to read so that's just personal preference.

There will however be times when you can't avoid HTML in a CLASS or
FUNCTION. For example when you work with plugins. Depending on how the
system was designed you will or will not have the ability to create on
page code. For example this product open realty only allows you to
"replace" a template tag so you have to replace it with your plugin
content including rendered html.

However if you are designing a system from scratch, you shouldn't have
ANY reason at all to mix the two and really shouldn't. The most
important point is being able to support, manage and delegate work. A
designer will deal with the HTML and CSS while you will deal with the
PHP. The two if completely separate give you both the ability to do
whatever you want without stepping on one anothers toes.

Jack Simmons told me in another thread that having a Wiki on your site
is very important and I agree, specifically for these sorts of things
so you can outline what values are available on a page and let them
handle it.

Not all HTML is going to be specific to a page, as some programmers
will make an HTML class specifically for that and include HTML files.

Just my 2 cents,

Nick

On Aug 25, 12:20 pm, Cecil Champenois <cecilchampen...@yahoo.com>

Cecil Champenois

unread,
Aug 30, 2010, 1:23:44 AM8/30/10
to professi...@googlegroups.com
Nick,
 
I appreciate your taking the time to explain these things. I looked aroudn for a PHP class in a local college and couldn't find even one class on PHP/MySQL.
 
Cecil



From: nickW+ <ni...@weeklyplus.com>
To: Professional PHP Developers <professi...@googlegroups.com>
Sent: Sun, August 29, 2010 9:44:10 PM
Subject: [Pro. PHP Dev.] Re: Mixing HTML into PHP Classes

Robert Gonzalez

unread,
Aug 30, 2010, 1:45:37 AM8/30/10
to professi...@googlegroups.com
OP == Original Poster.

On Sunday, August 29, 2010, Cecil Champenois <cecilch...@yahoo.com> wrote:
>
> Nick,
>
> I appreciate your taking the time to explain these things. I looked aroudn for a PHP class in a local college and couldn't find even one class on PHP/MySQL.
>  Cecil
>
>
>
>
>

Cecil Champenois

unread,
Aug 30, 2010, 1:50:27 AM8/30/10
to professi...@googlegroups.com
Ha! I thought it meant something else, like Old People, or who knows what else?
 
Cecil



From: Robert Gonzalez <robert.anth...@gmail.com>
To: "professi...@googlegroups.com" <professi...@googlegroups.com>
Sent: Sun, August 29, 2010 10:45:37 PM
Subject: Re: [Pro. PHP Dev.] Mixing HTML into PHP Classes

Jack Timmons

unread,
Aug 30, 2010, 7:01:40 AM8/30/10
to professi...@googlegroups.com
On Sun, Aug 29, 2010 at 11:44 PM, nickW+ <ni...@weeklyplus.com> wrote:
> I like shortcodes because
> it makes the code look cleaner, others will argue it makes it harder
> to read so that's just personal preference.

Do you mean shorttags?

And, if you do, do you know why you shouldn't use them?

> Jack Simmons told me in another thread that having a Wiki on your site
> is very important and I agree, specifically for these sorts of things
> so you can outline what values are available on a page and let them
> handle it.

It's Timmons. What, do I look like the brother of a gay fitness star?

Who is totally fabulous.

A wiki is the first step. There's a lot we should do as developers as
it relates to just administration crap.

> Just my 2 cents,

I want a refund.

Simmons. Seriously?

As a side note, I once knew a guy named Simmons. We were decent
friends, born on the same day, same year. He was a short, black man.
We got kicks out of that all the time.

On Mon, Aug 30, 2010 at 12:23 AM, Cecil Champenois


<cecilch...@yahoo.com> wrote:
> Nick,
>
> I appreciate your taking the time to explain these things. I looked aroudn
> for a PHP class in a local college and couldn't find even one class on
> PHP/MySQL.

You won't find it. Every college I've seen that -does- teach PHP is
still teaching it from back when PHP 4 came out.

--
Jack Timmons
@_Codeacula

nickW+

unread,
Aug 30, 2010, 3:34:12 PM8/30/10
to Professional PHP Developers
> Do you mean shorttags?
>
> And, if you do, do you know why you shouldn't use them?

Yes I meant shorttags.

> It's Timmons. What, do I look like the brother of a gay fitness star?

No comment. But an honest mistake... it was late in the AM :(

nickW+

unread,
Sep 8, 2010, 2:04:13 AM9/8/10
to Professional PHP Developers
> And, if you do, do you know why you shouldn't use them?

I'd actually like to know why it's better to use <?php echo $myVar ?>
instead of <?= $myVar ?>

Jack Timmons

unread,
Sep 8, 2010, 6:45:32 AM9/8/10
to professi...@googlegroups.com

Because short tags are an optional feature.

So, if you write all of your software using <?= and move to a new
server with it turned off, or update to a version of PHP that no
longer supports it, what choo gunna do?

That sort of deal.

--
Jack Timmons
@_Codeacula

Ovidiu Alexa

unread,
Sep 8, 2010, 7:02:16 AM9/8/10
to professi...@googlegroups.com
er, find and replace? hmmm, maybe Dreamweaver could do that, or a more
advanced option would be a linux command that does just that find ./
-name '*.php' | xargs perl -pi -e 's/<?=/<?php/g';
I don't know if you need to escape the '<' and '?' but if there's a will
there's a way

Cheers!

Jack Timmons

unread,
Sep 8, 2010, 7:07:11 AM9/8/10
to professi...@googlegroups.com
On Wed, Sep 8, 2010 at 6:02 AM, Ovidiu Alexa <ovidiu...@gmail.com> wrote:
>  er, find and replace? hmmm, maybe Dreamweaver could do that, or a more
> advanced option would be a linux command that does just that find ./ -name
> '*.php' | xargs perl -pi -e 's/<?=/<?php/g';
> I don't know if you need to escape the '<' and '?' but if there's a will
> there's a way

That's not the point. The point is you aren't making portable code
when you do it that way. And do you always have control of your
server? What happens if you use short tags, and the server is switched
to a version that doesn't use it without you knowing? How much of your
business will be affected because you couldn't at most type a few
extra characters, and at least have a template to automatically put in
a full echo.

I use ec in my HTML layout. Four keypresses gets me <?php echo();?>.

Once you start down the path of the dark side, forever tainted will
your code be.

--
Jack Timmons
@_Codeacula

Ovidiu Alexa

unread,
Sep 8, 2010, 7:15:32 AM9/8/10
to professi...@googlegroups.com
yeah, I admit, it could happen, but not so often. Even if it happens, it's the administrators' fault.. sort of.. and they should either enable the short php tags or replace them with the script that I just submitted.  Or find another data-center that fits your needs. Nevertheless, this is just a very rare situation, in conclusion, both <? and <?php are correct, and if you need to shorten out in some php like in this example:

<input name='name' type='text' value='<?=$name?>' /> then DO IT, it's far better to separate as much as possible the php code from the html code.

oh yeah, another example:

<div class='<?=$odd_even?>'><?=$the_value?></div>

VS

<div class='<?php echo $odd_even; ?>'><?php echo $the_value; ?></div>

catch my drift?
Cheers!

Jack Timmons

unread,
Sep 8, 2010, 7:15:49 AM9/8/10
to professi...@googlegroups.com
On Wed, Sep 8, 2010 at 6:02 AM, Ovidiu Alexa <ovidiu...@gmail.com> wrote:
>  er, find and replace? hmmm, maybe Dreamweaver could do that, or a more
> advanced option would be a linux command that does just that find ./ -name
> '*.php' | xargs perl -pi -e 's/<?=/<?php/g';
> I don't know if you need to escape the '<' and '?' but if there's a will
> there's a way
>
> Cheers!

Even better, for those of you who freelance PHP:

You're writing software for your client using short tags. Everything's
working just dandy. The client tries it out and says it seems to be
working fine. He gives you the rest of your pay (you should be asking
for some up front, you know), and you give him the software.

But now he goes to put it up. But, what the hell, little is showing
up. Why are there all these gaps? So, not wanting to look like an
idiot to you (and possibly get charged more money for something that's
his fault), he calls his tech friend over. She apparently didn't have
time to do the project for him, but she can spot in a heartbeat what
the issue is.

Congratulations, you just lost future business for an untold amount.
Now, when the client speaks of you, he's going to talk about that
developer he hired who couldn't do the job right. Or about the
developer who might've turned the project in on time and budget, but
needed to support the software in ways that should have never needed
it.

Guess how often this happens?

Not to me. I mean for others using short tags.

--
Jack Timmons
@_Codeacula

Ovidiu Alexa

unread,
Sep 8, 2010, 7:21:38 AM9/8/10
to professi...@googlegroups.com
Sure, sure, all you 'freelancers' need to listen to Jack. Not me. I'm
not one of you ;) cheers!

Jack Timmons

unread,
Sep 8, 2010, 7:30:02 AM9/8/10
to professi...@googlegroups.com
On Wed, Sep 8, 2010 at 6:15 AM, Ovidiu Alexa <ovidiu...@gmail.com> wrote:
> yeah, I admit, it could happen, but not so often. Even if it happens, it's
> the administrators' fault.. sort of..

There's so many things wrong with that statement.

First, never, -ever- insult your sysadmin. That's like being rude to
your waitress, except the sysadmin can do far worse than rub your New
York strip on the cook's buttocks.

Second, what are you going to do when this happens:

<?xml

> find another data-center that fits your needs. Nevertheless, this is just a
> very rare situation

Rare for you, perhaps. I've seen it frequently enough to warrant
notice beyond "Make it portable!"

My point is you can never know what's going to happen to your server,
and it's your duty to compensate for it. It's -your- fault for relying
on an optional feature of PHP.

> in conclusion, both <? and <?php are correct, and if
> you need to shorten out in some php like in this example:

"Correct" as in valid, I agree. But I'm arguing practice. One can
write software using sprintf correctly, but you're going to get yelled
at to use snprintf instead. And for good reason.

> <input name='name' type='text' value='<?=$name?>' /> then DO IT, it's far
> better to separate as much as possible the php code from the html code.
>
> oh yeah, another example:
>
> <div class='<?=$odd_even?>'><?=$the_value?></div>
>
> VS
>
> <div class='<?php echo $odd_even; ?>'><?php echo $the_value; ?></div>

The only thing I see wrong with that is I always encapsulate my echo's
in parenthesis.

> catch my drift?

If it ain't the Tokyo Drift, I ain't catchin' it.

I don't allow it in my department because I don't care to deal with
the possibility of someone turning it off, or a PHP update changing
the default from 0 to 1. If you want to, by all means it's technically
correct, and as a bureaucrat that's the best sort of correct you can
be. As a developer focused on portability and security, I'm going to
stick with my long tags.

--
Jack Timmons
@_Codeacula

Ovidiu Alexa

unread,
Sep 8, 2010, 8:22:19 AM9/8/10
to professi...@googlegroups.com
thanks for the heads-up, in other words, use <?php if you think that
you'll move your script on another server that may have the short php
turned off, or what-not and use <? if you're in a hurry and don't give a
damn about the script working on another server and may invoke
portability problems.


Cheers!

Jack Timmons

unread,
Sep 8, 2010, 8:51:26 AM9/8/10
to professi...@googlegroups.com
On Wed, Sep 8, 2010 at 7:22 AM, Ovidiu Alexa <ovidiu...@gmail.com> wrote:
>  thanks for the heads-up, in other words, use <?php if you think that you'll
> move your script on another server that may have the short php turned off,
> or what-not and use <? if you're in a hurry and don't give a damn about the
> script working on another server and may invoke portability problems.

What about just coding for portability and fighting The Good Fight?

That's what Three Dog would want you to do.

--
Jack Timmons
@_Codeacula

Joe Williams

unread,
Sep 8, 2010, 8:53:32 AM9/8/10
to professi...@googlegroups.com
Really? Three Dog Night? Most kids on this list aren't going to have an idea who that is lol.

-------
Twitter: @mrstatic
Website: https://www.speakservers.com

Sent on the Sprint® Now Network from my BlackBerry®

Jack Timmons

unread,
Sep 8, 2010, 9:01:02 AM9/8/10
to professi...@googlegroups.com
On Wed, Sep 8, 2010 at 7:53 AM, Joe Williams
<joseph.s...@gmail.com> wrote:
> Really? Three Dog Night? Most kids on this list aren't going to have an idea who that is lol.

I'm am an apparent master of the double entendre.

My mom used to play Three Dog Night. I really liked what I heard.

But, the Three Dog I mentioned is in a whole 'nother realm.

http://fallout.wikia.com/wiki/Three_Dog

What's all this crazy question you're askin' me?

--
Jack Timmons
@_Codeacula

Robert Gonzalez

unread,
Sep 8, 2010, 12:24:25 PM9/8/10
to professi...@googlegroups.com
Turn short tags then try to play with XML files. ;)

Simply put, the cleanest and most portable way to code for PHP is to use the long opening tag syntax. It isn't that hard to do and really doesn't save *that* much overhead in your coding as opposed to short tags.

And for the record, every server I manage has short_open_tags turned off. So if you ever provided code to me to use, if it was coded with short tags, it would be rejected right off the pop.


--
Jack Timmons
@_Codeacula

Tyler Gillispie

unread,
Sep 8, 2010, 3:56:24 PM9/8/10
to Professional PHP Developers
When I develop on a server where I have full control, I use <?php ?>.
It makes sense to me to develop to the highest possible standard. I
consider normal php tags the standard. The PHP documentation states,
"... if you are embedding PHP within XML or XHTML you will need to use
the <?php ?> tags to remain compliant with standards. " The doc does
mention that short tags should be avoided for redistributable code
which might might imply that if your code is not going to be
redistributable then it is ok? The most important info I find with
this doc refers to the standard of using <?php ?> and recommends this
action. There is potential for IDE's and various other clients that
might have problems with short tags.

Another compelling reason to not use short tags is that they are
probably not needed. I try not to put myself into a position where I
am coding mad amounts of PHP in my HTML. It helps me keep my files
clean and more maintenance friendly.

Short tags are not the end of the world and I don't see them being any
harm in a controlled environment. It seems there will be less overall
potential for problems if you use normal PHP tags.


On Sep 8, 5:22 am, Ovidiu Alexa <ovidiu.al...@gmail.com> wrote:
>   thanks for the heads-up, in other words, use <?php if you think that
> you'll move your script on another server that may have the short php
> turned off, or what-not and use <? if you're in a hurry and don't give a
> damn about the script working on another server and may invoke
> portability problems.
>
> Cheers!
>
> On 9/8/2010 14:30, Jack Timmons wrote:
>

Tyler Gillispie

unread,
Sep 8, 2010, 3:57:12 PM9/8/10
to Professional PHP Developers

Robert Gonzalez

unread,
Sep 8, 2010, 4:03:08 PM9/8/10
to professi...@googlegroups.com
Something to note on that page:

... but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print().

Someone had posted an example earlier in this thread that showed a huge glob of HTML outputted in a string of PHP echo. Perhaps not the best way to handle that?

On Wed, Sep 8, 2010 at 12:57 PM, Tyler Gillispie <mrt...@gmail.com> wrote:
http://php.net/manual/en/language.basic-syntax.phpmode.php

nickW+

unread,
Sep 8, 2010, 8:13:07 PM9/8/10
to Professional PHP Developers
Woah look at all the answers.

Alright I agree with what Jack says. I used short tags because it
makes the code cleaner.

My specific question was between <?= ?> and <?php echo ''; ?> ... I
realize that short tags also include <? vs <?php as the opening php
statement however I ALWAYS use the later.

Jack's main point here is portability. Each host is different. I have
the benefit of being on a completely dedicated server and my host will
ask my permission before even updating a package. However what happens
when we update to a new version of PHP without short tags on by
default? or need to move to another server that is freakishly careful
about security, well as I understand it, my code will fail. This is a
scary thought, but I'm very glad I understand that this can be a
problem and know what the symptom is.

I will probably start changing my code to the long method once I get
some time.

Robert. Sending a string is normally the best way to handle template
stuff. But I do agree that anytime I do on page PHP / HTML I close out
PHP do HTML and reopen <?php ...

I like to follow the best practices, new programmers understand it
better and less issues in the long run.

Since I do have my own server it's not such a big issue right now, but
for those that may run into this problem in future (me included) there
is a line you can add to the .htaccess that can fix this.

php_flag short_open_tag on

However you should know this won't work on all servers due to their
security settings.

Regards,

Nick

On Sep 8, 2:03 pm, Robert Gonzalez <robert.anthony.gonza...@gmail.com>
wrote:
> Something to note on that page:
>
> *... but for outputting large blocks of text, dropping out of PHP parsing
> mode is generally more efficient than sending all of the text through **
> echo()* <http://www.php.net/manual/en/function.echo.php>* or
> **print()*<http://www.php.net/manual/en/function.print.php>
> *.*

Robert Gonzalez

unread,
Sep 8, 2010, 8:16:22 PM9/8/10
to professi...@googlegroups.com
For the record, short open tags are <?. Long open tags are <?php. Script tags are <script language="php"></script>. And ASP tags are <%.

Jack Timmons

unread,
Sep 8, 2010, 9:28:27 PM9/8/10
to professi...@googlegroups.com
On Wed, Sep 8, 2010 at 7:16 PM, Robert Gonzalez
<robert.anth...@gmail.com> wrote:
> For the record, short open tags are <?. Long open tags are <?php. Script
> tags are <script language="php"></script>. And ASP tags are <%.

The latter two being, IM(not totally)HO, totally pointless, useless,
and seriously asking to be removed.

There might be a point. And there might be a use. But I've never once
come across it, and I don't think I care to.

--
Jack Timmons
@_Codeacula

Robert Gonzalez

unread,
Sep 9, 2010, 1:16:41 PM9/9/10
to professi...@googlegroups.com
Dude, I like ASP tags. I like watching all of my editors puke on them while trying to syntax highlight my code.


--
Reply all
Reply to author
Forward
0 new messages