kitchen cabinets forum

Members Login
    Remember Me  
Post Info TOPIC: Mastering PHP: Sample Assignments and Solutions for Programming Enthusiasts

Veteran Member

Status: Offline
Posts: 27
Mastering PHP: Sample Assignments and Solutions for Programming Enthusiasts

Are you struggling with your PHP assignments and looking for someone to handle them for you? Look no further! At, we specialize in providing expert assistance with PHP assignments. Our team is here to ensure that your assignments are completed accurately and on time, allowing you to focus on other priorities. Whether you're grappling with complex concepts or facing tight deadlines, we've got you covered. So, if you're thinking, 'Who can do my PHP assignment?' - rest assured, we are here to help. Let us take the stress out of your PHP assignments so you can achieve academic success with ease.

programming-assignment-help (5).png

Question 1: Manipulating Arrays in PHP

Your task is to write a PHP function that takes an array of integers as input and returns the sum of all positive integers in the array. If the array is empty or contains no positive integers, the function should return 0.


function sumPositiveIntegers($arr) {
    $sum = 0;
    foreach ($arr as $num) {
        if ($num > 0) {
            $sum += $num;
    return $sum;

// Example usage:
$array1 = [2, -5, 10, -8, 3];
echo "Sum of positive integers: " . sumPositiveIntegers($array1); // Output: 15


- We define a function `sumPositiveIntegers` that takes an array `$arr` as input.
- We initialize a variable `$sum` to store the sum of positive integers.
- Using a `foreach` loop, we iterate through each element of the array.
- Within the loop, we check if the current element `$num` is greater than 0. If it is, we add it to the sum.
- Finally, we return the computed sum.

Question 2: Handling File Uploads in PHP

You're tasked with creating a PHP script that allows users to upload images (only JPEG and PNG formats) to a server and saves them in a specified directory. The script should perform the following tasks:
1. Check if the uploaded file is a valid JPEG or PNG image.
2. If valid, move the file to the designated directory.
3. Display a success message upon successful upload.
4. Handle errors gracefully and provide appropriate error messages.


$targetDir = "uploads/";
$uploadFile = $targetDir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($uploadFile, PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if (isset($_POST["submit"])) {
    $check = getimagesize($_FILES["file"]["tmp_name"]);
    if ($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;

// Check file size
if ($_FILES["file"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;

// Allow only certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png") {
    echo "Sorry, only JPG, JPEG, PNG files are allowed.";
    $uploadOk = 0;

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $uploadFile)) {
        echo "The file " . basename($_FILES["file"]["name"]) . " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";


- We start by defining the target directory for uploads and the uploaded file's path.
- We check if the form has been submitted (`isset($_POST["submit"])`).
- Using `getimagesize()`, we verify if the uploaded file is indeed an image.
- We check the file size and only allow files smaller than 500KB.
- We restrict uploads to JPEG and PNG formats only.
- If all checks pass, we move the uploaded file to the specified directory using `move_uploaded_file()`.



Mastering PHP isn't just about solving assignments; it's about understanding the language's nuances and applying them creatively to solve real-world problems. With the solutions provided above, you're now equipped to tackle complex PHP tasks with confidence. Remember, practice makes perfect, so keep coding and exploring the vast possibilities of PHP programming. Stay tuned for more expert insights and sample assignments to fuel your programming journey!

Page 1 of 1  sorted by
Quick Reply

Please log in to post quick replies.

Create your own FREE Forum
Report Abuse
Powered by ActiveBoard