0% found this document useful (0 votes)
9 views6 pages

Assignment 2 Full Answers

The document contains a series of questions and answers related to PHP programming, covering topics such as array functions, image manipulation, string functions, and variable functions. It includes explanations, syntax, and example code for various PHP functions and concepts. Additionally, it provides sample programs for creating images and generating PDF documents in PHP.

Uploaded by

thopteayush604
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Assignment 2 Full Answers

The document contains a series of questions and answers related to PHP programming, covering topics such as array functions, image manipulation, string functions, and variable functions. It includes explanations, syntax, and example code for various PHP functions and concepts. Additionally, it provides sample programs for creating images and generating PDF documents in PHP.

Uploaded by

thopteayush604
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 2 - Questions and Answers

Q.1) Attempt any THREE of the following.

A. Write the use of array_flip()

The array_flip() function in PHP exchanges the keys with their associated values in an array.

Syntax: array_flip(array $array): array

B. Define ImageColorAllocate() function along with its syntax.

The ImageColorAllocate() function is used to allocate a color to an image in PHP.

Syntax: imagecolorallocate(resource $image, int $red, int $green, int $blue): int

C. Differentiate between implode and explode functions.

explode(): Splits a string into an array based on a delimiter.

implode(): Joins an array into a string using a separator.

D. Differentiate between include and require functions.

include: Includes a file but does not halt execution if the file is missing.

require: Includes a file and halts execution if the file is missing.

E. Write syntax of foreach loop.

Syntax:

foreach ($array as $key => $value) {

// Code to execute

F. List the parameter of function ImageCopyResampled().

The function takes the following parameters:

- Destination image resource

- Source image resource


- Destination X and Y coordinates

- Source X and Y coordinates

- Destination width and height

- Source width and height

G. Define Array. State its example.

An array in PHP is a data structure that stores multiple values in a single variable.

Example: $arr = array('apple', 'banana', 'cherry');

H. Explain variable function with example.

A variable function allows calling a function using a variable containing its name.

Example:

$func = 'strtolower';

echo $func('HELLO'); // Outputs: hello

I. List the array sorting function with its meaning.

Some common array sorting functions in PHP:

- sort(): Sorts an array in ascending order

- rsort(): Sorts an array in descending order

- asort(): Sorts an associative array in ascending order by value

- ksort(): Sorts an associative array in ascending order by key

Q.2) Attempt any THREE of the following.

A. Explain Indexed and Associative arrays with suitable example.

Indexed arrays use numeric keys, whereas associative arrays use named keys.

Example of Indexed array:

$fruits = array('Apple', 'Banana', 'Cherry');

Example of Associative array:


$person = array('Name' => 'John', 'Age' => 30);

B. Explain the following string functions with example.

- str_replace(): Replaces all occurrences of a string.

Example: str_replace('world', 'PHP', 'Hello world!'); // Outputs: Hello PHP!

- ucwords(): Capitalizes the first letter of each word.

Example: ucwords('hello world'); // Outputs: Hello World

- strlen(): Returns the length of a string.

Example: strlen('Hello'); // Outputs: 5

- strtoupper(): Converts a string to uppercase.

Example: strtoupper('hello'); // Outputs: HELLO

C. Write a PHP program to create a filled rectangle.

PHP Code:

<?php

$img = imagecreate(200, 100);

$bg = imagecolorallocate($img, 255, 255, 255);

$color = imagecolorallocate($img, 0, 0, 255);

imagefilledrectangle($img, 50, 20, 150, 80, $color);

header('Content-Type: image/png');

imagepng($img);

imagedestroy($img);

?>

D. Develop a program to print Fibonacci series using function.

PHP Code:

<?php

function fibonacci($n) {
$num1 = 0; $num2 = 1;

for ($i = 0; $i < $n; $i++) {

echo $num1 . ' ';

$temp = $num1 + $num2;

$num1 = $num2;

$num2 = $temp;

fibonacci(10);

?>

E. State the variable function. Explain it with example.

A variable function allows calling a function using a variable containing its name.

Example:

$func = 'strlen';

echo $func('Hello'); // Outputs: 5

F. Explain the concept of anonymous function in detail.

Anonymous functions are functions without a name, used for inline execution.

Example:

$sum = function($a, $b) { return $a + $b; };

echo $sum(3, 4); // Outputs: 7

G. Develop a program to create an image with text in PHP.

PHP Code:

<?php

$img = imagecreate(200, 100);

$bg = imagecolorallocate($img, 255, 255, 255);


$text_color = imagecolorallocate($img, 0, 0, 0);

imagestring($img, 5, 50, 40, 'Hello', $text_color);

header('Content-Type: image/png');

imagepng($img);

imagedestroy($img);

?>

H. Write a PHP program to create a triangle using imagepolygon() function.

PHP Code:

<?php

$img = imagecreate(200, 200);

$bg = imagecolorallocate($img, 255, 255, 255);

$color = imagecolorallocate($img, 0, 0, 255);

$points = array(100, 50, 150, 150, 50, 150);

imagepolygon($img, $points, 3, $color);

header('Content-Type: image/png');

imagepng($img);

imagedestroy($img);

?>

I. Write a program to create a PDF document using file content in PHP.

PHP Code:

<?php

require('fpdf.php');

$pdf = new FPDF();

$pdf->AddPage();

$pdf->SetFont('Arial', 'B', 16);

$pdf->Cell(40, 10, 'Hello World!');


$pdf->Output();

?>

You might also like