Programming Languages PHP Subjective
Nov 23, 2012

In PHP, what is PEAR?

Detailed Explanation

PEAR (PHP Extension and Application Repository) is a framework and distribution system for reusable PHP components.

Installation and Usage:

// Install PEAR package
pear install Mail
pear install DB

// Using PEAR Mail package
require_once "Mail.php";

$recipients = "user@example.com";
$headers["From"] = "sender@example.com";
$headers["Subject"] = "Test Email";
$body = "This is a test email";

$mail = Mail::factory("smtp", [
    "host" => "smtp.gmail.com",
    "port" => 587,
    "auth" => true,
    "username" => "your_email@gmail.com",
    "password" => "your_password"
]);

$result = $mail->send($recipients, $headers, $body);

PEAR Database Example:

require_once "DB.php";

$dsn = "mysql://username:password@localhost/database";
$db = DB::connect($dsn);

if (PEAR::isError($db)) {
    die("Connection failed: " . $db->getMessage());
}

// Execute query
$result = $db->query("SELECT * FROM users WHERE active = 1");
while ($row = $result->fetchRow()) {
    echo $row[0] . " - " . $row[1] . "\n";
}

Modern Alternative: Composer has largely replaced PEAR for dependency management in modern PHP development.

Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback