PHP Training Guide

Learn PHP using your VBScript knowledge

Table of Contents

VBScript to PHP Training Guide

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.

Why Learn PHP?

Coming from VBScript/Classic ASP, you'll find PHP offers several advantages:

  • Cross-platform compatibility: PHP runs on Windows, Linux, Mac, and many other systems
  • Large community: Extensive documentation, frameworks, and community support
  • Modern frameworks: Laravel, Symfony, CodeIgniter for rapid development
  • Better performance: Generally faster than Classic ASP
  • Rich ecosystem: Thousands of libraries and packages available via Composer
  • Database flexibility: Works with MySQL, PostgreSQL, SQLite, and many others

Key Similarities with Classic ASP

PHP shares several concepts with Classic ASP that will make your transition easier:

  1. Server-side execution: Both run on the server before sending HTML to the browser
  2. Embedded in HTML: Both can be mixed with HTML using special tags
  3. Form handling: Both can process GET and POST requests
  4. Session management: Both support user sessions and cookies
  5. Database connectivity: Both can connect to databases for dynamic content

Key Differences to Keep in Mind

Before we dive into the comparisons, here are some fundamental differences:

  1. Case sensitivity: PHP is case-sensitive for variables and user-defined functions
  2. Variable prefix: PHP variables must start with $ (dollar sign)
  3. Array indexing: PHP arrays are 0-based by default
  4. Error handling: PHP has more structured error handling options
  5. Object-oriented: PHP has full object-oriented programming support
  6. Syntax: PHP syntax is closer to C/Java than VBScript

Basic Syntax Comparison

Let's start with the most fundamental differences in syntax between VBScript and PHP.

Hello World and Basic Output

VBScript (Classic ASP):

<%
Response.Write("Hello World!")
%>

PHP:

<?php
echo "Hello World!";
?>

PHP Tags

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>

Comments

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
*/

Statements and Semicolons

VBScript:

' No semicolons needed
Dim name
name = "John"
Response.Write(name)

PHP:

// Semicolons are required to end statements
$name = "John";
echo $name;

Variables

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;

String Concatenation

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";

Variables and Data Types

PHP has multiple data types like Python, but it's more flexible about type conversion, making it somewhat similar to VBScript's Variant behavior.

Variable Declaration and Assignment

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

Data Types Comparison

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;

Working with Numbers

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)

Working with Strings

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"

Control Structures

PHP control structures are very similar to VBScript in logic but use different syntax, particularly with curly braces instead of End statements.

If 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";
}

Comparison Operators

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

For Loops

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>";
}

Web Development Features

This is where PHP really shines for VBScript/Classic ASP developers. Many concepts are directly comparable.

Form Handling

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";
}
?>

Query String Parameters

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";
?>

Session Management

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();
?>

Cookies

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';
?>

Next Steps

Now that you understand the basic syntax differences between VBScript and PHP, here are your next steps:

  1. Practice the basics: Work through the PHP practice exercises
  2. Learn modern PHP frameworks: Start with Laravel or Symfony
  3. Understand templating: Learn Twig or Blade templating engines
  4. Database integration: Learn PDO for database operations
  5. Composer and packages: Learn PHP's package manager
  6. Build projects: Create real applications to solidify your knowledge

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!