Hello and welcome to our community! Is this your first visit?
Register
View RSS Feed

tangoforce

Coding styles and $end errors

Rate this Entry
by , 07-06-2014 at 01:48 PM (2980 Views)
Hi

I'm sure many of you have come across the "Unexpected $end" or "Missing $end" messages many times and it's probably taken you many minutes, hours or even days to find it. If its the latter two options i'm sure if you're like me it's probably taken you a lot of desk thumping to to identify the fault.

Firstly I'd like to show you two different coding styles here:
PHP Code:
<?
//Example A - The most popular
function test_a($Input){
if (
$Input){
print 
'a';
}
}

//Example B - Less popular
function test_b($Input)
   {
   if (
$Input)
      {
      print 
'b';
      }
   }
?>
I've personally always found example B not only clearer to understand but easier to debug. Despite not fitting with the 'peer pressure' from other coders it is actually a lot nicer to work with and Notepad++ demonstrates this nicely too:



If you look at example A, you'll see that the opening brace is highlighted red along with its partner - Great huh?

Look at example B though and you'll see why it's so much easier to debug. Not only are the opening and closing braces highlighted red, NP++ also draws a red dotted line down the page directly to it so you can see it (Apologies, Photoshop didn't preserve the red dotted line very well). If you indent each block of braces as I have then it only makes things far easier to debug.

Following a recent thread by a member today I thought I would post this thread with a practical example in the hope that it helps others out here. The code has been slightly modified to remove the non-necessary html but it still serves as a good example for this demonstration:

PHP Code:
<?php
//This code will generate a missing $end error message
session_start();
require 
'database.php';
$nuser=$_SESSION['user'];
$auser=$_SESSION['admin'];

if(
$nuser){
$userfinal=$nuser;
}elseif(
$auser){
$userfinal=$auser;
}
if(isset(
$userfinal)){
$Members mysql_query("SELECT user FROM characters WHERE level ='1' ORDER BY exp DESC") or die(mysql_error());
$numRowsMembers mysql_num_rows($Members);

for(
$count 1$count <= $numRowsMembers$count++){
$name mysql_fetch_array($Members);

echo 
"<a href=\"member_profile.php?username=$name[user]\"> $name[user]</a></td>";
}
?>
Now I'd like to show you this at work in Notepad++:
Style A:


Notice how the opening { are paired with the closing } in red circled with matching colours. The single { by itself is circled in red. Ok, by pointing at each opening { I've found the { without its partner - but where should I now put the closing } ?

The answer lies in Style B - using indentations and brackets on their own lines. Take a look at this:


Again, you can see all the correctly paired opening { and closing } in red circled with matching colours with the odd { circled in red.

Now due to the original authors code, there is no way of knowing if that for() block was supposed to be inside that block or outside of it. For demonstration purposes only, I'm going to show you what would of happened if it was supposed to be inside:


Did you notice that red dotted line circled in blue? - It shows you exactly where the closing } should be.

Now, lets look at the finished code:


Done, with ease. No time wasted, no head scratching, no convincing yourself there is no error with the code and the computer is wrong etc.

I hope that by posting this topic, some of you will see tha advantages that Style B has when used with a proper editor like Notepad++ and that it can actually help you to fix code rather than make it difficult like style A.
Tags: None Add / Edit Tags
Categories
PHP Tips

Comments