UNIT II Arrays Function and Graphics
UNIT II Arrays Function and Graphics
Syntax
$var=function ($arg1, $arg2) { return $val; };
● There is no function name between the function keyword and
the opening parenthesis.
● There is a semicolon after the function definition because
anonymous function definitions are expressions
● Function is assigned to a variable, and called later using the
variable’s name.
● When passed to another function that can then call it later, it
is known as a callback.
● Return it from within an outer function so that it can access
the outer function’s variables. This is known as a closure.
<?php
$var = function ($x) {return pow($x,3);};
echo "cube of 3 = " . $var(3);
?>
Operation on string and string function
strlen() Function
It returns the length of the string i.e. the count of all the characters in the string
including whitespace characters.
Syntax
strlen(string or variable name);
<?php
$str = "Hello World!";
// Prints 12 as output
echo strlen($str);
?>
strrev() Function
It returns the reversed string of the given string.
Syntax
strrev(string or variable name);
<?php
$str = "Hello World!";
echo strrev($str);
?>
strtoupper() and strtolower() Function
It returns the string after changing the cases of its letters.
● strtoupper(): It returns the string after converting all the letters to
uppercase.
● strtolower(): It returns the string after converting all the letters to
lowercase.
Syntax
strtoupper(string)
strtolower(string)
<?php
$str = "Hello world";
echo strtoupper($str)."<br>";
echo strtolower($str);
?>
str_word_count() Function
It is used to return information about words used in a
string like the total number of words in the string,
positions of the words in the string, etc.
Syntax
str_word_count ( $string , $returnVal, $chars );
<?php
$mystring = "Hello world";
print_r(str_word_count($mystring));
?>
strpos() Function
This function helps us to find the position of the first occurrence of a string in
another string.
Syntax
strpos(original_str, search_str, start_pos);
Parameters
Out of the three parameters specified in the syntax, two are mandatory and one is
optional. The three parameters are described below:
● original_str: This is a mandatory parameter that refers to the original string
in which we need to search for the occurrence of the required string.
● search_str: This is a mandatory parameter that refers to the string that we
need to search.
● start_pos: This is an optional parameter that refers to the position of the
string from where the search must begin.
str_replace() Function
It is used to replace all the occurrences of the search string or array of search strings by
replacement string or array of replacement strings in the given string or array respectively.
Syntax
str_replace ( $searchVal, $replaceVal, $subjectVal, $count );
Parameters
This function accepts 4 parameters out of which 3 are mandatory and 1 is optional. All of these
parameters are described below:
● $searchVal: This parameter can be of both string and array types. This parameter
specifies the string to be searched and replaced.
● $replaceVal: This parameter can be of both string and array types. This parameter
specifies the string with which we want to replace the $searchVal string.
● $subjectVal: This parameter can be of both string and array types. This parameter
specifies the string or array of strings that we want to search for $searchVal and replace
with $replaceVal.
● $count: This parameter is optional and if passed, its value will be set to the total number
of replacement operations performed on the string $subjectVal.
<?php
// Input string
$subjectVal =
"Computer Science ";
// Using str_replace() function
$resStr =
str_replace('Science', 'algorithms', $subjectVal);
print_r($resStr);
?>
ucwords() function in PHP
This function takes a string as argument and returns the string with the first
character of every word in Upper Case and all other characters remains
unchanged.
Syntax:
ucwords($string)
<?php
// Original string
$str = "wbp eti ede mgt pwp";
// String after converting first character
// of every word to uppercase
$resStr = ucwords($str);
print_r($resStr);
?>
Basic Graphics Concepts
An image is a rectangle of pixels of various colors. Colors are
identified by their position in the palette, an array of colors. Each
entry in the palette has three separate color values—one for red,
one for green, and one for blue. Each value ranges from 0 (this
color not present) to 255 (this color at full intensity).
Image files are rarely a straightforward dump of the pixels and
the palette. Instead, various file formats (GIF, JPEG, PNG, etc.)
have been created that attempt to compress the data somewhat to
make smaller files.
Different file formats handle image transparency, which controls whether and
how the background shows through the image, in different ways. Some, such as
PNG, support an alpha channel, an extra value for every pixel reflecting the
transparency at that point. Others, such as GIF, simply designate one entry in
the palette as indicating transparency. Still others, like JPEG, don’t support
transparency at all.
imagecreate() Function
The imagecreate() function is an inbuilt function in PHP which is used to
create a new image. This function returns the blank image of given size. In
general imagecreatetruecolor() function is used instead of imagecreate()
function because imagecreatetruecolor() function creates high quality
images.
Syntax:
imagecreate( $width, $height )
Parameters: This function accepts two parameters as mentioned above and
described below:
● $width: It is mandatory parameter which is used to specify the image
width.
● $height: It is mandatory parameter which is used to specify the image
height.
imagescale() Function
The imagescale() function is an inbuilt function in PHP which is used to scale an image
using the given new width and height.
Syntax:
resource imagescale( $image, $new_width, $new_height , $mode =
IMG_BILINEAR_FIXED )
Parameters: This function accepts four parameters as mentioned above and described
below:
● $image: It is returned by one of the image creation functions, such as
imagecreatetruecolor(). It is used to create size of image.
● $new_width: This parameter holds the width to scale the image.
● $new_height: This parameter holds the height to scale the image. If the value of
this parameter is negative or omitted then the aspect ratio of image will preserve.
● $mode: This parameter holds the mode.
IMG_BILINEAR_FIXED: Fixed point implementation of the bilinear interpolation (default (also on image
creation)).
PHP class that contains many functions for creating and modifying PDFs. The FPDF class
includes many features like page formats, page headers, footers, automatic page break, line
break, image support, colors, links, and many more.
$pdf=new FPDF();
<?php
require('fpdf/fpdf.php');
// Instantiate and use the FPDF class
$pdf = new FPDF();
//Add a new page
$pdf->AddPage();
// Set the font for the text
$pdf->SetFont('Arial', 'B', 18);
// Prints a cell with given text
$pdf->Cell(60,20,'Hello');
// return the generated output
$pdf->Output();?>
Sort Functions For Arrays
● sort() - sort arrays in ascending order
● rsort() - sort arrays in descending order
● asort() - sort associative arrays in ascending order, according to the value
● ksort() - sort associative arrays in ascending order, according to the key
● arsort() - sort associative arrays in descending order, according to the value
● krsort() - sort associative arrays in descending order, according to the key
Example:
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
<?php
$numbers = array(50, 100, 20, 1, 400);
rsort($numbers);
print_r($numbers);
sort($numbers);
print_r($numbers);
?>
array_flip() Function
The array_flip() function flips/exchanges all keys with their associated
values in an array
Syntax
array_flip(array)
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);
?>
print_r()
The print_r() function prints the information about a
variable in a more human-readable way.
Syntax
print_r(variable, return);
Important Questions :
1. What is array ? How to store data in array ?
2. Write a program to create associative array in PHP.
3. Define function. How to define user defined function in PHP ? Give
example.
4. Write PHP script to sort any five numbers using array function.
5. Explain any four string functions in PHP with example.
6. Differentiate between Implode and Explode functions.
7. Write the use of array_flip()