Top Dog - Top 5 Intermediate PHP tips

June 25th, 2008 by Fritz

1. Use/Build a framework

If you do more than a few PHP projects you will want to look into frameworks.  Yes, at first frameworks are daunting.  But they don’t have to be.  This is why I like to recommend the idea of building your own.  Most likely you already have code and techniques you use regularly that would fit in perfectly.  You will also avoid a “learning curve” of using a standard framework.  I built my own framework using help from an article over at ONLamp.com: Understanding MVC in PHP.  The article is a great introduction to MVC and the framework he builds during it is small and functional.  I tweaked it in a variety of ways that worked for me and made modifications to include some of my favorite code/techniques for things like user authentication and database abstraction.  Give it a read, maybe it will work for you… if not you can always try some other popular frameworks like cakePHP or Zend.  To me these are too bulky with things I don’t want, but to each his own.

So, if you’ve never paid much attention to framework, you’re probably thinking “what’s in this for me?”  Frameworks provide a number of things.  They give you a common codebase with which to start any site.  They usually provide built in user authentication, database handling, and error handling so that you don’t have to rig up each site to use these things.  A good framework will establish these things with no extra code from you.  They will also provide you with a structure to your site and in an MVC provide logic/display seperation in one way or another.  Before long you will wonder how you ever got along without a trusty framework.

My Framework DIslcaimer: Frameworks can be big and scary at first and the concept of MVC even more confusing.  I read MVC/framework articles over and over and over before it really sunk in.  So don’t give up early, keep reading and keep trying to grasp the concepts.  Once you do you’ll realize they really aren’t that bad.  Some people may pick the idea up quickly and easily but don’t give up if you don’t get it the first few times.

2. Become a SQL ‘Top Dog’.

PHP (and any programming language) is just a way to manipulate and do cool things to data.  But you aren’t giving yourself a fair chance if you don’t properly understand how to get/create that data.  GROUP BY, LIMIT, UNION, JOIN, Aliases, Sub-Selects, WHERE EXISTS, Views… all of those things ring a bell right?  If not you better get to work brushing up on your SQL.  Data should often come to you on a SQL platter.  You shouldn’t have to get data out and then manipulate it before it’s even ready to “use”, or use the data you got out to go get more that yo need.  A lot of intermediate/beginners make these makes because it’s possible to get by with only a 20% knowledge of SQL.  However this will cost you tons of time, effort, extra code, and effeciency in the end.  Convinced yet? Get Reading.

2a. Become a database ‘Top Dog’.

A poorly structured database is a painful thing to see.  Database normalization along with a solid understanding of keys (primary, foreign), cardinality, and structure can go a long way in making your PHP product better.  Just like above… if you are not designing your database right you are introducing tons of extra code and potential bugs into the application.

3. Don’t be afraid to re-invent the wheel.

A PHP master or professional will often tell you “Don’t re-invent the wheel!!!”.  They argue that someone has already written code snippets for many of the things you need/want and that you should save time by using them.  I disagree with this approach for beginners/intermediate coders.  Sure if you’re a pro and money is on the table then time is everything.  But theres no better learning tool than writing code.  You have so much to learn by coding your own stuff.

This will also help you when the code breaks.  We all know we should comment out code because when you come back 6 months later you will have no idea what it was doing.  But it is ALWAYS easier for you to debug code you wrote than someone elses, no matter how long its been since you saw it last.  Having everything coded in your style will be much easier to think through and fix later.  And sure, your attempt may be 20 times less efficient than a snippet from a master coder but when was the last time something you did in PHP dealt with such a load of data and processing that effeciency really became an issue?

4.Master the array

The array is one of the strongest tools in PHP… arrays are fast, easy, non-typed, and easy to pass around.  Learning to store your data in arrays and pass/manipulate those arrays well is an important php skill.  I find that when dealing with actual application data I often forget about object orientation and instead using arrays to store my “objects.”

Quick example: Pull data from the database into an array… display that array in the form elements on the screen.  Using name=’data[field]’ on those form element makes the form itself return an array.  From that return I take the resulting data array, pass it off to a function that updates the database based on the values in the array.

Thats a simple example that many novices will spend far too many lines and variables accomplishing.  With the simple fact that you put anything in an array, create associative key or numbered key arrays, and pass arrays from one function to another dealing with data has never been easier.  I highly recommend writing your own functions that take arrays and parse them into SQL statements for your database.  This makes data manipulation easy as pie (mmm I love pie).

5. Don’t be afraid to start things and not finish them

Again if profit for your time is your priority you may not want to follow this… but don’t be afraid to start something. If you feel enthusiastic about an idea or just want to try something… start coding! It may turn into something great or you may never finish it. Don’t worry because either way you will learn things you didn’t know before. I’ve learned 50% of what I know from “play” projects, most of which I never finished. Don’t worry… every bit you learn will help you in the long run. Don’t be afraid to start projects and don’t fret having unfinished projects laying around.

If you’re thinking “hey this dog doesn’t know anything! I came in here for 5 PHP tips and I don’t even see any code!” Well… you should realize that 75% of being a good programmer is planning, theory, and approach. Anyone can hack out some code to do something. But truly writing good code takes a lot more thought and structure than just learning “coding tricks.” So spend some time improving your approach…

How To: Use Synergy In Ubuntu

June 23rd, 2008 by Fritz

To follow up on my review of Synergy. I decided to do a quick how to on using it with Ubuntu.  The Windows version has a nice GUI but things are a bit trickier in Ubuntu.

Install

The install can start a bit tricky.  Synergy’s website offers an .rpm file here.  Of course the Ubuntu package manager doesn’t handle RPM and trying to open it is going to give you an error.  To fix this use Alien.  If you don’t have Alien:

sudo apt-get install alien

Next you will need to use alien to open the rpm:

sudo alien -i synergy-1.3.1-1.i386.rpm (or whatever path/filename for the version you downloaded)

And that it, assuming you don’t get an error you’re not installed!

Server

For the server you must create a config file to tell it what screens you have and how to handle them.  For my example lets say the hostname of my server is fritz-desktop and my client will be fritz-laptop.  Here is an example config file:

section: screens
   fritz-desktop:
   fritz-laptop:
end
section: links
   fritz-desktop:
      right = fritz-laptop
   fritz-laptop:
      left = fritz-desktop
end

This example tells it to switch to the laptop when you exit the desktop screen on the right side and to return when you exit the laptop screen on the left.  Creating this file is easy, just drop it into your favorite editor and save it.  For my example I’m going to save it as fritz.conf.  I also recommend you put it in your home directory (/home/fritz/) or wherever you keep your user specific config files.
Next bring up a console and enter the command to start the server:

synergys -f –config /home/fritz/fritz.conf

This will start the server in a “test” mode as you’ll be able to tell since it locks up the console.  Start up your client and connect.  Everything work?  Good now hit cntrl-C to kill it and from now on you can run using:

synergys –config /home/fritz/fritz.conf

This (without the -f) will run it in the backgroud (cntrl-C will no longer kill it and you have to kill it from the process monitor).  You can also set this to start up everytime you start Ubuntu by using System->Preferences->Sessions and add a new one for this given command.  Then your synergy server will always be running waiting for a client to connect.

Client

The client is even easier.  To launch the client no config file is needed and instead simply open a console and use the command:

synergyc -f fritz-desktop (or whatever your server hostname is)

*NOTE: I had trouble with this and instead had to connect to my Ubuntu server using the IP.  In most places above you can subsititute the IP of the machine for the hostname.  So if the hostnames aren’t working or you don’t know what they are try the IP (but be aware, IPs can change depending on your network settings)



One final command you may find useful to avoid changing your config.  Lets say I have my sisters laptop with the hostname of heidi-laptop.  Well I don’t want to have a seperate config file on the server.  I can do this by using this command:

synergyc -f –name fritz-laptop fritz-desktop

In this case the server can’t find heidi-laptop in the config file so it doens’t know what screen is connecting. To avoid this issue we can use –name to tell it which name in the config file this PC is. So this tells the server to conser Heidi’s Laptop as “fritz-laptop” in the config file. Which in this case places it off the right of the desktop screen.

Synergy - It’s 10pm, do you know which screen your mouse is on?

June 22nd, 2008 by Fritz

I recently moved into my new home office and decided to simplify by removing the smaller of two desks I used to use. But this left me with a problem: How do I easily integrate my work laptop into the room? Before my 2nd desk was used for the work laptop, while never my favorite setup it did work… My main desk is also too tall to comfortably use a laptop sitting on it (I use the keyboard tray for my laptop). My original plan was to get a KVM switch to allow both PCs to share a monitor, keyboard and mouse. But then I stumbled upon Synergy.I can’t say enough good things about this software but lets start with this: IT’S FREE!

Synergy is simple, it uses your home network to “share” a mouse and keyboard between computers.  To use it you must setup one of your PCs as the “server” that is sharing its mouse and keyboard.  I used my desktop for this as its always here and the server software can be always running on it.  Once you’ve installed the software you need to configure your screen’s (computer’s) names and setup the linking relationship (for instance I have the right side of my desktop going to the left of my laptop and vise versa.  Once thats done you click start… its that easy.

Next you connect the client by specifying the ip or hostname of the server… hit start and boom you’re off and running.  A quick slide of the mouse reveals that it easily switches from screen to screen, computer to computer.  The keyboard enters text on whichever PC the mouse is on at the time.  This allows me to easily work on my laptop while continuing to use my home PC at the same time and without the trouble of switching anything.

And to make it even better it runs on Linux, Mac and Windows… so I’m currently running the server on Ubuntu, and my Windows laptop is connected as a client… I can easily switch the mouse/keyboard between the two!

Here’s a quick rundown…

Advantages:

  • Free
  • Works on any OS (well Linux, Mac and Windows anyways…)
  • Quick, easy, and painless, no physical connection between the PCs other than a standard network setup
  • Nice windows GUI to setup your screens and links

Disadvantages:

  • Doesn’t share the monitor like a KVM switch (for my situation I didn’t really want to do this anyways, the nice part of having the mouse and keyboard shared is that I can use BOTH PCs at the same time without losing ones screen)
  • Uses the network so any load/lag on the network can cause the mouse to stutter on the client machines.  This happens to me occasionally but it’s usually pretty stable.  This will depend on your network connection quality and speed.
  • No GUI for Linux and Mac… you have to manually write a config file instead.

Overall?  Great piece of software that saved me $100 in a KVM switch.  If you have two PCs and don’t want to both with two sets of Mice/Keyboards I highly recommend you give this a shot.

Review - Aliph Jawbone Bluetooth Headset

June 16th, 2008 by Fritz

Do you have a cell phone?  Of course you do… I mean come on, I’m a dog and even I have one…

Do you have a Jawbone?  WHY NOT?!  If you have a cell phone this is the only way to go on bluetooth…

Prior to my jawbone I used a Motorolla H700.  It wasn’t terrible but it wasn’t good.  It was often far too quiet and picked up way too much background noise.  But it was cheap and easy if you want cheap I still recommend it (I got mine back when they were new for $30 on ebay).  However, if you want a high quality bluetooth headset then you need the Aliph Jawbone. You won’t find any of these on ebay for $30 but I got mine for $66… well worth every penny.  So anyways, here my review:

First, this thing boasts Military grade noise canceling technology using a little nipple thingy that rests on your jawbone (thus the name geniuses…).  The idea is the jawbone detects what your saying from your jaw and filters out the rest.  This works, but not as well their demo.  I spend 95% of the time with the noise shield on.  The first day I had it I turned it off during a call (with only my TV on as background noise at normal volume) and immediately had complaints along the lines of “whats that noise!?”  Without the noise shield the thing is super sensitive.  With the shield on it works pretty well and people on the other end hear you despite background noise.  One thing to be aware of though, the thing still heavily picks up wind.  So whereas it will filter out the background noise if your outside, if there is any wind blowing the person on the other end will hear nothing but static.  So don’t expect to use this with the windows down in your car or while walking in a steady breeze… it’s not that perfect.

The next big bonus is the volume… you can actually hear your calls pretty much anywhere… with 5 volume ‘levels’ there is always a volume for the situation your in and the thing gets pretty loud when on the highest setting.  However, as with most phones, if you turn the volume up high in a quiet room your mic will most likely pick up noise coming out and people will complain of an echo.  This is strange as when noise is coming from your earpiece you are not talking and thus your jaw is not moving… but like I said, the noise cancellation on the mic isn’t perfect.

The last big piece to discuss is the physical device itself.  First off, its slick looking… not dorky and plastic like most bluetooths today.  It comes in black, grey, and red for those of you who like it wild.  And now there are new thinner models out with a more high scale look.  Not only does it wear well but it also fits well.  Because of the nipple that needs to stay on your face it has a spring in the ear hook that keeps it pressed to your face, this is nice and i sometimes wish the spring was even stronger to keep it securely in place.  There are several different ear hooks (left and right ear) and ear buds (from round ones that sit on top to long thin ones that go down in the ear) so you can find something that fits comfortably for you.

Overall, I highly recommend the Jawbone.  It’s the highest quality bluetooth headset overall and well worth the extra money.  I rarely ever hold the phone up to my face now…

I’m back!

June 16th, 2008 by Fritz

Spent the last couple weeks preparing to move and moving into my new place (packing with your teeth takes time…)  But I’m back in action and will try to keep the posts coming.

Being Tired is Not a Status Symbol *YAWN*

June 2nd, 2008 by Fritz

Great article… check it out here.

But man I wish it was…

These days people will use anything to try to prove how much better they are than you.  From posessions to fatigue… I couldn’t agree more with the author, showing the world how miserable and tired you are doesn’t prove anything…

www.flickr.com
fritzthetechdog's items Go to fritzthetechdog's photostream