Etc : Nano - coding

Coding

I hand code this web site using HTML to provide the content of each page, CSS to handle how it looks (pretty ain't it, OK it's like the back end of a school bus), a dash of JavaScript (Salt and Vinegar) for flavour and most importantly PHP to get the Web Server to stick it all together. Learning the above is a good introduction to coding, would you like to try...

The first section of new commands help with coding, so lets try them out by looking at a small piece of code greeting_errors.php that uses PHP and some HTML but it needs troubleshooting, Right Click greeting_errors.php and Choose to Open Link in new Tab to see what happens, NOTHING!!!.

In order for you to view the code I used the cp command to Copy greeting_errors.php and save it as greeting_errors.php.txt.

$ pwd
/var/www/html/etcetera.ie/nano/
$ cp greeting_errors.php greeting_errors.php.txt

To see the code Right Click greeting_errors.php.txt and choose to Save Link As into your nano folder.

With Nano running do a Ctrl+R to Read and then make sure to do a Alt+F for a new buffer and type in greeting_error.php.txt or Ctrl+T to browse to it.

Nano deleting .txt to show file as php

Nano reads configuration files in /usr/share/nano to highlight code including nanorc.php for PHP, nanorc.html for HTML, nanorc.css for CSS etc, so in order to see the php file in all its glory you need to rename it, dropping the .txt file and the quickest way to do that when you have it open in nano is to Ctrl+O Write Out and then use the backspace key to delete .txt and hit Enter. When prompted to Save File under a Different Name hit Y and now you are editing greeting_errors.php

On line 1 the <?php starts a section of php code and it is finished on line 15 with ?> so this should be an easy fix.

Nano PHP Error#1- Missing quotes on an echo command

When I tried this link on my laptop, which is running a LAMP Server that is Linux, Apache Web Server, MySQL database and PHP with the PHP set to display errors, I get the following below...

Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN _CURLY_BRACES) or {$ (T_CURLY_OPEN) in /var/www/html/etcetera.ie/nano/greeting_errors.php on line 16

Not very helpful since line 16 is the last line, but thanks to nano I see something is wrong with line 12...

Nano PHP Error#2: Missing end of line.

The echo command causes what is written between quotes to be printed to the screen, so a quote first, then a piece of HTML <hr> horizontal rule, which gives a nice horizontal line, then some text, then another <hr> and finally closed with a quote, which I forgot, so add one in and straight away the colour of the line changes looking like the echo commands on lines 3 and 7.

Happy days, time to hit that refresh key F5 to reload greeting_errors.php and see did that fix the issue.

NO, yet more errors...

Parse error: syntax error, unexpected 'goodbye' (T_STRING), expecting ';' or ',' in /var/www/html/etcetera.ie/nano/greeting_errors.php on line 14

OK, although its telling me the error is on line 14 the problem is still with line 12, I forgot to finish it off with a ; semi-colon, which shows PHP the end of a line of code, lines 3,7,10 and 14 all end with a ; ,so I put one at the end of line 12, saved the changes and reloaded the page.

Parse error: syntax error, unexpected end of file in /var/www/html/etcetera.ie/nano/greeting_errors.php on line 16

NOT AGAIN, taking deep breaths, I know whats wrong, look at lines 2 to 4, this is a function, a piece of code that will be run when called, its name is hello() and it will echo out the piece of text between the quotes. The function is defined between an left angle bracket { and a matching right angled bracket }

Nano has a nifty feature to check out matching brackets, select { on line 2 and then do a Alt+} and you will see the } on line 4 highlighted, perfect.

Nano PHP Error#3: Missing matching brackets

Now select the { on line 6 and again do a Alt+} to find that there is No matching bracket, one was needed on line 8 to finish off the function. The PHP server went through the rest of the code thinking it was all part of the function goodbye() and then gave out that it had reached the end of the file with no closing }.

Stick a } into line 8 and try Alt+} again, yes error number 3 is fixed.

Matching Brackets are a brilliant feature of nano and any other decent text editor, its common to have lines of code with many levels of brackets and very easy to forget a closing bracket. This throws the entire code into error and without that feature, it can take some time to find the cause.

OK, that was the final error, I don't want to turn you off coding but these are classic mistakes. I saved the changes and reloaded the web page and finally..

Nano and Firefox with a greeting success

And this is the error free version greetings.php

A good method to troubleshoot code is to comment out parts of it, say for example you suspect that running the hello() function was causing errors, select that line and then do Alt+3 to comment out that line a // will appear at the start of the line, the colour changes and that piece of code won't run.

Nano: PHP comments.

Nano will also use // to comment out lines of JavaScript code.

Nano: JavaScript comments.

For CSS it will use /* at the start of a line and */ at the end of the line.

Nano: CSS comments.

For HTML it will use <!-- at the start of a line and --> at the end.

Nano: HTML comments.

HAPPY CODING, now lets play with some more nano commands....