Unit 2 PHP
Unit 2 PHP
What is an Array?
An array is a special variable that can hold many values under a single name,
and you can access the values by referring to an index number or name.
By default, the first item has index 0, the second item has item 1, etc.
<!DOCTYPE html>
<html>
<body>
<?php
echo $cars[0];
?></body>
</html>
Output: Volvo
<!DOCTYPE html>
<html>
<body>
<pre>
<?php
var_dump($car);
?>
</pre>
</body>
</html>
Output:
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(1964)
}
PHP implode() Function
the implode() function returns a string from the elements of an array.
Note: The implode() function accept its parameters in either order. However,
for consistency with explode(), you should use the documented order of
arguments.
Syntax
implode(separator,array)
<!DOCTYPE html>
<html>
<body>
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode("+",$arr)."<br>";
echo implode("-",$arr)."<br>";
echo implode("X",$arr);
?></body>
</html>
Output:
Syntax
explode(separator,string,limit)
Parameter Values
Parameter Description
Possible values:
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
?>
</body>
</html>
Output:
$name = "Fred";
$age = 35;
$wife = "Betty";
This function uses array keys as variable names and values as variable values.
For each element it will create a variable in the current symbol table.
Syntax
extract(array, extract_rules, prefix)
<!DOCTYPE html>
<html>
<body>
<?php
$a = "Original";
extract($my_array);
?>
</body>
</html>
Syntax
compact(var1, var2...)
<!DOCTYPE html>
<html>
<body>
<?php
$firstname = "Peter";
$lastname = "Griffin";
$age = "41";
?></body>
</html>
Output: Array ( [firstname] => Peter [lastname] => Griffin [age] => 41 )
Traversing Array
Number of times we need to visit each element of any array.
Following are way for traversing array
For()
Foreach()
<?php
$name_one = [
];
// Looping through an array using foreach
echo "Husband is " . $val . " and Wife is " . $val_value . "\n";
$keys = array_keys($name_one);
$round = count($name_one);
?>
Output:
Accessing the elements directly:
zara
sara
any
Rani
Ravina
PHP | Functions
A function is a block of code written in a program to perform some specific task.
We can relate functions in programs to employees in a office in real life for a
better understanding of how functions work. Suppose the boss wants his
employee to calculate the annual budget. So how will this process complete?
The employee will take information about the statistics from the boss, performs
calculations and calculate the budget and shows the result to his boss.
Functions works in a similar manner. They take informations as parameter,
executes a block of statements or perform operations on this parameters and
returns the result.
PHP provides us with two major types of functions:
Syntax:
function function_name(){
executable code;
}
Example:
PHP
<?php
?>
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 callbacks, function tables, and so forth.
Example
<?php
function hello(){
echo "Hello World";
}
$var="Hello";
$var();
?>
Output :Hello World
PHP Anonymous functions
Anonymous function is a function without any user defined name. Such a function is also
called closure or lambda function. Sometimes, you may want a function for one time use. Closure is an
anonymous function which closes over the environment in which it is defined. You need to specify use
keyword in it.Most common use of anonymous function to create an inline callback function.
Example
<?php
$var = function ($x) {return pow($x,3);};
echo "cube of 3 = " . $var(3);
?>
Output: cube of 3 = 27
Syntax
strcmp(string1,string2)
<!DOCTYPE html>
<html>
<body>
<?php
echo strcmp("Hello","Hello");
echo "<br>";
echo strcmp("Hello","hELLo");
?></body>
</html>
Output:
0
-32
Syntax
strlen(string)
<?php
?>
Output: 12
Syntax
strncmp(string1,string2,length)
<!DOCTYPE html>
<html>
<body>
<?php
echo strncmp("Hello","Hello",6);
echo "<br>";
echo strncmp("Hello","hELLo",6);
?>
</body>
</html>
Output:
0
-32
Syntax
strrev(string)
<?php
?>
<!DOCTYPE html>
<html>
<body>
<p>Search the string "Hello World!", find the value "world" and replace it
with "Peter":</p>
<?php
?>
</body>
</html>
Output: Search the string "Hello World!", find the value "world" and replace it with
"Peter":
Hello Peter!
Syntax
strtolower(string)
<?php
?>
Syntax
strtoupper(string)
<?php
?>
Output:HELLO WORLD!
Syntax
ucwords(string, delimiters)
<?php
?>
Syntax
strlen(string)
<?php
echo strlen("Hello world!");
?>
Output: 12
Syntax
str_word_count(string,return,char)
<?php
?>
Output: 2
Syntax
str_repeat(string,repeat)
<?php
echo str_repeat("Wow",13);
?>
Output: WowWowWowWowWowWowWowWowWowWowWowWowWow
Syntax
strpos(string,find,start)
<?php
echo strpos("I love php, I love php too!","php");
?>
Output: 7
Syntax
strrpos(string,find,start)
<?php
echo strrpos("I love php, I love php too!","php");
?>
Output: 19
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.
HP Imagecreate( ) Function
Image create ( ) function is another inbuilt PHP function mainly used to create a new
image. The function returns the given image in a specific size. We need to define the
width and height of the required image. Instead of the image create ( ) function, we can
also use other creative functions like imagecreatetruecolor( ), which is a better
alternative as it will return a better image quality.
Syntax
The image creates ( ) function returns the resource identifier of an image on successful
execution of the program and FALSE on a failed attempt.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta http - equiv="X - UA - Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<title>PHP</title>
</head>
<body>
<?php
// to define the size of the image
$image= imagecreate(400, 200);
// to define the background color of the image
$background-color= imagecolorallocate($image, 0, 150, 2);
// to define the text color of the image
$text-color= imagecolorallocate($image, 255, 255, 255);
// function which will define the character to be displayed on the screen
Imagestring($image, 5, 180, 100, "GOOD MORNING EVERYONE", $text-color);
Imagestring($image, 3, 160, 120, "HELLO WORLD", $text-color);
Header("Content - Type: image/png");
Imagepng($image);
Imagedestroy($image);
?>
</body>
</html>
Output
Scaling Images
There are two ways to change the size of an image.
ImageCopyResampled( ) function
ImageCopyResized( ) function
ImageCopyResized(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);
ImageCopyResampled(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);
In this example, we are having a text file that contains toys detail as records.
We are reading this file’s content, then generate a PDF document and display
it in the browser.
<?php
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$row=file('toys.txt');
$pdf->SetFont('Arial','B',12);
foreach($row as $rowValue) {
$data=explode(';',$rowValue);
foreach($data as $columnValue)
$pdf->Cell(90,12,$columnValue,1);
$pdf->SetFont('Arial','',12);
$pdf->Ln();
}
$pdf->Output();
?>