UNIT-II_Arrays_Functions_and_Graphics
UNIT-II_Arrays_Functions_and_Graphics
************************************************
* Class Name : VJTech Academy ,Maharashtra *
* Author Name: Vishal Jadhav Sir *
* Mobile No: 9730087674 *
************************************************
============
PHP Array:
============
Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and
" . $cars[2] . ".";
?>
==================
What is an Array?:
==================
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
-However, what if you want to loop through the
cars and find a specific one? And what if you had
not 3 cars, but 300?
array();
-In PHP, there are three types of arrays:
==========
Programs:
==========
➢ Program-1:
<?php
$cars = array("Volvo",
"BMW", "Toyota");
foreach($cars as $x)
{
echo "<br>I like $x car";
}
?>
➢ Program-2:
<?php
$cars = array("Volvo", "BMW",
"Toyota","Wagnor","Kia");
$i=0;
while($i<cou
nt($cars))
{
echo "<br>I like car ".$cars[$i];
$i=$i+1;
}
?>
================================================
Get The Length of an Array - The count()Function
================================================
-The count() function is used to return the
length (the number of elements) of an array:
Example
<?php
$cars = array("Volvo",
"BMW", "Toyota"); echo
count($cars);
?>
===================
PHP Indexed Arrays:
===================
$cars = array("Volvo",
"BMW", "Toyota"); or the
index can be assigned
manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
========================
Multidimensional Arrays:
========================
• Arrays containing one or more arrays.
• Values in the multi-dimensional array are
accessed using multiple index.
- Example:
$a=array(10,20,30);
$b=array(40,50,60);
$c=array(70,80,90);
$multiArray=array($a,$b,$c);
Ex:
<?php
$a=array(10,20,30);
$b=array(40,50,60);
$c=array(70,80,90);
$multiArray=array($a,$b,$c);
========================
extract() function:
========================
• This function automatically creates local variables
from an array.
• The indexes of the array elements are the variable
names.
- Example:
<?php
$Language=array("pop"=>"C
Lang","oop"=>"C++","script"=>"JavaScript";
extract($Language);
echo "The value of Variable \$pop
= $pop"; echo "<br>The value of
Variable \$oop = $oop";
echo "<br>The value of Variable \$script =
$script";
?>
OUTPUT
The value of Variable $pop
= C Lang The value of
Variable $oop = C++
The value of Variable $script = JavaScript
- Example:
<?php
$a=10;
$b=20;
$c=30;
$d=40;
$e=50;
$Number=array("a","b","c","d","e");
$NumArray=compac
t($Number);
print_r($NumArra
y);
?>
OUTPUT:
Array ( [a] => 10 [b] => 20 [c] => 30 [d] => 40 [e] =>
50 )
========================
Implode() function:
========================
========================
Explode() function:
========================
Example:
<?php
$str="one|two|three|four|five";
$NumArray=explode
("|",$str);
print_r($NumArray
);
?>
OUTPUT:
Array ( [0] => one [1] => two [2] => three [3] => four
[4] => five )
========================
Array_Flip() function:
========================
===================
Iterator Functions:
===================
• Every PHP array keep track of current element.
• The pointer to the current element is known as
Iterator.
• Following are the iterator functions:
<?php
$m=array(10,20,30,40,50);
while(list($key,$val)=each($m))
{
echo "$key => $val ";
}
?>
OUTPUT:
0 => 10 1 => 20 2 => 30 3 => 40 4 => 50
****************************
Function and ITS Type:
****************************
• Function is a block of code.
• Function is subroutine, whenever we want we can call
to function.
• Function is a part of main program.
• Function will help us to achive modular approch.
• There are two types of functions.
1)Built-in function
2)UserDefined Function.
Advantages of Functions:
•PHP functions reduce the memory and complexity of the
program.
•Function help us to achive modular approch. It means
we can divide large script into small parts.
•Functions help in code re-usability.
•PHP functions reduces the bugs and save the time of
programmer.
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
UNIT-II : Arrays,Functions and Graphics
Calling function:
• When calling function is executed then program
controller goes to function definition.
• Using calling function, we can pass arguments to the
definition.
• After successfully execution of function definition,
program controller come back to end of the calling
function.
- Syntax:
function_name(argument_list);
Example-1:Display simple message
<?php
function vjtech()
{
echo "This is user defined function";
}
vjtech();
?>
Example-2: Addition of two numbers
<?php
function Addition()
{
$a=100;
$b=200;
$c=$a+$b;
echo "Addition of Two Number = $c";
}
Addition();
?>
=====================================
Anonymous Function (Lambda Function)
=====================================
• We can define the function that has no specified name.
• We call this function is an Anonymous function.
• It is also called as Lambda function.
• Anonymous function is created by using
create_function().
• This method taken two parameter.
• The first parameter is the parameter list for the
Anonymous function and second parameter contains the
actual code of function.
• Syntax:
$function_name=create_function(args_list,actual_code);
- Example:
<?php
$add=create_function('$a,$b','return($a+$b);');
$m=$add(10,20);
echo "Addition of Two Number is $m";
?>
2) strlen()
-------------------
• This is predefined method of PHP
• This method is used to find the length of string
including white spaces and special characters.
- Syntax:
strlen(string)
- Example:
<?php
$str="VJTech Academy";
echo "Your String is $str";
$len=strlen($str);
echo "<br>The length of string is $len";
?>
OUTPUT:
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
UNIT-II : Arrays,Functions and Graphics
Your String is VJTech Academy
The length of string is 14
3) strrev()
-------------------
• This is predefined method of PHP
• This functin takes string and return reversed copy of
it.
- Syntax:
strrev(string)
- Example:
<?php
$str="VJTech Academy";
echo "Your Original String is $str";
echo "<br>Reversed string is ".strrev($str);
?>
OUTPUT:
Your Original String is VJTech Academy
Reversed string is ymedacA hceTJV
4) strpos()
-------------------
• This is predefined method of PHP
- Example:
<?php
$str="VJTech Academy is one of the best academy in
Maharashtra";
echo "Your Original String is $str";
echo "<br>Position = ".strpos($str,"one");
?>
OUTPUT:
Your Original String is VJTech Academy is one of the
best academy in Maharashtra Position = 18
6) str_replace()
-------------------
• This is predefined method of PHP
• This function is used to replace some character of the
string with other characters.
- Syntax:
str_replace(search,replace,string,count)
-Where:
search - to be search for replacement
replace - the value which will replace search.
string - given string
count - total no of replacements performed on the given
string.
- Example:
<?php
$str="VJTech Academy is one of the good academy";
$search="good";
$replace="best";
$newStr=str_replace($search,$replace,$str,$count);
echo "Your Original String is $str";
echo "<br>After replacement your string : $newStr";
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
UNIT-II : Arrays,Functions and Graphics
echo "<br> No of replacement : $count";
?>
OUTPUT:
Your Original String is VJTech Academy is one of the
good academy
After replacement your string : VJTech Academy is one of
the best academy
No of replacement : 1
7) ucwords()
-------------------
• This is predefined method of PHP
• This function is used to convert first character of
each word to uppercase in a string.
- Syntax:
ucwords(string,separator)
-Where:
string - given string
separator - wors separator characters.
- Example:
<?php
$str="VJTech Academy is one of the good academy";
echo "Your Original String is $str";
echo "<br>After applying ucwords() method string is
:".ucwords($str);
?>
OUTPUT:
Your Original String is VJTech Academy is one of the
good academy
After applying ucwords() method string is :VJTech
Academy Is One Of The Good Academy
8) strtoupper()
-------------------
• This is predefined method of PHP
• This function is used to convert all characters of
given string to uppercase.
- Syntax:
strtoupper(string)
- Example:
<?php
$str="VJTech Academy is one of the best academy";
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
UNIT-II : Arrays,Functions and Graphics
echo "Your Original String is $str";
echo "<br>String after applying strtoupper()
method:".strtoupper($str);
?>
OUTPUT:
Your Original String is VJTech Academy is one of the
best academy
String after applying strtoupper() method:VJTECH ACADEMY
IS ONE OF THE BEST ACADEMY
9) strtolower()
-------------------
• This is predefined method of PHP
• This function is used to convert all characters of
given string to lowercase.
- Syntax:
strtolower(string)
- Example:
<?php
$str="VJTech Academy is one of the best academy";
echo "Your Original String is $str";
echo "<br>String after applying strtolower()
method:".strtolower($str);
?>
OUTPUT:
Your Original String is VJTech Academy is one of the
best academy
String after applying strtolower() method:vjtech academy
is one of the best academy
10) strcmp()
-------------------
• This is predefined method of PHP
• This function is used compare two string. It will give
below results:
string1==string2 : 0
string1>string2 : >0
string1<string2 : <0
- Syntax:
strcmp(string1,string2)
- Example:
<?php
$str1="VJTech Academy";
$str2="VJTech Academy";
echo "<br>String compare results:".strcmp($str1,$str2);
?>
OUTPUT:
String compare results:0
=======================
BASIC GRAPHICS CONCEPT
=======================
• PHP supports basic computer graphics.
• By using this, we can create images with jpeg,gif,png
extension.
• Before creating images in PHP, first we need undertsand
some basic concepts related to images.
• In computer, normally we can create colors using RGB
model.
• RGB stands for red,green and blue.
• In computer, we have two different types of images
namely raster and vector. Raster image is also called
as bitmap images. This image will create on the basis
of pixels.
• The vector image uses mathematical equations to create
image.
• Vector images are great for digrams that includes
lines,curves, etc but that is not suitable for
photographs or images.
• The image functions that php uses are based on the GD
image library that is developed by Tom Boutell.
• PHP GD image function work with four main raster image
format JPEG, PNG, GIF, JPG.
• In PHP, we use imagecreate() to create images.
- Syntax:
imagecreate($width,$height)
- Example:
<?php
$img=imagecreate(500,500);
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
UNIT-II : Arrays,Functions and Graphics
$bgcolor=imagecolorallocate($img,150,200,230);
$fontcolor=imagecolorallocate($img,100,150,175);
imagestring($img,2,50,50,"VJTech Academy",$fontcolor);
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
?>
Images with Text:
=================
• The function imagettftext() is used to write text in
images:
- Syntax:
imagettftext($img,$size,$angle,$x,$y,$color,$fontfile
,$strtext)
- Where:
$img - image resource which is created using
imagecreate()
$size - font size
$angle - Angle in degree.
$x & $y - x Axis and y axis value in pixel.
$color - color of the text
$font file - font which apply on the text.
$text - text which will going to displayed.
- Example:
<?php
$img=imagecreate(500,500);
$bgcolor=imagecolorallocate($img,150,200,230);
$fontcolor=imagecolorallocate($img,100,150,175);
imagettftext($img,20,0,5,5,$fontcolor,'arial.ttf',"VJTec
h
Academy");
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
?>
VJTech Academy…