If I buy an unlocked iphone 3, 4, or 5 can I just put my sim card in it?

I have a Motorola Atrix 3G and the 2GB data plan on AT&T. Can I buy an iphone 3, 4, or 5 off of the internet that is unlocked and simply put my sim card in it? As I said above, I already have the data plan. So will that automatically just work in the iphone? Do I need to get a new contract or something? Will AT&T bill me for something simply because I bought the iphone from an outside source and just stuck my sim card in there? I have no idea if this is illegal or not to do so can someone also answer that for me please? Sorry for all the questions haha, and thank you in advance!

Yeah I Think So

Of course, it's unlocked so it should be able to work internationaly also. However you'll have to cut the sim into a micro sim if your getting an iPhone 4 or 4s. If your getting an iPhone 5 the sim has to be even smaller, you can cut it by going to an AT&T store, and it's not illegal. You won't need to get a new contract.

I probably don't live in the same country as you, but how phone contracts work in my experience is this:
You pay monthly for a set number of months for a call, text and data plan.
A phone is included in the deal.
More expensive phones affect the monthly payments.
In my experience the contract is for the plan connected to the SIM card and the phone is just an extra. It is possible to use the SIM card and this the plan on a different phone, after all the contract is for the plan, not the phone. If you buy a phone capable of using the SIM card, the you can use it, it isn't illegal and doesn't breech your contract.
Just one note: the iPhone 4S uses the micro sim and the iPhone 5 uses the nano sim. Your SIM card might not be physically able to fit into those phones, unless your current SIM is a micro or nano SIM, I'm not familiar with the size of the SIM required for the Motorola Atrix 3G.

So if I have a sim card from my Iphine 3 can I buy a iPhone 4 and put it in there? Will it work just like my iphine 3?

Can my iPhone 5s work with a sim card