Skip navigation.

Part 2: PHP, JScript and VBScript Function and Class Syntax

This summarises the syntax differences when declaring functions and classes for the different languages:

Part 1 Language Basics
Part 3 ASP Objects and PHP

PHP JScript and Javascript VBScript
Function Example
function inc($val)
{ return $val + 1;}

Procedures are functions that return no value.

function inc(val) 
{ return val + 1;}

Procedures are functions that return no value.

function inc(val)
  inc = val + 1
end function

' procedure sub inc2(byref val) val = val + 1

end sub

Class Example
class parent {
 	var property;
  function parent() { }
 	function method() { }
} 

/* inheritance */ class child extends parent {
var property='new value'; }

class parent { 
  property=value
  function parent()
function method()
{ }
}

Inheritance not supported.

Not available
Scope of Variables

Variables defined outside a function or class are available globally; variables defined in a class or function are available locally.

Unlike other languages, you must declare a global variable in a function or class before you use the variable, using the keyword global.

$globalvar = 1;
function show_global( )
{ global $globalvar; print $globalvar; }
Similar to PHP, except no need to declare global variables in functions. Similar to PHP, except no need to declare global variables in subroutines nor functions.
Call by Reference Use & keyword in function parameters. Simple types passed by value, complex types by reference in functions. Use ByRef keyword in Sub or Function parameters.
Default Parameters

Yes.

function A(param1="abc")

No No
Returning by Reference

By default, return is by value. For objects and arrays, we want to return by reference for superior performance. Use

function & getarray123() {
 	$val = array(1,2,3);
 	return $val;
} 

Then in the calling code, use

 $val = &getarray123();
Not an issue Not an issue
Classes

Inheritance and constructors supported. Parent constructor not called automatically during object creation, only derived constructor. No multiple-inheritance.

class child extends parent true

Classes and constructors supported. No inheritance. Primitive form of classes in newer versions of VBScript. Constructors and destructors available. Inheritance not supported. See Using Classes in ASP
Error Handling

Use @ to prevent a run-time error from halting execution.

$val = @function_can_fail();

The scope of @ is the current statement. The last error can be checked in $php_errormsg if track_errors=On is set in PHP.ini.

PHP5 supports try-catch exception handling.

Use try and catch.

try {
  function_can_fail()
} catch(err) {
Response.Write(err) }

Use On Error Resume Next to ignore run-time errors.

On Error Resume Next
 call sub_can_fail
 call sub_can_fail2 

The scope of this statement is the current procedure. Use the global Err.number and Err.description to find out what was the last error number and description..

Newer versions of VBScript support try and catch.

 

Part 1 Language Basics
Part 3 ASP Objects and PHP