• Home

CodingExperiments.com

CodingExperiments.com is a site where I can (obviously) experiment with various demonstrations of code.

Search

Category:

  • AJAX
  • Announcement
  • Apple-related
  • Best Practices
  • Blogger
  • Blogging
  • BurstCMS
  • Content Management System
  • Debugging
  • Experiments
  • FriendFeed
  • Gaming
  • General Code
  • Internet
  • Javascript
  • Linux
  • Microsoft
  • Microsoft Windows
  • Networks
  • Open Source
  • PHP
  • Programming Tips
  • Rant
  • Security
  • Storage
  • Twitter
  • Ubuntu
  • Uncategorized
  • Web Development
  • Windows Vista
  • WordPress

Archives:

  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007

Pages

  • About
    • The Authors
  • Commenting your code
  • How to Write Papers with Groff
  • ModCMS Anti-Spam Component Set
  • ModCMS Technical Specifications
  • Regular Expressions Guessing Game
  • Saving code directly to a web server
  • The (Almost) Perfect PHP 404 Page

Meta:

  • RSS
  • Comments RSS

Awesomeness tracker

CodingExperiments at Blogged View blog authority
Free Page Rank Tool

Beginner PHP Programmer Mistakes Part Two: Going Nutty with Exceptions

June 27th, 2008 by possible248

You’re a beginner PHP programmer, and your server has just been upgraded to PHP 5. Yay! Time to rewrite that error handling code to include exceptions! One mistake that beginner PHP programmers make is that they go crazy with exceptions. They might turn the below code:

function checkUsername ($username)
{
    if (!is_string ($username))
    {
        die ('The username is invalid');
        /* Also acceptable are:
        *     trigger_error (<something>);
        *     ^-- If you the proper error handling
        *         setup for site visitors.
        *
        *     return <something>;
        *      ^-- Provided that the caller saves
        *      the return value and that the
        *      value is checked.*/
    }
    else
    {
        return true;
    }
}

oldCheckUsername ($username);

into:

class OMGYourUsernameIsNotValidException extends
Exception {}

function checkUsername ($username)
{
    if (!is_string ($username))
    {
        throw new OMGYourUsernameIsNotValidException
        ('Username = fail');
    }
    else
    {
        return true;
    }
}

try
{
    checkUsername ($username);
}
catch  (OMGYourUsernameIsNotValidException  $e)
{
    echo $e->getMessage ();
}

Exceptions are best used for things like a database connection failing. They aren’t really for simple things like form validation.

Uncaught PHP exceptions are considered E_FATAL errors, and I consider that a good measurement of whether or not you need exceptions. While you might want to terminate script execution in the case of improperly filled in forms, you wouldn’t want to use that error level for form validation.

A failed database connection seems to fit well into E_FATAL, and by that logic, it’s okay to use exceptions for it.

Disagree? Know a better way to handle errors in PHP? Tell me in the comments.

Posted in Best Practices, PHP | Comments

Python, Javascript, And PHP as Languages for Beginners

June 7th, 2008 by possible248

Introduction

Python, Javascript, and PHP are all known as languages that beginners can use to sink their teeth in programming. Those three language are also frequently used in many commercial applications. Given all these three choices, which language should a beginner learn? Well, here’s the features of the three languages broken down.

Python

Pros:

  • Cross-platform
  • Used on the desktop and server
  • Has a module for everything that you could ever think of
  • Has good documentation

Cons:

  • Indentation is required, and errors will come up if code is not indented properly. UPDATE: A bunch of people told me that they think indentation is good as it teaches newbie programmeres to properly indent their code. I understand the good part of this, but I think that forcing indentation seems extreme. I guess whether this is a con or not is completely up to the developer.
  • It may take some Googling to figure out how to use a certain module. I can tell that from personal experience.
  • It’s not HTML-embedded like PHP or Javascript, so it’s a little more work to write web applications with it.

Python is ideal for the person that wants to create cross-platform applications on the desktop and server, and doesn’t mind having to install Python if they’re on Windows.

Javascript

Pros:

  • All you need to execute Javascript is a modern browser, which is something available on plenty of computers.
  • Reduces server load in web applications
  • With AJAX-style Javascript coding, you can make web applications more like desktop applications
  • In the form of Greasemonkey scripts and similar, Javascript can be used to modify the functionality of other webpages.
  • Since Javascript works with HTML, you can create user interfaces much more easily than with Python.
  • Javascript can be used to make desktop applications with things like Adobe AIR.

Cons:

  • You can’t expect Javascript to act the same everywhere. Different web browsers handle Javascript differently.
  • Users can disable Javascript in their browser, or have a web browser that’s too old for your application.
  • Because of the the first con, Javascript is harder to write and debug

Javascript is a good first language for people that are willing to put work into writing and debugging code, but they do not want to install things. It’s also good for people that want to make HTML pages easier to use.

PHP

Pros:

  • Is server-side like Python, but shares the same ease-of-interface making as Javascript.
  • Is HTML embedded. This makes it even easier to make user interfaces in HTML.
  • You can bet on recent versions of PHP being available on (most good) shared hosting servers due to it’s demand.
  • Has good documentation

Cons:

  • It’s easy to shoot yourself in the foot.
  • Not good for desktop applications at all.
  • The community is filled with newbie programmers showing others code examples with bad programming practices.

PHP is good for people that want to develop server-side web applications quickly, and are okay with the application not being as fast (I can’t find the link to the testing that shows that Python is faster). It is also a good idea to buy a book on PHP in order to learn all the bad programming practices that are often showed in newbie code examples that are posted on the Internet.

Posted in Javascript, PHP, Programming Tips | Comments

PHP Is Good. Yeah Really, Hear Me out Here

May 25th, 2008 by possible248

Introduction

PHP is an odd and inconsistent language. Many developers agree on this, but some of those developers still like PHP while others hate it. A few days ago, a post titled “PHP Sucks, But It Doesn’t Matter” appeared on Coding Horror. That post by Coding Horror is one of many negative PHP posts. I believe PHP is good, but I’ll also cover some of the bad points

Blessings

1) PHP has one of the best documentations, that programmers can add comments and code samples to.

This is one of the things that make PHP an easy-to-learn language. The top-notch documentation will usually help you find what you are looking for. If it’s not in the documentation, there’s always something interesting in the comments.

The bad side: Beginners often add code examples that use bad programming practices, resulting in more beginners using those bad programming practices

2) PHP is everywhere.

You can find a decent version of PHP everywhere. Bluehost, the one that I use for this site, has PHP version 5.2.6, which is currently the latest version. Bluehost, unfortunately, only has Python 2.3.4, which makes it difficult for me as a lot of my scripts rely on Python 2.5. Because PHP is so popular, web hosts work harder to cater to the demands of PHP developers rather than Python developers.

The bad side: While PHP is everywhere, the php.ini can be used to change the environment, making it difficult for PHP scripts to run everywhere.

3) PHP is HTML-embedded.

This makes it easy to create templates with HTML pages with one-line PHP scripts inserting the content that goes here or there.

The bad side: This makes it easy to escalate to spaghetti-code. However, it doesn’t mean that it is impossible to write good code in PHP.

4) PHP is beginner-friendly.

This makes it so non-programmers can quickly pick up PHP and start a web service

The bad side: It’s way too easy to have security holes and other bad programming practices in code. This is the programmer’s fault.

5) PHP is open-source.

Being an open-source fan, I love using an open-source technology.

The bad side: For now, businesses don’t take PHP seriously. This is partly because it’s open-source. This isn’t the fault of the language, but the attitude that people take to it.

Curses

1) PHP has inconsistent function names.

There is no one naming scheme for functions. You have the function strpos (); and str_replace ();. It takes a bit of practice to remember the naming conventions.

The good side: You can always consult PHP’s excellent documentation if you get confused.

2) PHP is slower than a lot of other languages.

Python and other languages are faster than PHP at doing a lot of things. PHP simply loses here.

The good side: It’s still a good language to for beginners that are not performance-crazed.

3) PHP developers don’t get paid a lot.

It probably stems from PHP being open-source and that there are many beginner PHP developers out there. For more reading, you can check out “Dealing with the low PHP salary problem“.

You could argue that this is not the fault of PHP itself, but this is still an important issue.

The good side: I’m not sure on this one.

4) You can make bad code easily.

Making bad code means that you will have to spend more developer time going back and fixing it. Recoding introduces new bugs which means even more time has to be spent fixing the bugs.

The good side: This one is the programmer’s fault, but plenty of anti-PHP blog posts list this as a reason on why PHP is bad. I think that you can make bad code in any language. Making a language idiot-proof is a waste of time. As the saying goes, “If you design something idiot-proof, the world will design a better idiot.”

Conclusion

It’s totally your opinion whether PHP is good or not, but I would like to point out that it isn’t as bad as everybody says it is. Yes, you can write horrible code with PHP. That’s the programmer’s fault. It’s wise for a beginner programmer to learn PHP through reliable sources instead of PHP documentation comments showing off bad code.

Some of the things, like PHP being slower, not being taken seriously by companies, and developers getting paid less will simply have to be fixed as time passes on.

Posted in Open Source, PHP, Uncategorized, Web Development | Comments

« Previous Entries

 
Wordpress Themes by and Website Templates by Blogcut Blogged Blog Directory Blog Directory - Blogged