Core PHP Intermediate Capabilities
- Robust exception and error management
- Handling file transmissions (uploads/downloads)
- Dynamic media manipulation (e.g., GD library)
- Persistent data storage via PDO or MySQLi
- Debugging strategies and unit testing
- Object-Oriented paradigms (OOP)
- Namespace organization
- Dependency management with Composer and PSR standards
- Non-blocking execution (ReactPHP, Swoole)
- Security hardening (XSS mitigation, cryptographic hashing)
Below are practical implementations demonstrating file handling, image manipulation, database operations, and HTTP requests.
// Handling File Transmission
if (isset($_FILES['document']) && $_FILES['document']['error'] === UPLOAD_ERR_OK) {
$tempPath = $_FILES['document']['tmp_name'];
$originalName = basename($_FILES['document']['name']);
$destination = '/var/www/uploads/' . $originalName;
if (move_uploaded_file($tempPath, $destination)) {
// File successfully moved
}
}
// Image Manipulation using GD
$sourceImage = imagecreatefromjpeg($destination);
$width = imagesx($sourceImage);
$height = imagesy($sourceImage);
$newImage = imagecreatetruecolor($width, $height);
imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($newImage, '/var/www/processed/resized_' . $originalName, 90);
imagedestroy($sourceImage);
imagedestroy($newImage);
// Database Operation using PDO
try {
$dbConnection = new PDO('mysql:host=127.0.0.1;dbname=app_db', 'db_user', 'secure_pass');
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$insertQuery = "INSERT INTO accounts (handle, contact_email) VALUES (:handle, :email)";
$statement = $dbConnection->prepare($insertQuery);
$statement->execute([':handle' => $handle, ':email' => $email]);
} catch (PDOException $ex) {
// Handle database exception
}
// Executing HTTP GET Request
function fetchRemoteData($apiEndpoint, $queryParams = []) {
if (!empty($queryParams)) {
$apiEndpoint .= '?' . http_build_query($queryParams);
}
$curlHandler = curl_init();
$options = [
CURLOPT_URL => $apiEndpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_TIMEOUT => 30,
];
curl_setopt_array($curlHandler, $options);
$responsePayload = curl_exec($curlHandler);
if ($responsePayload === false) {
$failureReason = curl_error($curlHandler);
curl_close($curlHandler);
throw new RuntimeException("HTTP request failed: " . $failureReason);
}
curl_close($curlHandler);
return $responsePayload;
}