You are on page 1of 5

GOVERNMENT POLYTECHNIC MURTIZAPUR

DIPLOMA IN INFORMATION TECHNOLOGY


Subject: Web based application with PHP(22619)

Sem: IF6I
Assignment No. 01 ANSWERS

1. State any four advantages of PHP.


Advantages of PHP :
• The most important advantage of PHP is that it’s open-source and free
from cost. It can be downloaded anywhere and is readily available to use
for events or web applications.
• It is platform-independent. PHP-based applications can run on any OS
like UNIX, Linux, Windows, etc.
• Applications can easily be loaded which are based on PHP and connected
to the database. It’s mainly used due to its faster rate of loading over
slow internet speed than other programming language.
• It has less learning curve because it is simple and straightforward to use.
Someone familiar with C programming can easily work on PHP.

2. Write PHP program to print whether given number is odd or


even.
<?php
// PHP code to check whether the number
// is Even or Odd in Normal way
function check($number){
if($number % 2 == 0){
echo "Even";
}
else{
echo "Odd";
}
}

// Driver Code
$number = 39;
check($number)
?>

3. Explain if and switch statement.


An if-else statement can evaluate almost all the types of data such as
integer, floating- point, character, pointer, or Boolean. A switch
statement can evaluate either an integer or a character. In the case of
'if-else' statement, either the 'if' block or the 'else' block will be
executed based on the condition.

4. Write a program to find the largest number among three


numbers.
<?php

// Input the three numbers


// and store it in variable

$number1 = 12;
$number2 = 7;
$number3 = 15;

// Using the max function to find the largest number


$maxNumber = max($number1, $number2, $number3);
echo "The largest number among three is: $maxNumber\n";

?>

5. Explain variable and anonymous function with suitable example.


6. Variable:
7. In PHP, variables are declared using the $ symbol, followed by the variable name.
The type of data a variable holds is determined dynamically based on the assigned value.

<?php
// Variable example
$age = 25;
$name = "John";

echo $name . " is " . $age . " years old.";


?>

Anonymous Function:

Anonymous functions, or closures, in PHP are created using the function keyword without giving
them a name. They are often used for small, one-time-use operations.

<?php
// Anonymous function example
$multiply = function ($x, $y) {
return $x * $y;
};

$result = $multiply(3, 4);


echo $result; // Output: 12
?>

8. Explain extract, implode and flip function in arrays.

1. extract function:

The extract function in PHP is used to import variables into the local symbol table from
an associative array. It takes an associative array as an argument, and it creates variables
for each key-value pair in the array.

2. implode function:
The implode function is used to join array elements with a string. It takes an array as its
first argument and optionally a string as its second argument. It returns a string
containing all the array elements joined by the specified string.

3. flip function:

The flip function is used to exchange the keys with their associated values in an array. It returns a
new array where the values become keys, and the keys become values.

<?php
// Example data
$userData = array(
"name" => "John",
"age" => 25,
"city" => "New York",
"email" => "john@example.com"
);

// Using implode to concatenate user details into a string


$userDetailsString = implode(", ", $userData);
echo "User Details: " . $userDetailsString . "\n";

// Using extract to create variables from the array


extract($userData);
echo "Name: $name, Age: $age, City: $city, Email: $email\n";

// Using array_flip to swap keys and values in the array


$flippedUserData = array_flip($userData);
echo "Flipped User Data: ";
print_r($flippedUserData);
?>

9. Write a program to illustrate scaling of image.


<?php
$originalImagePath = 'image.jpg';
$newWidth = 500;
$newHeight = 300;
$newImage = imagecreatetruecolor($newWidth, $newHeight);
$originalImage = imagecreatefromjpeg($originalImagePath);
imagecopyresized($newImage, $originalImage, 0, 0, 0, 0, $newWidth,
$newHeight, imagesx($originalImage), imagesy($originalImage));
imagejpeg($newImage, 'scaled_image.jpg');
imagedestroy($newImage);
imagedestroy($originalImage);

?>

10. Write a php program to do string manipulations.


<?php
$inputString = "Hello, PHP!";
$uppercase = strtoupper($inputString);
echo "Uppercase: $uppercase\n";
$length = strlen($inputString);
echo "String Length: $length\n";
$substring = substr($inputString, 0, 5);
echo "Substring: $substring\n";
$replacement = str_replace("PHP", "World", $inputString);
echo "Replace: $replacement\n";

$reversedString = strrev($inputString);
echo "Reverse: $reversedString\n";
?>

11. What is function? Also Define formal and actual parameter in


PHP.
a function is a block of reusable code that performs a
specific task or set of tasks. Functions allow you to organize
your code into modular and manageable pieces,
promoting code reusability and readability.

12. Enlist different operators used in PHP.


Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
13. What is function? Also Define formal and actual parameter in
PHP.

You might also like