ASP VBScript for PHP programmers
I wrote this little manual for those PHP programmers who as by some unlucky reason are forced to write something in ASP's VBScript. And yes, you have to be forced to do that because I think VBScript is a real pain in the ass to use.
So, I put up a simple list of suggestions and solutions for problems I had to learn and overcome when I was migrating my already working CMS system from PHP to ASP (don't ask me why ok? just follow me: "management department sucks")
So.. that ASP VBScript is:
- it's a scripting language for MS IIS server
- it is highly object-oriented language
- it sucks because most of object are half-baked stuff (reverse array, anyone?)
- it sucks in easy-of-use and implementation department comparing to PHP (and feels like a language from stone age)
- a language where databases access classes are actually not bad at all (one you learn how to use them)
- an annoying language where you have to define every single variable you use in advance (via "Dim myvar") and of course I know that defining vars is a good style and all, but I don't think it is really necessary when your "program" lives less than a second and then all its data gets destroyed
A bit about programming problems and solutions.
File upload handling
There are no dynamic arrays
yes. nothing like $myarray[] = "new element". Also, you can't sort, reverse and almost do anything useful with VBScript array. But there are solutions
Multiple-lines operators
as lines in VBScript are not ended with ";" like in PHP, it's not easy to split long strings for several lines. The solution is to use the "_" operator. Have a look below:
Dim myLongArray
myLongArray = Array("ok here we go", _
"next element goes here", _
"you got the idea")Dim myLongString
myLongString = "Some strings can actually " &_
"be pretty long and boring and span " &_
"multiple lines"
'defining constantsConst CONST_DSN_NAME = "cms"
Const CONST_DB_USER = "sa"
Const CONST_DB_PASS = ""Dim objDBconnString = "DSN="&CONST_DSN_NAME&";UID="&CONST_DB_USER&";PASSWORD="&CONST_DB_PASS'creating database connectionSet objDB = Server.CreateObject("ADODB.Connection")
objDB.connectionString = connString
objDB.Open'making a simple queryDim qstringqstring = "select * from mytable"set rs = Server.CreateObject("ADODB.Recordset")
rs.Open qstring, objDB, adOpenStatic'print resultResponse.Write "data:" & rs("field_name")
getenv("DOCUMENT_ROOT")?
You might wonder if there is an analog for this function in ASP? There is an analog. Just do this:
Dim docRoot
docRoot = Server.MapPath("/")
SET your object vars
Yeah I bloody know that different syntax for defining a normal variable and variable which actually contains class is not very convenient, but VBScript does just that. So.. if you want to store just a string or a number, you write
Dim myvar
myvar = "easy breezy"
But if you want to store an object in the variable (a Recordset or an Array for example), do this:
Dim myvar
set myvar = new Server.CreateObject("ADODB.Recordset")
Inconvenient. I know.

