Forum Moderators: coopster

Message Too Old, No Replies

Is this normal formatting?

         

optik

9:50 am on Jul 8, 2010 (gmt 0)

10+ Year Member



<?

...other code...

function displayForm() {
$this->pageTop();
?>
<form id="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>
<?

...rest of code


Ok I've never seen a PHP file done like this and wondered how conventional it is, it's a .php file and this is part of a Class, when this method is called the code outside of the PHP tags is echo'd. The whole document is done like this mixing design and logic and there are loads of PHP tags in the document.

Maybe I'm missing something but it seems a mad way of coding?

Matthew1980

10:30 am on Jul 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there optik,

Seems a bit odd, but not totally unique, there are lots of different ways of doing the same thing, but where possible try to keep the php & html seperate, makes for better coding if you make things modular.

All that has been done is to try and give the CPU & parser a break by not echoing huge chunks of html from within the function, this is great if you are doing chunks but it would be easier to just include a file with the html in that.

<?php is preferred over <? because of server/host transition issues should you wish to migrate :) not all servers support short form tags..

Also, you don't necessarily need to use $_SERVER['PHP_SELF']; just leave it blank because by default the form will submit to itself, and there are security issues using $_SERVER['PHP_SELF']; on a live site.. google "$_SERVER['PHP_SELF']; security issues" to see what I mean.

Hope that helps ;)

Cheers,
MRb

rocknbil

6:23 pm on Jul 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...it seems a mad way of coding?


Completely agree, on a couple points. However, if it's for a closed system with no intent of repurpose or expansion, I suppose it's "good enough."

My problem with PHP is that it lends itself to this kind of coding, which gave birth to a "whole new kind of spahetti code:"

<?php
// block
?>
<html here>
<?php
// block
?>
<more html><?php // block ?><more html>
<more html>
<?php
// block
?>

This drives me nutz. :-)

Then,

it's a .php file and this is part of a Class,


You begin using classes and functions to compartmentalize your code and move toward object oriented programming (or something like it,) overall, making your functions anonymous objects you can re-purpose without modifying every time you use them.

You pass parameters to a function and get results, for example,

$my_form = get_some_form('contact-response.php',$error_messages,'some-style-selector');

Instead, the approach here puts hard-coded HTML snippets in the function, and makes assumptions: it will always be a contact form, they will always want a page top (see below) . . . list goes on. Admittedly, there are times you will need to put some form of hard coding in your objects, but what if there is an input error and you want to display the error right on the form?* Or change it's style? (thankfully, there is an ID for this purpose, but if it's used for "something else" someone trying to mod the CSS can wonder why "#contactform" is affecting a form that is NOT a contact form.)

(*Another thing about PHP is the abuse of sessions. Instead of programming the form right so it accepts and messages you wish to pass to it, I've seen many systems tack on an error message using sessions to implement returning to forms on error. OK, I guess it works, but it an obvious patch to a poorly planned implementation.)

Then . . . it does an echo from within the class. What if you want to do something like this?

// collect database data
// print header
// print database output
// print form
// print footer

What you wind up doing is digging in the class, modifying the output, hoping to god it doesn't break something else, and wondering WTH they were thinking . . .

But the most revealing error of all,

.... <?php echo $_SERVER['PHP_SELF']; ?>


1. Google for PHP_SELF vulnerability, this is bad. (Note to Matt: leaving it blank is not a good idea either, it's almost the same thing . . . there's some recent threads about this.)
2. See "example" above, the function should expect the action as an input parameter so that you can change it at any time without having to mod the function.

As mentioned, if this was a one-off for a small project and it works, no big deal, but one of the mistakes programmers make (I've made it far to often myself) is to assume "this is the project, this is what they need, this will never change." If it's part of a bigger project, horrible.

jatar_k

12:56 pm on Jul 9, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> My problem with PHP is that it lends itself to this kind of coding,

I have an opposite view, I believe it's part of what makes php so powerful and versatile.

I like the blending and it allows me to do lots of things very succinctly. I think it's a mad way of coding when I see people hauling around piles of classes and code they don't need on every page or instantiating a couple hundred lines of code to modularize something that could have been done in a couple lines of blended code.

All things in moderation, just because it's in a class doesn't mean it's good.

I believe php needs a bit of a unique approach to use it to it's utmost

optik

3:59 pm on Jul 9, 2010 (gmt 0)

10+ Year Member



That was just a snippet, the whole page was like that, I later found out it was knocked up quickly as a favour so I'm slightly forgiving but still bemused at the approach.

I got it back to something that doesn't fry my brain fairly quickly so I'm over it now but good to know I wasn't doing things wrong.

Matthew1980

6:39 pm on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I like jumping in and out where I can, makes things easier to edit and keep track of, nothing irritates me more than seeing huge chunks of constructed $var's to echo later down the page, Ok, if its small chunks that wouldn't be too much of a strain on the overheads, but generally speaking jumping in and out where possible is a great way to blend html & php together.

Cheers,
MRb

rocknbil

6:41 pm on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have an opposite view, I believe it's part of what makes php so powerful and versatile.


Most coders do, it's just my op' that comes from several working concepts. In page creation, we separate presentation from document structure for a reason, to allow the presentation to be controlled externally without modification. See Zen Garden.

In the same way, scripting and programming - the functions and mechanics that drive the dynamic software - should also be allowed to be controlled remotely, and you should be able to not only change it's form and output with external tools (templates,) you shouldn't have to muck about with the code to do so. When you mix programming in with (X)HTML it moves farther and farther from separation of form and function.

Second, it makes it very difficult for a designer to create a template, especially if they are working in WYSIWYG's. On a whim the other day I opened a PHP script in a browser, just to see if I could get a visual on what the HTML parts were doing. It was so spaghetti'ed up with PHP every third line, viewing it in a browser was a waste of time. With applications I code, a template has markers, like

|HEADING|
|CONTENT|

or more design-friendly,

<!--HEADING-->

and any designer can modify to their heart's content as long as they leave the markers in.

I've had to mod many current WP, vBulletin, PHPBB templates, and while I muddle through it, it takes me three times longer than it should, and I wonder how many non-savvy designers do it.

seeing huge chunks of constructed $var's to echo later down the page


When you store output in a variable, yes, you are taking up space in memory. But what's more costly, a single request to the PHP parser and storing it's work in memory or multiple calls to it? I'm asking because with PHP, I don't know, but in my experience with Perl, the former gives much more consistent results. There used to be a day when we would consider memory usage over user experience, but the technology has changed, we can trade a little memory for an improved user experience.

Unless you have extremely large pages, storing page output, which may (should!) be 20-50K at best, is a highly efficient approach as opposed to process and print. Have you ever had a page that does this?

-- shows the header
-- pauses, waits for a complicated database query
-- starts to output the query
-- outputs rows slowly, if the query is inefficient
-- sometimes dies in mid page if the mysql server chokes, leaving the bottom half of the page empty
-- finally finishes out the rest of the page.

I'd rather spit the entire output to the browser as a completed chunk than line by line. When you process->print process-> print you have to remember, the print is the connection from the server to the browser, you're sending data along that connection to the end user. A lot of things can go wrong between process and print that have nothing to do with your script. The more prints you have, the more opportunity for something to break. I'd rather send it once.

All boils down to experimentation and what gives the best experience at the least cost to the server, I guess.

[edited by: rocknbil at 7:16 pm (utc) on Jul 9, 2010]

Matthew1980

6:53 pm on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I like jumping in and out where I can, makes things easier to edit and keep track of, nothing irritates me more than seeing huge chunks of constructed $var's to echo later down the page, Ok, if its small chunks that wouldn't be too much of a strain on the overheads, but generally speaking jumping in and out where possible is a great way to blend html & php together.

[EDIT:]Though Rocknbil does make a good case there :)

Cheers,
MRb

optik

9:27 pm on Jul 9, 2010 (gmt 0)

10+ Year Member



Rocknbil is right imo, keeping logic and design separate as much as possible has lots of advantages it makes what your coding far easier for people to contribute to and for you to re-use your code in a more modular way.

It took me a good while to get used to this idea though.

If I compare my updated version of this page to what I was supplied with the difference is pretty substantial.

Matthew1980

10:04 pm on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

But what's more costly, a single request to the PHP parser and storing it's work in memory or multiple calls to it? I'm asking because with PHP, I don't know.


I am just thinking from a logical point of view, I read an answer on another forum a few weeks back to this exact same issue; I quote:-
that makes it harder to read the structures though. Execution time is cheap, man hours are expensive. Not to mention this is one of those microoptimisations which is almost negligible.


<rant>
IMO, for larger chunks of code stored in memory but using php to parse the information to be displayed in the browser is more CPU intensive, and as you point out, this is a probable cause of some websites 'choking' at the amount of data needing to be parsed in order for the browser to the receive the data and display it, this IMO is why dipping in and out is a efficient way to go about things.

Imagine how much time would be taken up if you had a high volume of traffic coming to your site, so many calls to the parser, the 'heavier' more intensive calls take longer to process, but because this in Pre Hypertext Preprocessing the clue I think lye's in this title, dipping in and out reduces the time spent scanning the document - therefore html can be piped directly to screen, until the next call is requested, though I think the exe file does actually read the file and only react if it comes across the <?php tag, then it interprets the data there until the closing ?> is found.
</rant>

There again, I may be totally wrong, but this is how I have always treated this type of situation.

Cheers,
MRb

TheMadScientist

9:50 pm on Jul 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I read an answer on another forum a few weeks back

Hmmm... I read years ago in the Library (yes, it was with the eyes, using the Browser for all the Clue players. LOL)

Benchmarking PHP text output [webmasterworld.com]
Test 3
- accumulating all output to a variable and then echoing once is always at least an order of magnitude faster than echoing piecemeal or switching in and out of PHP.

I rarely switch in and out of php and for some reason I usually use single quotes and almost always use concatenation and accumulation... But then again I'm a bit of a speed freak. ;)

BTW: HUGE THANKS to ergophobe for the post...
It's one of the most useful posts for php 'best practices' or 'good code style base' I've read here, or anywhere for that matter.

Readie

11:02 pm on Jul 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I have to wonder is, how does that compare to building all dynamic content into a variable and jumping into PHP to echo it in the midst of a static HTML-filled page?

It's also a bit of a pity that there's no mention of output buffering in the post.

I rarely switch in and out of php and for some reason I usually use single quotes and almost always use concatenation and accumulation... But then again I'm a bit of a speed freak. ;)

I do the same, but just some quirky habit I picked up :)