Skip navigation.

Part 1: PHP, Javascript and VBScript Language Summary

This is a summary of the language differences between PHP, JScript/Javascript and VBScript that I did to help my company learn the 3 server side scripting languages. Hope it's helpful to others.

I started out as an ASP developer, and switched to PHP. I have been using ASP since 1998 (we continue to support our customers who are still using ASP), and PHP since 2000 (our main web development platform). In general, my experience with PHP has been very positive. Here are the nice things i found about PHP:

  • No need to purchase additional ActiveX controls for email, gif manipulation, pdf handling, etc. Batteries included.
  • Runs well on both IIS and Apache, Windows and Linux. Cross-platform power.
  • Good performance. PHP is faster than VBScript and JScript, based on the Bagley benchmarks.
  • Even if you are a VB programmer, provided you have some Javascript experience, you are already half-way up the learning curve, as PHP looks like Javascript with $ signs in front of variables.
  • For those of you who miss ADO (i did), there's ADOdb, a database abstraction library that works like ADO. And ASP2PHP is an automated VB to PHP converter.
  • I recommend this PHP installer for IIS (ok, ok, I admit wrote it). The default PHP installer at php.net is not tuned for high performance IIS usage - this one is.

Feature PHP JScript and Javascript VBScript
Example (print 1 to 10 on a web page)
<html> <body>
<?php
for ($i=1; $i<=10; $i++)
print "$i<br>";
?> </body></html>
<html> <body>
<%
for (i=1; i<=10; i++)
Response.Write(""+i+"<br>")
%> </body></html>
<html> <body>
<%
for i = 1 to 10
 Response.Write(i & "<br>")
next
%> 
</body></html> 
Script Tags

<?php    ?>

If short tags are enabled in PHP.ini:
<?         ?>

If ASP tags are enabled in PHP.ini:
<%       %>

<%=  return expression %>

<%      %>

<%=  return expression %>

<%       %>

<%=return expression %>

End of statement semicolon (;) is compulsory End of line or optional semicolon End of line
Comments
// PHP comment 1
/* This is a 
   multi-line
  Comment */
# PHP comment 3
// JScript comment

/* This is a 
   multi-line
   Jscript Comment */
' VBScript comment
Variables Prefixed by $ No prefix No prefix
Need to declare variables?

No, and every variable is preinitialised to type NULL, which is equivalent to false, the empty string and 0.

$AVAR = 123;

Optional. Error if variable not initialised when used.

var AVAR

Optional. Error if variable not initialised when used.

Dim AVAR

Loosely Typed Variables

Yes. This means that the type of a variable can be changed at run-time.

To change variable type, use typecasting or settype( )

Yes Yes
Case-Sensitivity Yes for variables, no for function-names and reserved words Yes for variables and reserved words No
Strings

Delimited by single-quotes, double-quotes or PERL heredoc style.

$avar = 'this is a string';
$avar = "this is a string";
$avar = <<<EOS
  this is a string
EOS;
Delimited by single-quotes or double-quotes. Delimited by double-quotes.
String Concatenation

The . (dot) symbol.

$s = 'A' . 'B';

The + (plus) symbol.

s = 'A' + 'B'

The & (ampersand) symbol.

s = "A" & "B"

String Evaluation

Yes, variables embedded in strings are evaluated if string delimited by double-quotes.

$a = "A";
$b = "$a B";
/* now $b == 'A B' */

No No
Common string constants

Line feed: "\n"
Carriage return: "\r"

Note that single-quoted 'n' is not converted to a line-feed for PHP. Only double-quoted strings are evaluated.

Line feed: "\n"
Carriage return: '\r'

Line feed: vbLf
Carriage return: vbCr

HTML encoding functions

htmlspecialchars($str) Converts < > & and " to their HTML entity equivalents, eg. < becomes &lt;

urlencode( ) All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.

urldecode( )All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.

escape(str) All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding

unescape(str) inverse of escape

Requires ASP Server object (and consequent COM overhead).

Server.HMTLEncode( ) Similar to PHP's htmlspecialchars.

Server.URLEncode( ) Similar to PHP's urlencode.

 

Regular Expressions

Built-in.

if (preg_match ("/php/i", $str))
  print ("php found");

Built-in.

if (str.match(/php/i))
  Response.Write("php found")

Use COM object RegExp. Only available with the latest versions of VBScript (5.5 or later).

Dates
$adate = time();
$adate=mktime(0,0,0,1,30,2000); 
print date('d-m-Y',$adate);
adate = new Date();
adate = new Date(2000,1,30)
Response.Write(''+adate)

Dates are formated using the current locale configured in the control panel, so you have to hand-code date formatting or change your locale.

adate = Now
date= CDate('2000-1-30')
 	
FormatDateTime(adate,2) 

Dates are formated using the current locale configured in the control panel, so you have to hand-code date formatting or change your locale.

Arrays

Declare with
$avar = array(1,'two');

For indexing use [ ]. Arrays begin with zero element.

Use sizeof(array) function to get size of array.

Arrays can be iterated over using:

$val = reset($arr);
$max = sizeof($arr);
for ($i=0; $i<$max; $i++) { print "$val<BR>"; $val = next($arr); }

/* or more simply */

foreach ($arr as $element)
print "$element<BR>";

Declare with
avar = new Array(1,'two');

For indexing use [ ]. Arrays begin with zero element.

Use array.length to get size of array.

Declare with
DIM avar(2)

For indexing use ( ). Arrays begin with zero element.

 

Associative Arrays

Yes

$avar = array();
$avar['newton']='isaac';
echo $avar['newton'];

Note: You can append new elements to arrays with
$avar[] = 'string to add';

Yes

avar = new Array();
avar['newton'] = 'isaac';
Response.Write(avar['newton']) 
Not built into the language, but can simulated using the Dictionary object (which is not thread-safe in VBScript 5.0).
True and False

Has constants true and false.

The following also evaluate to false:

0   /* zero */
""  /* empty string */
"0" /* string with zero */

Has constants true and false.

Empty strings and zero will evaluate to false.

Has constants true and false.

Unlike PHP and JScript, zero does not evaluate to false.

Equality
== true if equal
!= not equal

=== true if equal & same type !== not identical

Note that some functions can return 0 or false (false is typically used to indicate failure) depending on the context. For these functions, you will need to use === to determine whether 0 or false was returned. Some problematic functions: strpos, strrpos, strstr, stristr, strrchr

== true if equal
!= not equal

=== true if equal & same type !== not identical

JScript does not have the 0/false problem.

= true if equal
<> not equal
 
Assignment statements

Allows C style shortcut assignments. For example, to append a string to a variable:

$avar .= 'append to end';

Allows C style shortcut assignments. For example, to append a string to a variable:

avar += 'append to end';

No shortcut assignments.
Sending HTML to Browser print $avar;
echo "The avar variable '$avar' is a string";
Response.Write(avar); Response.Write(avar);
If statements
if (strlen($avar) == 0) {
  $avar = "abc";
} else
$avar .= 'end';
Similar to PHP and C.
if len(avar) = 0 then
  avar = "abc"
else
  avar = avar & "end"
endif
While statements
while ($a > 0) {$a -= 1;}
while (a > 0) a -= 1;
while a > 0
  a = a - 1
wend
For loops
for ($i=0,$m=9; $i<$m; $i++){
print $i; }
for (i=0,m=9; i<m; i++) {
 Response.Write(i);
} 
for i=1 to 100
  Response.Write(i)
next 
Switch/Case
switch($aColor) {
case 'red':   do1;break;
case 'green': do2;break; 
default:      do3; break;
}
switch(aColor) { 
case 'red': 
 do1
 break
case 'green': 
 do2
 break
default: do3; break;
} 
select case aColor
case "red"
   do1
case "green"
   do2
case else
   do3
end select

Part 2 Functions, Classes
Part 3 ASP Objects and PHP

Comment viewing options

Select your preferred way to display the comments and click 'Save settings' to activate your changes.

hii

Good day