Learn PHP using your VBScript knowledge
Welcome to your journey from VBScript/Classic ASP to PHP! This guide is specifically designed for programmers who are comfortable with VBScript and Classic ASP and want to learn PHP for modern web development.
PHP (PHP: Hypertext Preprocessor) is a server-side scripting language designed specifically for web development. Coming from Classic ASP, you'll find PHP familiar in many ways since both are designed for creating dynamic web pages. However, PHP offers more modern features, better performance, and runs on multiple platforms.
Coming from VBScript/Classic ASP, you'll find PHP offers several advantages:
PHP shares several concepts with Classic ASP that will make your transition easier:
Before we dive into the comparisons, here are some fundamental differences:
Let's start with the most fundamental differences in syntax between VBScript and PHP.
VBScript (Classic ASP):
<%
Response.Write("Hello World!")
%>
PHP:
<?php
echo "Hello World!";
?>
PHP offers several tag styles (though only the first is recommended):
<?php echo "Standard tags (recommended)"; ?>
<?= "Short echo tags" ?>
<? echo "Short tags (deprecated)"; ?>
<script language="php">echo "Script tags (deprecated)";</script>
VBScript:
' This is a single line comment
REM This is also a comment
' Multi-line comments require multiple single quotes
' Line 1 of comment
' Line 2 of comment
PHP:
// This is a single line comment
# This is also a single line comment
/*
This is a multi-line comment
Line 1 of comment
Line 2 of comment
*/
VBScript:
' No semicolons needed
Dim name
name = "John"
Response.Write(name)
PHP:
// Semicolons are required to end statements
$name = "John";
echo $name;
VBScript:
' Variables declared with Dim
Dim userName
Dim userAge
userName = "John"
userAge = 30
PHP:
// Variables start with $ and don't need declaration
$userName = "John";
$userAge = 30;
VBScript:
Dim firstName, lastName, fullName
firstName = "John"
lastName = "Doe"
fullName = firstName & " " & lastName
PHP:
$firstName = "John";
$lastName = "Doe";
$fullName = $firstName . " " . $lastName; // Using . operator
// Or using double quotes (variable interpolation)
$fullName = "$firstName $lastName";
PHP has multiple data types like Python, but it's more flexible about type conversion, making it somewhat similar to VBScript's Variant behavior.
VBScript:
' Variables must be declared with Dim
Dim myNumber
Dim myString
Dim myDate
' Then assigned values
myNumber = 42
myString = "Hello"
myDate = #01/01/2024#
PHP:
// No declaration needed - variables are created when assigned
// All variables start with $
$myNumber = 42;
$myString = "Hello";
$myDate = "2024-01-01"; // PHP handles dates differently
VBScript Variant Content | PHP Equivalent | Example |
---|---|---|
Integer | int |
$age = 25; |
Decimal | float |
$price = 19.99; |
String | string |
$name = "John"; |
Boolean | bool |
$isActive = true; |
Date | string or DateTime |
$date = "2024-01-01"; |
Array | array |
$numbers = [1, 2, 3]; |
Nothing/Null | null |
$result = null; |
VBScript:
Dim num1, num2, result
num1 = 10
num2 = 3
result = num1 + num2 ' Addition: 13
result = num1 - num2 ' Subtraction: 7
result = num1 * num2 ' Multiplication: 30
result = num1 / num2 ' Division: 3.333...
result = num1 \ num2 ' Integer division: 3
result = num1 Mod num2 ' Modulus: 1
result = num1 ^ num2 ' Exponentiation: 1000
PHP:
$num1 = 10;
$num2 = 3;
$result = $num1 + $num2; // Addition: 13
$result = $num1 - $num2; // Subtraction: 7
$result = $num1 * $num2; // Multiplication: 30
$result = $num1 / $num2; // Division: 3.333...
$result = intval($num1 / $num2); // Integer division: 3
$result = $num1 % $num2; // Modulus: 1
$result = $num1 ** $num2; // Exponentiation: 1000 (PHP 5.6+)
$result = pow($num1, $num2); // Exponentiation (older PHP)
VBScript:
Dim firstName, lastName, fullName
firstName = "John"
lastName = "Doe"
' String concatenation with &
fullName = firstName & " " & lastName
' String functions
Dim upperName, lowerName, nameLength
upperName = UCase(fullName) ' "JOHN DOE"
lowerName = LCase(fullName) ' "john doe"
nameLength = Len(fullName) ' 8
' Substring
Dim firstThree
firstThree = Left(fullName, 3) ' "Joh"
PHP:
$firstName = "John";
$lastName = "Doe";
// String concatenation with .
$fullName = $firstName . " " . $lastName;
// Or using double quotes (variable interpolation)
$fullName = "$firstName $lastName";
// String functions
$upperName = strtoupper($fullName); // "JOHN DOE"
$lowerName = strtolower($fullName); // "john doe"
$nameLength = strlen($fullName); // 8
// Substring
$firstThree = substr($fullName, 0, 3); // "Joh"
$lastThree = substr($fullName, -3); // "Doe"
PHP control structures are very similar to VBScript in logic but use different syntax, particularly with curly braces instead of End statements.
VBScript:
Dim score
score = 85
If score >= 90 Then
Response.Write("Grade: A")
ElseIf score >= 80 Then
Response.Write("Grade: B")
ElseIf score >= 70 Then
Response.Write("Grade: C")
ElseIf score >= 60 Then
Response.Write("Grade: D")
Else
Response.Write("Grade: F")
End If
PHP:
$score = 85;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 80) {
echo "Grade: B";
} elseif ($score >= 70) {
echo "Grade: C";
} elseif ($score >= 60) {
echo "Grade: D";
} else {
echo "Grade: F";
}
VBScript | PHP | Description |
---|---|---|
= |
== |
Equal to (loose comparison) |
= |
=== |
Identical to (strict comparison) |
<> |
!= or <> |
Not equal to (loose) |
<> |
!== |
Not identical to (strict) |
< |
< |
Less than |
> |
> |
Greater than |
<= |
<= |
Less than or equal |
>= |
>= |
Greater than or equal |
VBScript:
' Simple For loop
Dim i
For i = 1 To 10
Response.Write(i & "<br>")
Next
' For Each loop with array
Dim fruits(2)
fruits(0) = "Apple"
fruits(1) = "Banana"
fruits(2) = "Orange"
Dim fruit
For Each fruit In fruits
Response.Write(fruit & "<br>")
Next
PHP:
// Simple for loop
for ($i = 1; $i <= 10; $i++) {
echo $i . "<br>";
}
// Foreach loop with array
$fruits = ["Apple", "Banana", "Orange"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
// Foreach with index
foreach ($fruits as $index => $fruit) {
echo "$index: $fruit<br>";
}
This is where PHP really shines for VBScript/Classic ASP developers. Many concepts are directly comparable.
Classic ASP (VBScript):
<%
Dim userName, email
userName = Request.Form("username")
email = Request.Form("email")
If userName <> "" And email <> "" Then
Response.Write("Hello " & userName & "!")
Response.Write("Your email: " & email)
End If
%>
PHP:
<?php
$userName = $_POST['username'] ?? '';
$email = $_POST['email'] ?? '';
if (!empty($userName) && !empty($email)) {
echo "Hello $userName!";
echo "Your email: $email";
}
?>
Classic ASP:
<%
Dim page, category
page = Request.QueryString("page")
category = Request.QueryString("category")
If page = "" Then page = "1"
Response.Write("Page: " & page & ", Category: " & category)
%>
PHP:
<?php
$page = $_GET['page'] ?? '1';
$category = $_GET['category'] ?? '';
echo "Page: $page, Category: $category";
?>
Classic ASP:
<%
' Start session (automatic in ASP)
Session("user_id") = 123
Session("username") = "john_doe"
' Read session
Dim userId, userName
userId = Session("user_id")
userName = Session("username")
' Clear session
Session.Abandon
%>
PHP:
<?php
// Start session (required in PHP)
session_start();
$_SESSION['user_id'] = 123;
$_SESSION['username'] = 'john_doe';
// Read session
$userId = $_SESSION['user_id'] ?? null;
$userName = $_SESSION['username'] ?? '';
// Clear session
session_destroy();
?>
Classic ASP:
<%
' Set cookie
Response.Cookies("theme") = "dark"
Response.Cookies("theme").Expires = DateAdd("d", 30, Now())
' Read cookie
Dim theme
theme = Request.Cookies("theme")
If theme = "" Then theme = "light"
%>
PHP:
<?php
// Set cookie
setcookie("theme", "dark", time() + (30 * 24 * 60 * 60)); // 30 days
// Read cookie
$theme = $_COOKIE['theme'] ?? 'light';
?>
Now that you understand the basic syntax differences between VBScript and PHP, here are your next steps:
Remember, your VBScript and Classic ASP knowledge gives you a solid foundation for understanding web development concepts. PHP just provides a more modern and flexible way to implement these concepts!