Unit 2 PHP
Unit 2 PHP
$mobile= array(“samsung",
", ““nokia", “oneplus");
$mobile= array(“samsung",
", “nokia",
“ “oneplus");
echo $mobile[0];
echo $mobile[1];
$mobile= array(“samsung",
", “nokia",
“ “oneplus");
echo $mobile[1];
$mobile[1]=“motorola”;”;
echo $mobile[1];
foreach loop
$mobile= array(“samsung", “nokia", “oneplus");
foreach( $mobile as $x)
{
echo “$x”;
echo “<br>”;
}
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
Display array using for loop
$mobile= array("samsung", "nokia", "oneplus");
<?php
$car = array("brand"=>"Ford", "model"=>"Mustang",
"year"=>1964);
var_dump($car);
?>
Access Associative arrays
To access an array item you can refer to the key name
name.
echo $car["model"];
Change Value
To change the value of an array item, use the key
name:
$car["year"] = 2024;
var_dump($car);
Foreach loop
$car = array("brand"=>"Ford", "model"=>"Mustang",
"year"=>1964);
echo "<pre>";
print_r($car);
$car=array_flip($car);
print_r($car);
echo "</pre>";
Multidimensional arrays
A multidimensional array is an array containing one or
more arrays.
Consider a table: (3 rows and 3 columns)
Abc 10 20
Pqr 55 65
Xyz 67 97
Multidimensional arrays
Abc 10 20
Pqr 55 65
Xyz 67 97
$value = array (
array ("Abc","10","20"),
array ("Pqr","55","65"),
array ("Xyz","67","97")
);
<?php
$value = array (
array ("Abc","10","20"),
array ("Pqr","55","65"),
array ("Xyz","67","97")
);
echo "<pre>";
var_dump($value);
print_r($value);
echo "</pre>";
?>
Using for loop
for ($r=0; $r<3 ; $r++)
{
for($c=0; $c<3 ; $c++)
{
echo $value[$r][$c];
}
echo "<br>";
}
Using foreach loop
foreach ($value as $v1)
{
foreach ($v1 as $v2)
{
echo "$v2 ";
}
echo "<br>";
}
Program to find sum of array elements
<?php
$value = array(10,20,30);
$sum=0;
echo "<pre>";
print_r($value);
sort($value);
print_r($value);
echo "</pre>";
?>
<?php
$car = array("brand"=>"Ford", "model"=>"Mustang",
"color"=>"black");
echo "<pre>";
print_r($car);
krsort($car);
print_r($car);
echo "</pre>";
?>
PHP explode() Function
The explode() function breaks a string into an array
array.
Syntax
explode(separator,
separator, string, limit)
limit
echo "<pre>";
print_r($output);
echo "</pre>";
//print_r(explode("," ,$input ));
?>
PHP implode() Function
The implode() function returns a string from the
elements of an array.
Syntax
implode(separator,array
separator,array)
The separator parameter of implode() is optional
and by default it is an empty string.
PHP implode() Function
<?php
$input=array("abc","def","ghi","jkl");
$output=implode(" ",$input);
echo "$output";
?>
PHP string Functions
strlen() Function
: This function takes a string as argument
and returns and integer value representing
the length of string. It calculates the length
of the string including all the whitespaces
and special characters.
strrev() Function
: The strrev()
() function reverses a string.
string
PHP string Functions
strtoupper() Function
:The strtoupper()
() function takes a string as
argument and returns the string with all
characters in Upper Case.
Case
strtolower()() Function
:This
This function takes a string as argument ans
returns the string with all of the characters in
Lower Case.
PHP string Functions
ucwords()
() Function
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.
unchanged
PHP string Functions
strpos() Function:
This function helps us to find the position of
the first occurrence of a string in another
string. This returns an integer value of the
position of the first occurrence of the string.
This function is case-sensitive
sensitive.
Syntax: strpos(original_str
original_str, search_str,
start_pos);
PHP string Functions
str_replace()
() Function
The str_replace()
() function replaces some
characters with some other characters in a
string.
Syntax:
str_replace(find,replace,string,count
find,replace,string,count)
Count is optional
PHP string Functions
strcmp() Function
The strcmp() () function compares two strings
strings.
It is case sensitive.
PHP Functions
PHP has more than 1000 built
built-in functions, and
in addition you can create your own custom
functions / user defined functions.
A function is a block of statements that can be
used repeatedly in a program.
A function will not execute automatically.
A function will be executed by a call to the
function.
Create a user defined Function
A user-defined
defined function declaration starts with
the keyword function, followed by the name of
the function:
function hello()
{
echo “hello”;
}
Call a user defined Function
To call the function, just write its name followed
by parentheses ()
function hello()
{
echo “hello world”;
}
hello();
hello();
Function parameters/Arguments
Information can be passed to functions through
arguments. An argument is just like a variable
variable.
display(10);
display(20);
Default parameters/Arguments
function display( $x=20 )
{
echo “value is $x”;
}
$value= input(40);
echo $value;
//echo input(50);
Function with return values
function sum( $x, $y )
{
$z=$x+$y;
return $z;
}
$value= sum(40,50)
echo $value;
echo current($choice)."<br>";
echo next($choice)."<br>";
echo prev($choice)."<br>";
echo end($choice)."<br>";
echo reset($choice)."<br>";
echo key($choice)."<br>";
echo pos($choice)."<br>";
echo next($choice)."<br>";
echo pos($choice)."<br>";
<?php
$input="Govt poly khamgaon";
$op=explode(" ",$input);
$c=count($op);
echo "number of words = $c";
?>
<?php
$im=imagecreate(500,500);
imagecolorallocate($im,0,0,100);
$c=imagecolorallocate($im,255,255,255);
imagestring($im,5,200,0,"GP Khamgaon",$c);
imagefilledrectangle($im, 40, 90,450 , 25, $c);
header("Content-Type:
Type: image/png");
imagepng($im);
imagedestroy($im);
?>
Basics graphics in php
imagecreate(( $width, $height )
used to create a new image.
In general imagecreatetruecolor()
imagecreatetruecolor function is
used instead of imagecreate()
imagecreate function
because imagecreatetruecolor()
imagecreatetruecolor function creates
high quality images.
Basics graphics in php
imagecolorallocate ( $image, $red, $green, $blue);
require("fpdf.php");
Pdf files in php
Create object of FPDF
$pdf->SetFont('Arial','UIB',
'UIB',20);
$pdf->cell(80,10,"GP Khamgaon");
Khamgaon
//x and y coordinages along with text in cell()
method
Pdf files in php
Use output() method to get the output
require("fpdf.php");
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetTextColor(255,0,0);
>SetTextColor(255,0,0);
$pdf->SetFont('Arial','UIB',20);
>SetFont('Arial','UIB',20);
$pdf->cell(80,10,"GP
>cell(80,10,"GP Khamgaon");
$pdf->output();
?>
THANK YOU