You are on page 1of 10

2 marks:

1. Describe following function w.r.t String in PHP


I. substr () – substr () function is used to display or extract a string from a particular
position
II. strcmp () - Compare two strings (case-sensitive).
III. If this function returns 0, the two strings are equal.
IV. If this function returns any negative or positive numbers, the two strings are not
equal.
V. strrve () - Reverses a string
VI. ucwords ()- Convert the first character of each word to uppercase
VII. strtoupper () - Converts a string to uppercase letters
VIII. strpos () - Returns the position of the first occurrence of a string inside another
string (case sensitive)
IX. str_word_count ()- Count the number of words in a string

2. Explain array implode and explode function in PHP with example.


A. Implode
- Implode () function is a built-in function to join all the elements in an array. Like join()
function it returns the string formed from the elements of an array
- Syntax: string implode(separator, array);
- Where separators are parameter provided here to separate the values and add whose
value is to be joined to a string.
Example:
<?php 
    // PHP Code to implement join function 
        $InputArray = array('CO','IF','EJ'); 

    // Join without separator 


    print_r(implode($InputArray)); 
    print_r("<BR>"); 

    // Join with separator 


    print_r(implode("-",$InputArray)); 
?>
Output:
COIFEJ
CO-IF-EJ
B.  Explode ()
- The explode () is a built-in function in PHP used to split the string wherever
the delimeter character occurs. This function returns an array containing the strings
formed by splitting the original string.
- Syntax: array explode (separator, OriginalString, NoOfElements);
- Separator: compulsory parameter which specifies the point at which string will split
- Original String: compulsory parameter. Input string which is to be split in array
- No of elements: optional parameter.
- Example:
<?php
$str = "PHP: Welcome to the world of PHP";
print_r (explode (" ",$str));
?>
Output:
Array ( [0] => PHP: [1] => Welcome [2] => to [3] => the [4] => world [5] => of [6] =>
PHP )

3. Describe Math function with example.


- PHP provides many predefined functions that can be used to perform mathematical
operations.
I. abs (): Returns absolute value of given number.
Example: Abs (-7) = 7
II. ceil (): Rounds fractions up.
Example: Ceil (3.3) =4
III. floor (): Rounds fractions down.
Example: Floor (3.3) =3
IV. sqrt (): Returns square root of given argument.
Example: Sqrt (16) =4
V. Pi (): Returns the value of pi.
Example: 3.14159265

4. Write short note on image functions.


The Web is more than just text. Images appear in the form of logos, buttons,
photographs, charts, advertisements, and icons PHP supports graphics creation with
the GD and Imlib2 extensions. Image creating functions for various formats:
i. imagecreate (): To create an image in memory to work with
ii. imagegif(): Output a GIF image to browser or file.
iii. imagejpeg(): Output a JPEG image to browser or file.
iv. imagewbmp(): Output a WBMP image to browser or file.
v. imagepng(): Output a PNG image to browser or file.
vi. imagestring():inbuilt function in PHP which is used to draw the string
horizontally.
vii. ImageCopyResampled(): function to resize pictures.
viii. After sending the image, you can destroy the image object with the
imagedestroy() function

5. Write short note on types of arrays with example.

6. State the variable function. Explain it with example


- PHP supports the concept of variable functions.
- This means that if a variable name has parentheses appended to it, PHP will look for a
function with the same name as whatever the variable evaluates to and will attempt to
execute it.
- Among other things, this can be used to implement call-backs, function tables and so
forth.
- Variable functions won't work with language constructs such as echo, print, unset(),
isset(), empty(), include, require and the like.
- We utilize wrapper functions to make use of any of these constructs as variable
functions. Example:

<?php
function simple ()
{
echo "In simple ()<br />\n";
}
function data ($arg = '')
{
echo "In data (); argument was '$arg'.<br />\n";
}
$func = 'simple';
$func(); // This calls simple()
$func = 'data';
$func('test'); // This calls data ()
?>
OUTPUT:
In simple ()
In data (); argument was 'test'.

7. State the use of str word_count along with its syntax.


The str_word_count() function is a string function used to count the number of words in
a specified string.
Syntax: str_word_count(string)
8. Write the use of array_combine() and array_merge().
- array_combine(): The array_combine() function creates an array by using the
elements from one "keys" array and one "values" array.
- Syntax
array_combine(keys, values)
array_merge()
- The array_merge() function merges two arrays into one array.
- Syntax
array_merge(array1, array2, array3, ...)

9. Write the use of array_flip.


- This built-in function of PHP array_flip() is used to exchange elements within an
array, i.e., exchange all keys with their associated values in an array and vice-versa.
- Syntax: array_flip(array);

10. Define Imagecolorallocate() function along with its syntax


- To set colors to be used in the image, you use the imagecolorallocate( ) function.
- Syntax: imagecolorallocate(image, red, green, blue);

4 marks:
1. Difference between array_pop () and array_push () (with example)
array_pop() array_push()
The array_pop() method is used to pop the The array_push() method is used to push
elements from the array. multiple elements in the array
simultaneously
.
The elements are removed from the end Elements are inserted in the order in
one by one each time when this method is which they are specified in the method call
called.
Syntax: Syntax:
array_pop($array) array_push($array, $ele1, $ele2, ...)
It takes array as an argument It takes multiple array elements as
argument
Example: Example:
<?php $arr = array();
array_push($arr, 1, 2, 3, 4, 5); array_push($arr, 1, 2, 3, 4, 5);
print_r("Array after multiple insertions print_r("Array after multiple insertions
</br>"); </br>");
print_r($arr); print_r($arr);
array_pop($arr);
print_r("Array after a single pop </br>");
print_r($arr);

2. Explain sorting mechanism w.r.t. array with example.


- Sorting refers to ordering data in an alphabetical, numerical order and increasing or
decreasing fashion according to some linear relationship among the data items depends
on the value or key.
- Sorting greatly improves the efficiency of searching.
- Following are the Sorting Functions for Arrays in PHP:
1. sort (): sorts arrays in ascending order
2. rsort(): sorts arrays in descending order
3. asort(): sorts associative arrays in ascending order, according to the value
4. ksort(): sorts associative arrays in ascending order, according to the key
5. arsort(): sorts associative arrays in descending order, according to the value
6. krsort(): sorts associative arrays in descending order, according to the key
Example:

<?php
$num = array (40, 61, 2, 22, 13);
echo "Before Sorting:<br>";
foreach ($num as $key=>$value) {
echo $value. "<br>";
}
sort($num);
echo "After Sorting in Ascending order:<br>";
foreach ($num as $key=>$value) {
echo $value. "<br>";
}
echo "After Sorting in Descending order:<br>";
rsort($num);
foreach ($num as $key=>$value) {
echo $value. "<br>";
}
?>
OUTPUT:
Before Sorting: 40 61 2 22 13
After Sorting in Ascending order: 2 13 22 40 61
After Sorting in Descending order: 61 40 22 13 2

3. Explain Indexed and Associative arrays with suitable example.


A. Indexed Array
- An array with a numeric index where values are stored linearly. Numeric arrays use
number as access keys. An access key is a reference to a memory slot in an array
variable. The access key is used whenever we want to read or assign a new value an
array element
- Example:
<?php
$name [0]="Anurag";
$name [1]="Karan";
$name [2]="Justin";
echo "Size: $name [0], $name[1] and $name[2]";
?>
B. Associative Array
- This type of arrays is like the indexed arrays but instead of linear storage, every
value can be assigned with a user-defined key of string type. An array with a string
index where instead of linear storage, each value can be assigned a specific key.
Associative array differs from numeric array in the sense that associative arrays use
descriptive names for id keys.
Example
<?php
$salary=array("Justin"=>"99999999","Anurag"=>"250000","Karan"=>"50");
echo "Justin salary: ".$salary["Justin"]."<br/>";
echo "Anurag salary: ".$salary["Anurag"]."<br/>";
echo "Karan salary: ".$salary["Karan"]."<br/>";
?>
OUTPUT:
Justin salary: 99999999
Anurag salary: 250000
Karan salary: 50

4. Write PHP script demonstrate variable function.


<?php
function simple ()
{
echo "In simple ()<br />\n";
}
function data ($arg = '')
{
echo "In data (); argument was '$arg'.<br />\n";
}
$func = 'simple';
$func(); // This calls simple()
$func = 'data';
$func('test'); // This calls data ()
?>
Output:
In simple ()
In data (); argument was 'test'.
5. Explain the concept of anonymous function.
- When we need to create a small, localized throw-away function consisting of a few
lines for a specific purpose, such as a callback. It is unwise to pollute the global
namespace with these kind of single use functions. For events like callback, you can
create anonymous or lambda functions using create_function (). Anonymous
functions allow to create functions which have no specified name.
Example 1:
<?php
$str = "hello world!";
$lambda = create_function('$match', 'return "friend!";');
$str = preg_replace_callback('/world/', $lambda, $str);
echo $str ;
?>
Output:
hello friend!!

6. Write a PHP script to:


a. Transform a string all uppercase letters.
b. Transform a string all lowercase letters.
c. Make a string’s first character uppercase.
d. Make a string’s first character of all the words uppercase.
<?php
$string = "vidyalankar polytechnic";
// Transform a string all uppercase letters.
echo "All Uppercase Letters: " . strtoupper($string);
echo "<br>";
// Transform a string all lowercase letters.
echo "All Lowercase letters: " . strtolower($string);
echo "<br>";
// Make a string’s first character uppercase
echo "Only the first letter in Uppercase: " . ucfirst($string);
echo "<br>";
// Make a string’s first character of all the words uppercase.
echo "Letters of all words in Uppercase: " . ucwords($string);
echo "<br>";
OUTPUT
All Uppercase Letters: VIDYALANKAR POLYTECHNIC All Lowercase letters: vidyalankar
polytechnic Only the first letter in Uppercase: Vidyalankar polytechnic Letters of all
words in Uppercase: Vidyalankar Polytechnic
7. Write a PHP script to sort the following associative array:
array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40") in
a) ascending order sort by value
b) ascending order sort by Key
c) descending order sorting by Value
d) descending order sorting by Key
OUTPUT

8. Write a PHP code to read and display user information as Name, Email, ContactNo using
multi-dimensional array.
<?php
// Defining a multidimensional array
$person = array(
array(
"name" => "Yogita K",
"mob" => "5689741523",
"email" => "yogi_k@gmail.com",
),
array(
"name" => "Manisha P.",
"mob" => "2584369721",
"email" => "manisha_p@gmail.com",
),
array(
"name" => "Vijay Patil",
"mob" => "9875147536",
"email" => "Vijay_p@gmail.com",
)
);
// Accessing elements
echo "manisha P's email-id is: " . $person[1]["email"], "<br>";
echo "Vijay Patil's mobile no: " . $person[2]["mob"];
?>

Output:
Manisha P's email-id is: manisha_p@gmail.com
Vijay Patil's mobile no: 9875147536

6 marks:
1. WAP to create PDF document.
<?php
require('fpdf/fpdf.php');
class PDF extends FPDF
{
// Page header
function Header()
{
// Set font family to Arial bold
$this->SetFont('Arial', 'B', 20);
// Move to the right
$this->Cell(80);
// Header
$this->Cell(50, 10, 'Heading', 1, 0, 'C');
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial', 'I', 8);
// Page number
$this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0,
'C');
}
}
// Instantiation of FPDF class
$pdf = new PDF();
// Define alias for number of pages
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times', '', 14);
for ($i = 1; $i <= 30; $i++)
$pdf->Cell(0, 10, 'line number ' . $i, 0, 1);
$pdf->Output();
?>

You might also like