Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Lately I've been working a lot in PHP and have run into an abnormally large number of parsing errors. I realize these are my own fault and a result of sloppy initial coding on my part, but it's getting to the point that I'm spending more time resolving tags than developing. In the interest of not slamming my productivity, are there any tricks to locating the problem in the code?

What I'd really be looking for would be a line to put in the code which would output the entire faulty tag in the parsing error, or something similar.

Purely for reference sake, my current error is Parse error: syntax error, unexpected '}' in /home/content/80/9480880/html/cache/tpl_prosilver_viewtopic_body.html.php on line 50

(which refers to this):

</dd><dd><?php if ($_poll_option_val['POLL_OPTION_RESULT'] == 0) {  echo ((isset($this->_rootref['L_NO_VOTES'])) ? $this->_rootref['L_NO_VOTES'] : ((isset($user->lang['NO_VOTES'])) ? $user->lang['NO_VOTES'] : '{ NO_VOTES }')); } else { echo $_poll_option_val['POLL_OPTION_PERCENT']; } ?></dd> </dl> <?php }} if ($this->_rootref['S_DISPLAY_RESULTS']) {  ?> <dl> <dt>&nbsp;</dt> <dd class="resultbar"><?php echo ((isset($this->_rootref['L_TOTAL_VOTES'])) ? $this->_rootref['L_TOTAL_VOTES'] : ((isset($user->lang['TOTAL_VOTES'])) ? $user->lang['TOTAL_VOTES'] : '{ TOTAL_VOTES }')); ?> : <?php echo (isset($this->_rootref['TOTAL_VOTES'])) ? $this->_rootref['TOTAL_VOTES'] : ''; ?></dd> </dl> <?php } if ($this->_rootref['S_CAN_VOTE']) {  ?> <dl style="border-top: none;"> <dt>&nbsp;</dt> <dd class="resultbar"><input type="submit" name="update" value="<?php echo ((isset($this->_rootref['L_SUBMIT_VOTE'])) ? $this->_rootref['L_SUBMIT_VOTE'] : ((isset($user->lang['SUBMIT_VOTE'])) ? $user->lang['SUBMIT_VOTE'] : '{ SUBMIT_VOTE }')); ?>" class="button1" /></dd> </dl> <?php } if (! $this->_rootref['S_DISPLAY_RESULTS']) {  ?> <dl style="border-top: none;"> <dt>&nbsp;</dt> <dd class="resultbar"><a href="<?php echo (isset($this->_rootref['U_VIEW_RESULTS'])) ? $this->_rootref['U_VIEW_RESULTS'] : ''; ?>"><?php echo ((isset($this->_rootref['L_VIEW_RESULTS'])) ? $this->_rootref['L_VIEW_RESULTS'] : ((isset($user->lang['VIEW_RESULTS'])) ? $user->lang['VIEW_RESULTS'] : '{ VIEW_RESULTS }')); ?></a></dd> </dl> <?php } ?> </fieldset></div>
share|improve this question
5  
Format Your Code A big reason for using consistent line breaks, indentation, spacing, and naming is so that errors jump out at you. There's no reason for all of this to be on one line. (You're also abusing the ? operator, which makes the code especially obtuse in the absence of formatting.) –  grossvogel Jun 20 '12 at 17:48
2  
I'm really working on improving that. These are old edits to someone else's code. –  ryan.riverside Jun 20 '12 at 17:53
1  
@ryan.riverside: Depending on what tools you're using, it can automatically format the mix of PHP/HTML at the click of a button. That might make line 50 a little more readable. ;) –  FrustratedWithFormsDesigner Jun 20 '12 at 17:55
    
Ah... yeah, that's a rough spot to be in. If asked to change code like this (and if quitting isn't an option), I'd first reformat it w/o altering its behavior. THEN make the desired changes. –  grossvogel Jun 20 '12 at 17:55
    
@FrustratedWithFormsDesigner: What in particular are you thinking of? –  ryan.riverside Jun 20 '12 at 20:30
show 1 more comment

2 Answers

up vote 6 down vote accepted

In no particular order (well, maybe in order order of relevance, sort of):

  • format your code;
  • create code generators, so you don't mangle PHP and HTML code. It's a basic principle: separation of concerns for view/UI and business/logic code;
  • use automated code checkers;
  • write at least a few basic unit or integration tests that will detect the "blank page because of parse error" scenarios;
  • use a decent IDE, that will give you instant feedback on parse errors (if you separate UI and logic code...);
  • have shorter code-to-test lifecycles so you catch these early.
share|improve this answer
    
What is your preferred IDE? –  ryan.riverside Jun 20 '12 at 20:27
2  
@ryan.riverside: Strangely enough, for PHP I actually don't really use one, as I use emacs. But if you separate code it works. So I'm not the best example for my recommenadtions here ;) That being said I used to code with the PHP plugin for Eclipse (PDT), and a long time ago Aptana. I've also heard that PHP Storm is quite good. To be honest, nowadays I rarely code in PHP, except for some freelancing gigs, and I don't do it merrily. I dread both the language and its tooling. –  haylem Jun 20 '12 at 20:34
    
Thanks for such a thorough and complete answer and response! –  ryan.riverside Jun 20 '12 at 20:39
    
@ryan.riverside: You're welcome. And welcome to P.SE. –  haylem Jun 20 '12 at 20:40
add comment

This example is screaming to be refactored into a function. Maybe two or more.

Once you've created a function, and put it in an included PHP file, you can test it in (relative) isolation. Perhaps by creating a page that calls that function and does nothing else, and passing that page to an HTML verifier (I'm assuming your problem is HTML parsing, because otherwise the PHP engine would be telling you where the problem is).

Plus, with a function you can insert comments that explain exactly where the content is being generated (although remove them for production use).

share|improve this answer
    
Well, part of the problem is that the file is generated. In the original files (there are many that this draws from), there must be an error...somewhere...? I was given this site because it was popping errors on every other page. I'd like to find the error somehow so I don't have to redesign it, but the base site I'm dealing with is modular enough I could just replace the entire php display styleset with another. –  ryan.riverside Jun 20 '12 at 20:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.