PHP For PERL Developers

As a seasoned computer science teacher, I'm excited to guide you through the world of PHP, especially if you're coming from a PERL background. Don't worry if you're new to programming – we'll start from the basics and work our way up. Let's dive in!

PHP - For PERL Developers

Similarities between PERL and PHP

1. Scripting Languages

Both PERL and PHP are scripting languages, which means they don't need to be compiled before running. This makes them great for quick development and testing.

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

In this simple example, we're using PHP to print "Hello, World!" to the screen. Notice how we don't need to compile this code – we can run it directly in a PHP-enabled web server.

2. Syntax

PERL developers will find PHP syntax familiar. Both languages use semicolons to end statements and curly braces for code blocks.

<?php
if ($weather == "sunny") {
    echo "Let's go for a walk!";
} else {
    echo "Maybe we should stay inside.";
}
?>

This code checks the weather and suggests an activity. The structure is very similar to what you'd see in PERL.

3. Variables

Both languages use a $ sign to denote variables. This makes the transition from PERL to PHP smoother.

<?php
$name = "Alice";
$age = 30;
echo "My name is $name and I am $age years old.";
?>

Here, we're declaring and using variables in PHP. It's almost identical to how you'd do it in PERL!

4. Regular Expressions

Both PERL and PHP have robust support for regular expressions, though the syntax differs slightly.

<?php
$string = "The quick brown fox jumps over the lazy dog";
if (preg_match("/fox/", $string)) {
    echo "Found a fox!";
}
?>

This code searches for the word "fox" in our string using a regular expression. While the function name is different from PERL, the concept is the same.

Differences between PERL and PHP

1. Web Focus

While PERL is a general-purpose language, PHP is designed primarily for web development.

<?php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
echo "You are using: $user_agent";
?>

This PHP code accesses the user's browser information, something that's built into PHP but would require additional modules in PERL.

2. Array Syntax

PHP uses a different syntax for arrays compared to PERL.

<?php
$fruits = array("apple", "banana", "cherry");
echo $fruits[1];  // Outputs: banana
?>

In PHP, we use square brackets to access array elements, and array indices start at 0, unlike PERL's 1-based indexing.

3. Function Definitions

PHP requires the 'function' keyword when defining functions, unlike PERL.

<?php
function greet($name) {
    return "Hello, $name!";
}

echo greet("Bob");  // Outputs: Hello, Bob!
?>

This example shows how to define and use a function in PHP. The 'function' keyword is necessary, which isn't the case in PERL.

4. Object-Oriented Programming

While both languages support OOP, PHP's implementation is more Java-like and arguably more intuitive for many developers.

<?php
class Dog {
    public function bark() {
        echo "Woof!";
    }
}

$myDog = new Dog();
$myDog->bark();  // Outputs: Woof!
?>

This code defines a simple Dog class with a bark method. The syntax for creating and using objects is quite different from PERL's OOP approach.

5. Built-in Web Functionality

PHP has many built-in functions specifically for web development that PERL lacks.

<?php
session_start();
$_SESSION['user'] = 'Alice';
echo "Welcome back, " . $_SESSION['user'];
?>

This code demonstrates PHP's built-in session handling, which is crucial for web applications but would require additional modules in PERL.

Method Comparison Table

Functionality PERL PHP
Print to screen print "Hello"; echo "Hello";
Define variable $name = "Alice"; $name = "Alice";
Array declaration @fruits = ("apple", "banana"); $fruits = array("apple", "banana");
Function definition sub greet { ... } function greet() { ... }
Regular expression if ($string =~ /pattern/) { ... } if (preg_match("/pattern/", $string)) { ... }
File reading open(my $fh, "<", "file.txt"); $fh = fopen("file.txt", "r");

Remember, while there are differences, the core programming concepts remain the same. As you practice more with PHP, you'll find that your PERL knowledge gives you a solid foundation to build upon. Happy coding!

Credits: Image by storyset