Professional Documents
Culture Documents
OPEN SOURCE
PROGRAMMING
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP Array - Creation
Various array creation method is available in PHP;
they are
1. Many values in a single creation
$numbers = array( 1, 2, 3, 4, 5);
y w ith
Arra k e y <?php
p l ici t
Im $name[’firstname’] = “Albert”;
$name[’lastname’] = “Einstein”;
$name[’age’] = 124;
<?php $name[3]=“Scientist”;
$flavor[] = ‘blue rasberry’; ?>
$flavor[] = ‘root beer’;
$flavor[] = ‘pineapple’;
?>
y w ith
Arra it key
x p l ic
E
4
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP Array Types
There are three different kind of arrays and each array value
is accessed using an ID c which is called array index.
1. Numeric array − An array with a numeric index. Values are
stored and accessed in linear fashion.
2. Associative array − An array with strings as index. This
stores element values in association with key values rather
than in a strict linear index order.
3. Multidimensional array − An array containing one or more
arrays and values are accessed using multiple indices.
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Numeric Arrays
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Associative Arrays
•The associative arrays are very similar to numeric arrays in
term of functionality but they are different in terms of their
index. Associative array will have their index as string so that
you can establish a strong association between key and values.
E.g.,
To store the salaries of employees in an array, a numerically
indexed array would not be the best choice. Instead, we could
use the employees names as the keys in our associative array,
and the value would be their respective salary.
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP Multidimensional Arrays
•A multi-dimensional array each element in the main array can also
be an array. And each element in the sub-array can be an array, and
so on. Values in the multi-dimensional array are accessed using
multiple index.E.g., create a two dimensional array to store marks of
three students in three subjects
$marks = array(
"mohammad" => array ( "physics" => 35, "maths" => 30, "chemistry" => 39 ),
"qadir" => array ( "physics" => 30, "maths" => 32, "chemistry" => 29 ),
"zara" => array ( "physics" => 31, "maths" => 22, "chemistry" => 39 )
);
Accessing two dimensional array:
echo "Marks for mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for qadir in maths : ";
echo $marks['qadir']['maths'] . "<br />";
echo "Marks for zara in chemistry : " ;
echo $marks['zara']['chemistry'] . "<br />"; ?> 8
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP Array Traverse
By the help of PHP for each loop, we can easily
traverse the elements of PHP array.
<?php
$salaries = array("mohammad"=>2000, "qadir"=>1000, "zara"=>500);
Foreach($salaries as $sal)
echo $sal,”<br>”;
Foreach($salaries as $key=>$val)
echo $key,“ ‘s salary is “,$val,”<br>”;
?>
Output:
==========
2000
1000
500
Mohammad’s salary is 2000
Qadir’s salary is 1000
Zara’s salary is 500 9
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
E.g.:
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r($a);
?>
Output:
Array ( [a] => Dog [b] => Cat [c] => Horse )
10
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
array_combine(array1,array2) creates an array by combining
two other arrays, where the first array is the keys, and the other array
is the values.
E.g.:
<?php
$a1=array("a","b","c","d");
$a2=array("Cat","Dog","Horse","Cow");
$arr3 =array_combine($a1,$a2);
Print_r($arr3);
?>
Output:
Array ( [a] => Cat [b] => Dog [c]
=> Horse [d] => Cow )
11
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
array_count_values(array) returns an array, where
the keys are the original array's values, and the values is
the number of occurrences.
E.g:
<?php Output:
$a=array("Cat","Dog","Horse","Dog");
$count_arr = array_count_values($a); Array ( [Cat] => 1 [Dog] => 2
Print_r($count_arr); [Horse] => 1 )
?>
12
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
array_fill(start,number,value ) function returns an array
filled with the values you describe
E.g:
Output:
<?php
$a=array_fill(2,3,"Dog");
Array ( [2] => Dog [3] => Dog [4] => Dog )
print_r($a);
?>
13
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
The array_flip(array ) function returns an array with all
the original keys as values, and all original values as
keys.
E.g:
<?php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse");
$flipped = array_flip($a);
print_r($flipped);
?>
Output:
Array ( [Dog] => 0 [Cat] => 1 [Horse] => 2 )
14
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
The array_key_exists(key,arr ) function checks
an array for a specified key, and returns true if
the key exists and false is the key does not exist.
E.g:
<?php
$a=array("a"=>"Dog","b"=>"Cat");
Output:
if (array_key_exists("a",$a))
echo "Key exists!"; Key exists!
else
echo "Key does not exist!";
?>
15
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
E.g:
<?php
$a=array("Dog","Cat","Horse");
Output:
$val1 = array_pop($a); Horse
Echo $val1,”<br>”; Array ( [0] => Dog [1] => Cat )
print_r($a); Dog
$val2 = array_shift($a); Array ( [0] => Cat )
Echo $val2,”<br>”;
print_r($a);
?>
16
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
The array_push(array,value1,value2...) function inserts
one or more elements to the end of an array.
Array_unshift(array) – inserts an element at the
beginning of the array
E.g:
<?php
$a=array("Dog","Cat");
array_push($a,"Horse","Bird");
Print_r($a);
array_unshift($a, “fish”);
print_r($a);
?>
Output:
Array ( [0] => Dog [1] => Cat [2] => Horse [3] => Bird )
Array ( [0] => fish [1]=>Dog [2] => Cat [3] => Horse [4]
=> Bird )
17
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
Output ????????
E.g:
<?php
$a=array("a"=>"Dog","b"=>"Cat"); Output:
array_push($a,"Horse","Bird");
print_r($a); Array ( [a] => Dog [b] => Cat [0]
?> => Horse [1] => Bird )
18
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
The array_reverse(array,preserve) function returns an
array in the reverse order.
E.g:
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>" Output:
Horse");
print_r(array_reverse($a)); Array ( [c] => Horse [b] => Cat
?> [a] => Dog )
19
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
E.g:
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"
Horse"); Output:
$val = array_search("Dog",$a);
echo $val; a
?>
20
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
E.g: Output:
<?php Array
$my_array = array("a" => "Dog", "b" => (
"Cat", "c" => "Horse"); [c] => Horse
arsort($my_array); [a] => Dog
print_r($my_array); [b] => Cat
?> )
21
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
E.g: Output:
<?php Array
$my_array = array("a" => "Dog", "b" => (
"Cat", "c" => "Horse"); [b] => Cat
asort($my_array); [a] => Dog
print_r($my_array); [c] => Horse
?> )
22
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
E.g: Output:
<?php Array
$my_array = array("a" => "Dog", "b" => (
"Cat", "c" => "Horse"); [c] => Horse
krsort($my_array); [b] => Cat
print_r($my_array); [a] => Dog
?> )
23
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
E.g: Output:
<?php Array
$my_array = array("a" => "Dog", "b" => (
"Cat", "c" => "Horse"); [a] => Dog
ksort($my_array); [b] => Cat
print_r($my_array); [c] => Horse
?> )
24
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
The rsort(array,sorttype) function sorts an array by
the values in reverse order. This function assigns new keys for the
elements in the array. Existing keys will be removed. This
function returns TRUE on success, or FALSE on failure.
E.g: Output:
<?php Array
$my_array = array("a" => "Dog", "b" => (
"Cat", "c" => "Horse"); [0] => Horse
rsort($my_array); [1] => Dog
print_r($my_array); [2] => Cat
?> )
25
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
The sort(array,sorttype) function sorts an array by the
values. This function assigns new keys for the elements in the
array. Existing keys will be removed. This function returns TRUE
on success, or FALSE on failure
E.g: Output:
<?php Array
$my_array = array("a" => "Dog", "b" => (
"Cat", "c" => "Horse"); [0] => Cat
sort($my_array); [1] => Dog
print_r($my_array); [2] => Horse
?> )
26
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Array Functions
Other Array Functions:
27
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP – String
A PHP string is a sequence of characters i.e. used to store and
manipulate text. Strings can be specified either by double
quotes(“) or single quotes(‘).
E.g:
<?php
$str 1= "Hello World!";
$str2 = ‘Sample PHP’;
echo $str1;
Echo $str2;
?>
28
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The explode(separator, string, limit) function breaks a
string into an array.
Output:
Array
(
E.g: [0] => Hello
[1] => world.
<?php [2] => It's
$str = "Hello world. It's a beautiful [3] => a
day."; [4] => beautiful
print_r (explode(" ",$str)); [5] => day.
?> )
29
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The implode(separator,array ) &
join(separator,array ) function returns a string from the
elements of an array.
E.g: Output:
<?php Hello World! Beautiful Day!
$arr =
array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
30
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The ltrim(string,charlist) & rtrim(string,charlist) function
will remove whitespaces or other predefined character from the left and
right side of a string respectively.
The soundex(string) function calculates the soundex key of a
string.
The str_shuffle(string) function randomly shuffles all the
characters of a string.
31
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The strlen(string) function returns the length of a string.
E.g: Output:
<?php Length = 12
$a= “hello World!”;
echo “Length = “, strlen($a);
?>
32
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The strrev(string) reverses the given string.
E.g: Output:
Reverse of “hello World!” is !
<?php
dlroW olleh
$a= “hello World!”;
echo “Reverse of \”$a\” is “, strrev($a);
?>
33
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The strtoupper(string) converts to upper case character
The strtolower(string) converts to lower case character
E.g: Output:
<?php Upper of “hello World!” is
$a= “hello World!”; HELLO WORLD!
echo “Upper of \”$a\” is “, strtoupper($a); Lower of “hello World!” is hello
echo “Lower of \”$a\” is “, strtolower($a); world
?>
34
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The strpos(string,exp) returns the numerical position of
first appearance of exp.
The strrpos(string,exp) returns the numerical position of
last appearance of exp.
E.g: Output:
<?php 2
$a= “hello World!”; 9
echo strpos($a,”l”),”<br>”;
echo strrpos($a,”l”);
?>
35
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The substr(string,start,length) function returns a sub
string of the size “length” from the position of “start”.
E.g: Output:
<?php lo
$a= “hello World!”;
echo substr($a,3,2);
?>
36
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The substr_count(string,substr) counts number of times
a sub string occurred in given string.
E.g: Output:
<?php 2
$a= “hello Worlod!”;
echo substr_count($a,”lo”);
?>
37
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The ucfirst(string) converts the first character to upper case.
The ucwords(string) converts the first character of each word
to upper case.
E.g: Output:
<?php Hello world!
$a= “hello world!”; Hello World!
echo ucfirst($a),”<br>”;
echo ucwords($a);
?>
39
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The parse_str(string,arr) function parses a query string
into variables.
E.g: Output:
<?php 23
parse_str("id=23&name=Kai%20Jim"); Kai Jim
echo $id."<br />";
echo $name;
?>
40
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The str_replace(find,replace,string,count ) function
replaces some characters with some other characters in a string.
Note: str_ireplace() – for case sensitive
E.g: Output:
<?php
$str=“Hallo World”; str_ireplace(“al",“el",$str,
$i)); The new string is Hello
echo “the new string is “,$str World
echo "Replacements: $i"; Replacements: 1
?>
41
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
The strcasecmp(string1,string2) function compares two strings
as case insensitive. Strcmp(string1,string2) compares as case
sensitive.
This function returns:
0 if the two strings are equal
< 0 if string1 is less than string2
> 0 if string1 is greater than string2
E.g:
Output:
<?php
0
echo strcasecmp("Hello world!","HELLO WORLD!");
1
echo “<br>”;
echo strcmp("Hello world!","HELLO WORLD!");
?>
42
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP Date
Returns a string formatted according to the given
format string, timestamp is optional and defaults to the
value of time().
string date ( string $format [, int $timestamp = time() ] )
echo "Today is " . date("Y/m/d") . "<br>"; // 2017/Aug/09
echo "Today is " . date("Y.m.d") . "<br>"; // 2017.Aug.09
echo "Today is " . date("Ymd") . "<br>";//// 2017Aug09
echo "Today is " . date("l");// Wednessday
43
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP Date
44
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP Date Format String
45
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP Date Format String
String Meaning Output
F
A full textual representation of a month, January through
such as January or March December
Numeric representation of a month, with
m 01 through 12
leading zeros
A short textual representation of a month,
M Jan through Dec
three letters
Numeric representation of a month, without
n 1 through 12
leading zeros
t Number of days in the given month 28 through 31
A full numeric representation of a year, 4 Examples: 1999 or
Y
digits 2003
y A two digit representation of a year Examples: 99 or
03
Lowercase Ante meridiem and Post
a am or pm
meridiem
46
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
PHP Date Format String
47
Open Source
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT