8/3/2019 OSP Notes1
1/104
Open Source Programming
ByProf. A. Vijayarani
SITEVIT
8/3/2019 OSP Notes1
2/104
Open Source Programming
IntroductionUnit I
Open source Programming PHP, Apache,
MySQL, Postgress, SQL and Perl-Overview of PHP Variables, operators,Constants, control structures arrays,Functions, classes Handling files.
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
3/104
By allowing the open exchange ofinformation, programmers from all over
the world contribute to make a trulypowerful and efficient piece of softwarewithout royalties or fees.
What is Open source
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
4/104
Why OSP ROCKS?
They are cross-platform and
technology-neutral.They must not restrict other software.
They embrace diversity.
They are free.
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
5/104
OSP Software - OS Linux operating system kernel based on Unix
GNU Project a sufficient body of free software
OpenBSD operating system derived from Unix
FreeBSD operating system derived from Unix
OpenSolaris Unix Operating System from Sun
Microsystems
Symbian real-time mobile operating system
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
6/104
OSP Software - ServerApache HTTP web server
Tomcat web server web container
Mediawiki wiki server software
Alfresco, TYPO3 content management systemRenovatioCMS content management system
Joomla content management system
Drupal content management system
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
7/104
WordPress blog softwareMongoDB document-oriented, non-relational
databaseEclipse software development environment
comprising an integrated development environment(IDE)Moodle course management system or virtuallearning environment
openSIS open source Student Information System
OSP Software
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
8/104
osCommerce ecommercePeaZip File archiver
Mozilla Firefox web browser
Mozilla Thunderbird e-mail client
OpenOffice.org office suite
Stockfish chess engine series, one of the strongestchess programs
7-Zip File Archiever
Open Source Programming
OSP Software
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
9/104
OSP Software (Others)
PHP Hypertext Preprocessor
PERL & Python Interpreted Dynamic Language
MySQL Data Base
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
10/104
Intro to AMP Package
MySQL: It enables PHP and Apache to work
together to access and display data in a readableformat to a browser.
Open Source Programming
AMP is an acronym formed from the initials ofApache, MySQL and PHP or Python or Perl.
Apache: It acts as Web server. Its main job is toparse any file requested by a browser and display thecorrect results according to the code within that file.
PHP: PHP is a server-side scripting languagethat allows your Web site to be truly dynamic.
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
11/104
Open Source Programming
AMP as Restaurant
Apache This is theChef. Whatever people ask
for, prepares it withoutcomplaint. He/She is quick,flexible, and able toprepare a multitude ofdifferent types of foods.
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
12/104
8/3/2019 OSP Notes1
13/104
AMP as RestaurantMySQL This is Stockroom of ingredients.
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
14/104
Appache is a Web server Responds to clientrequests by providing resources
URI (Uniform Resource Identifier)
Web server and client communicate with platform-independent Hypertext Transfer Protocol (HTTP)
Apache Introduction
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
15/104
Request methods
get
post
Retrieve and send client form data to Web
server
Post data to a server-side form handler
Open Source Programming
HTTP request types
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
16/104
Multi-tier application (n-tier application)
Information tier (data or bottom tier) Maintains data for the application
Stores data in a relational database management system
(RDBMS) Middle tier Implements business logic and presentation logic
Control interactions between application clients andapplication data
Client tier (top tier)
Applications user interface Users interact directly with the application through the
client tier
Open Source Programming
System Architecture
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
17/104
Open Source Programming
Overview of Php PHP Hypertext Preprocessor.
Other Names : Personal Home Page, ProfessionalHome Page
PHP is a server side scripting language.
Capable of generating the HTML pages
HTML generates the web page with the static text and
images.
However the need evolved for dynamic web based
application, mostly involving database usage.
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
18/104
Features of PHP
Is an open source.
There are no. of server side scripting available likeASP, SSJS, JSP..
PHP involves simplicity in scripting (..generally usingthe database) platform independence.
PHP is primarily designed for web applicationswell optimized for the response times needed forweb applications
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
19/104
Open Source Programming
CLIENT
WEBSERVER
HTTP Request
(url)
Gets Page
Hello
Interprets the PHP code
Server response
How it works?
Browser creates
the web page
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
20/104
PHP with HTML
Open Source Programming
PHP programs are written using a text editor,such as Notepad or WordPad, just like HTMLpages.
PHP pages, for the most part, end in a .phpextension. This extension signifies to the serverthat it needs to parse the PHP code beforesending the resulting HTML code to the viewersWeb browser.
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
21/104
Open Source Programming
PHP with HTML What makes PHP so different is that it not only allowsHTML pages to be created.
It is invisible to your Web site visitors. The only thingthey see the resulting HTML output. This gives you more
security for your PHP code and more flexibility in writing it.
HTML can also be written inside the PHP section of yourpage.
PHP can also be written as a standalone program, with no
HTML
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
22/104
Open Source Programming
PHP SyntaxPHP is denoted in the page with opening andclosing tags as follows:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Opening Tag
Closing Tag
8/3/2019 OSP Notes1
23/104
My First PHP Program
Open Source Programming
PHP ProgramsCode
Result
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
24/104
Open Source Programming
PHP ProgramsCode Result
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
25/104
Data Type
PHP is an untyped language variables type can change on the fly.
Four basic data types:
More data types
1. Integer2. Double3. String
4. Boolean
1. Array2. Object
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
26/104
..values that never changes.
Constants are defined in PHP by using the
define( ) function.
For e.g.define(NCST, National Centre for
Software Technology)
defined() function says whether theconstant exists or not.
Constants
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
27/104
Variables The variables in PHP are declared by
appending the $ sign to the variable name.
For e.g
$company = NCST;
$sum = 10.0;
Variables data type is changed by the value
that is assigned to the variable.
Type casting allows to change the data type
explicitly.
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
28/104
Variables
Open Source Programming
Rules for Naming Variables:
In PHP, unlike some other programming languages,there is no restriction on the size of a variable name.
Variable names should identified by dollar ($) symbol.
Variable names can begin with an underscore.
Variable names cannot begin with a numericcharacter.
Variable names must be relevant and self-explanatory.
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
29/104
Variables
Open Source Programming
Valid Examples In Valid Examples
$prod_desc $9OctSales$Intvar Sales123$_Salesamt $*asgs
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
30/104
$MyStrVal = "This is an example of a string value";$MyIntVal = 145665; // An integer value$MyBoolval = True; // A Boolean value can either be
True or False$MyFloatVal=2346.45 // A float value$MyArrVal[0] = "My"; //An array containing three
elements$MyArrVal[1] = "First";
$MyArrVal[2] = "Array";
Variables (cont)
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
31/104
Settype
Open Source Programming
In PHP, Variable type can be changed by Settype
Syntax:Settype(Variablename, newDataType);
E.g.$pi = 3.14 //floatSettype($pi,string); //now string 3.14
Settype($pi,integer);// now integer - 3
Settype
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
32/104
Settype & Gettype
Open Source Programming
Syntax:Gettype(Variablename);
E.g.$pi = 3.14; //float
print gettype($pi);Print ---$pi
;
Settype($pi,string);print gettype($pi);Print ---$pi
;
Settype($pi,integer);print gettype($pi);Print ---$pi
;
In PHP, Variable type and can know by Gettype.
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
33/104
Operators All the operators such as arithmetic,
assignment, Comparison, and logical
operators are similar to the operators in C
and C++.
In PHP the string concatenation operator is
denoted by .
For e.g.$name = My name is.$myname;
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
34/104
Quotes ( Vs. )
Open Source Programming
In a double-quoted string any variable names are expanded
to their values.In a single-quoted string, no variable expansion takes place.
$name = "Phil";$age = 23;echo '$name is $age';
$name = "Phil";$age = 23;echo $name is $age;
Output is$name is $age
Output isPhil is 23
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
35/104
Operators (cont)Comparison
OperatorLogical
Operator
|| ( highprecedence)
&& ( high
precedence)
Xor
!
Or
And
===
(identical)
>=
$num2){
$max = $num1;}
else{
$max = $num2;}
Syntax E.g.
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
40/104
2. Ifelse if Statement multiple decision stmt
if (){
//True part Php code}
else if (){//1st False 2nd true part
php code}
else{
// false part PhP code}
if ($num1 > $num2)
{ $max = $num1;}
else if ($num2 > $num3){
$max = $num3;}
else$max = $num2;
Syntax
E.g.
Conditional Statements(Branching)
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
41/104
3. Switch Statement replacement of if else if stmt.
switch (expression){
case value1: {stmts1;break;}case value2: {stmts2;
break;}
[default: stmts;]}
switch ($day){case 1: {echo its Sunday;break;}case 2: {echo its Monday;break;}case 7: {echo its Saturday;
break;}default: {echo not specifiedvalue;}}
SyntaxE.g.
Conditional Statements(Branching)
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
42/104
Open Source Programming
4. For Statement Looping Statement.
for( initial_expression;termination_check;index_updation)
{statement (S);
}
for($i=1; $i < 10; $i++)echo $i;
(or)$i=1;for(;$i
8/3/2019 OSP Notes1
43/104
5. Do While Statement
do{
Statement (S);}while ();
do{
$i=$i+1;echo $i;
} while ($i < 10);
Syntax E.g.
Conditional Statements(Looping)
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
44/104
5. While Statement
while(){
Statement (S);}
while ($i < 10){
$i=$i+1;echo $i;
}
Syntax E.g.
Conditional Statements(Looping)
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
45/104
Arrays
In Php, arrays are lists of bits of informationmapped with keys and stored under one variable name.For example, you can store a persons name and
address or a list of states in one variable.
Open Source Programming
Generally, array is a collection of homogeneouselements.
Lets store a persons name and age under one variable name :
$name = array(firstname=>Albert,lastname=>Einstein,age=>124);
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
46/104
Open Source Programming
Arraywit
h
Implicitk
ey
Arraywith
Explicitkey
Arrays
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
47/104
Multidimensional Arrays
Open Source Programming
Array of arrays is called as multidimensionalarrays.
$Student = array ("0"=> array ("name"=>"James", "sex"=>"Male", "age"=>"28"),"1"=> array ("name"=>"John", "sex"=>"Male", "age"=>"25"),"2"=> array ("name"=>"Susan", "sex"=>"Female", "age"=>"24"));
$student[1"][name"] returns John
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
48/104
Functions:
A function is a block of code that is not immediately
executed but can be called by scripts whenever needs.
Functions can be built-in or user-defined. They can require
information to be passed to them and usually return a value.
Functions
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
49/104
Open Source Programming
Array Functions
Array ( [a] => Dog [b] => Cat [c] => Horse )
Array(key=>value) Creates an array with keys andvalues
Output:
E.g.:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
50/104
Open Source Programming
Array Functions
array_chunk(array,size,preserve_key ) - splits an arrayinto chunks of new arrays
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
51/104
Array ([0] => Array ( [0] => Cat [1] => Dog )[1] => Array ( [0] => Horse [1] => Cow ))
Output:
E.g.: 1
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
52/104
Array ([0] => Array ( [a] => Cat [b] => Dog )[1] => Array ( [c] => Horse [d] => Cow )
)
Output:
E.g.: 2
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
53/104
array_combine(array1,array2) creates an array by combiningtwo other arrays, where the first array is the keys, and the other arrayis the values.
Open Source Programming
Array Functions
Array ( [a] => Cat [b] => Dog [c]=> Horse [d] => Cow )
Output:
E.g.:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
54/104
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:
Array ( [Cat] => 1 [Dog] => 2[Horse] => 1 )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
55/104
array_diff(arr1,arr2,) function compares two ormore arrays, and returns an array with the keysand values from the first array, only if the valueis not present in any of the other arrays.
E.g:
Array ( [0] => Cat )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
56/104
The array_diff_assoc(arr1, arr2,) function
compares two or more arrays, and returns anarray with the keys and values from the firstarray, only if they are not present in any of theother arrays.
E.g:
Array ( [0] => Cat [2] =>Horse )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
57/104
array_fill(start,number,value ) function returns anarray filled with the values you describe
E.g:
Array ( [2] => Dog [3] =>Dog [4] => Dog )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
58/104
The array_flip(array ) function returns an arraywith all the original keys as values, and all originalvalues as keys.
E.g:
Array ( [Dog] => 0 [Cat] => 1[Horse] => 2 )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
59/104
The array_key_exists(key,arr ) function checksan array for a specified key, and returns true ifthe key exists and false is the key does notexist.
E.g:
Key exists!
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
60/104
The array_merge(array1,array2,array3...)function merges one ore more arrays into onearray.
E.g:
Array ( [a] => Horse [b] =>Cat [c] => Cow )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
61/104
E.g:
Array ( [0] => Horse [1] => Dog )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
62/104
array_multisort(array1, sortingorder, sortingtype, array2, array3 ) returns a sorted array. Youcan assign one or more arrays.
8/3/2019 OSP Notes1
63/104
E.g:
Array ( [0] => Cat [1] => Dog [2] => Dog )Array ( [0] => Missy [1] => Pluto [2] => Fido )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
64/104
The array_pop(array) function deletes the last element ofan array. Array_shift(array) removes an element at thebeginning of the array
E.g:
Array ( [0] => Dog [1] => Cat )Array ( [0] => Cat )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
65/104
The array_push(array,value1,value2...) function insertsone or more elements to the end of an array.
Array_unshift(array) inserts an element at the beginningof the array
E.g:
Array ( [0] => Dog [1] => Cat [2] => Horse [3] => Bird)
Array ( [0] => fish [1]=>Dog [2] => Cat [3] => Horse[4] => Bird )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
66/104
E.g:
Array ( [a] => Dog [b] => Cat [0]=> Horse [1] => Bird )
Output:
Output ????????
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
67/104
array_rand(array,num) function returns a random key froman array, or it returns an array of random keys if you specify thatthe function should return more than one key.
E.g:
b
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
68/104
The array_reverse(array,preserve) function returns anarray in the reverse order.
E.g:
Array ( [c] => Horse [b] => Cat[a] => Dog )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
69/104
The array_search(value,array,strict ) function search anarray for a value and returns the key.
E.g:
a
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
70/104
The array_slice(array, start, length, preserve) functionreturns selected parts of an array.
E.g:
Array ( [0] => Cat [1] => Horse )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
71/104
The array_splice(array,start,length,array) functionremoves selected elements from an array and replaces it withnew elements. The function also returns an array with theremoved elements.
E.g:
Array ( [0] => Tiger [1] => Lion[2] => Horse [3] => Bird )
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
72/104
The array_sum(array) function returns the sum of all thevalues in the array.
8/3/2019 OSP Notes1
73/104
The arsort(array,sorttype) function sorts an array by thevalues in reverse order. The values keep their original keys.
E.g:
Array([c] => Horse[a] => Dog[b] => Cat)
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
74/104
The asort(array,sorttype) function sorts an array by thevalues. The values keep their original keys
E.g:Array([b] => Cat[a] => Dog[c] => Horse
)
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
75/104
The krsort(array,sorttype) function sorts an array by thekeys in reverse order. The values keep their original keys.
E.g:Array([c] => Horse[b] => Cat[a] => Dog
)
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
76/104
The ksort(array,sorttype) function sorts an array by thekeys. The values keep their original keys.
E.g:Array([a] => Dog[b] => Cat[c] => Horse
)
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
77/104
The rsort(array,sorttype) function sorts an array bythe values in reverse order. This function assigns new keys forthe elements in the array. Existing keys will be removed. Thisfunction returns TRUE on success, or FALSE on failure.
E.g:Array([0] => Horse[1] => Dog[2] => Cat
)
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
78/104
The sort(array,sorttype) function sorts an array by thevalues. This function assigns new keys for the elements in thearray. Existing keys will be removed. This function returns TRUEon success, or FALSE on failure
E.g:
Array([0] => Cat[1] => Dog
[2] => Horse)
Output:
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
79/104
Other Array Functions:
Array array range(low,high,step) Populates the array from lowvalue to high value with the interval of step.$a = array(1,10,1) // 1,2,310$odd = array(1,10,2) //1,3,5,,,9
boolean is_array(variable name) Used to check for an arrayArray Array_keys(arrayname) retrieves keys from the array.Array array_values(array) retrieves valuesInt count(arrayname[,mode]) counts values in an array. Mode 1counts recursively (ie for 2dim array)
Sizeof
Open Source Programming
Array Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
80/104
Asort Ascending-values & keeps keys
Arsort - Descending-values & keeps keys
Ksort - Ascending-keys & keeps keys
Krsort - Descending-keys & keeps keys
Sort - Ascending-values & new keys
Rsort Descending values & new keys
Sorttype?
8/3/2019 OSP Notes1
81/104
COUNT_RECURSIVE
COUNT_NORMAL
SORT_LOCALE_STRING
SORT_STRING
SORT_NUMERIC
SORT_REGULAR
SORT_DESC
SORT_ASC
CASE_UPPER
CASE_LOWER
Open Source Programming
Array Constants
EXTR_REFS
EXTR_IF_EXISTS
EXTR_PREFIX_IF_EXISTS
EXTR_PREFIX_INVALID
EXTR_PREFIX_ALL
EXTR_PREFIX_SAME
EXTR_SKIP
EXTR_OVERWRITE
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
82/104
The count_chars(string,mode ) function returns how manytimes an ASCII character occurs within a string and returns theinformation.
Open Source Programming
String Functions
Array
([32] => 1[33] => 1[72] => 1[87] => 1[100] => 1[101] => 1
[108] => 3[111] => 2[114] => 1)
Output:
E.g:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
83/104
The different return modes are:
0 - an array with the ASCII value as key and number ofoccurrences as value
1 - an array with the ASCII value as key and number ofoccurrences as value, only lists occurrences greater than zero
2 - an array with the ASCII value as key and number ofoccurrences as value, only lists occurrences equal to zero arelisted
3 - a string with all the different characters used
4 - a string with all the unused characters
Open Source Programming
String Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
84/104
The explode(separator, string, limit) function breaks astring into an array.
Open Source Programming
String Functions
8/3/2019 OSP Notes1
85/104
The implode(separator,array ) &join(separator,array )function returns a string from the elements of an array.
Open Source Programming
String Functions
E.g:
Hello World! Beautiful Day!
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
86/104
The ltrim(string,charlist) & rtrim(string,charlist) function will
remove whitespaces or other predefined character from the left andright side of a string respectively.
The soundex(string) function calculates the soundex key of astring.
The str_shuffle(string) function randomly shuffles all thecharacters of a string.
Open Source Programming
String Functions
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
87/104
The strlen(string) function returns the length of a string.
Open Source Programming
String Functions
E.g:
Length = 12
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
88/104
The strrev(string) reverses the given string.
Open Source Programming
String Functions
E.g:
Reverse of hello World! is!dlroW olleh
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
89/104
The strtoupper(string) converts to upper case characterThe strtolower(string) converts to lower case character
Open Source Programming
String Functions
E.g:
Upper of hello World! isHELLO WORLD!Lower of hello World! is helloworld
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
90/104
The strpos(string,exp) returns the numerical position of
first appearance of exp.The strrpos(string,exp) returns the numerical position oflast appearance of exp.
Open Source Programming
String Functions
E.g:
210
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
91/104
The substr(string,start,length) function returns a sub stringof the size length from the position of start.
Open Source Programming
String Functions
E.g:
lo
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
92/104
The substr_count(string,substr) counts number of times asub string occurred in given string.
Open Source Programming
String Functions
E.g:
2
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
93/104
The substr_replace(string,replacement,start,len)replaces a portion of a string with a replacement string, beginningthe substitution at a specified starting position and ending at apredefined replacement length.
Open Source Programming
String Functions
E.g:
heaaorld!
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
94/104
The ucfirst(string) converts the first character to upper case.The ucwords(string) converts the first character of each wordto upper case.
Open Source Programming
String Functions
E.g:
Hello world!Hello World!
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
95/104
The parse_str(string,arr) function parses a query string intovariables.
Open Source Programming
String Functions
E.g:
23Kai Jim
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
96/104
The str_replace(find,replace,string,count ) functionreplaces some characters with some other characters in a string.
Note: str_ireplace() for case sensitive
Open Source Programming
String Functions
E.g:Array([0] => blue[1] => pink
[2] => green[3] => yellow)Replacements: 1
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
97/104
The str_pad(string,length,padchar,padtype) functionpads a string to a new length.
Open Source Programming
String Functions
E.g:
.........Hello World
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
98/104
The str_split(string,length ) function splits a string into an array.Default length is 1.
Open Source Programming
String Functions
E.g:
Array(
[0] => Hel[1] => lo)
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
99/104
The str_word_count(string) function counts the number of wordsin a string.
Open Source Programming
String Functions
E.g:
2
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
String Functions
8/3/2019 OSP Notes1
100/104
The strcasecmp(string1,string2) function compares two strings ascase 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
Open Source Programming
E.g:
01
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
8/3/2019 OSP Notes1
101/104
String Functions
8/3/2019 OSP Notes1
102/104
The stristr(string,exp) function searches for the first occurrence ofa string inside another string.This function returns the rest of the string (from the matching point), orFALSE, if the string to search for is not found
Open Source Programming
E.g:
World! Have a niceday
Output:
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Date Functions
8/3/2019 OSP Notes1
103/104
Boolean Checkdate(month,day,year) used to check the givenparameters are a valid date.
Date(formatstring) - The date() function returns a stringrepresentation of the current date and/or time formatted according tothe instructions specified by a predefined format.
Array Getdate(int timestamp) returns array of datetimecomponents
Time() returns time stamp
Mktime(int h,int min, int sec, int day, int mnth, int yr) create a date and time
Open Source ProgrammingAuthor: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Math Functions
8/3/2019 OSP Notes1
104/104
Some mathematical functions are listed:
1. sin(X) , cos(x), tan(x), asin(x), acos(x), atan(x), exp(x), log(x) are trigonometric functions returns the respected values.
2. Ceil(x), floor(x) used to round the numbers.
3. Abs(x) returns absolute value for the given number.
4. Fmod(x,y) returns the reminder of the division x /y.
5. Min(x,y) & Max(x,y) returns the min and max values respectively.
6. Bindec(x), binoct(x),binhex(x) converts the given binary value intodecimal, octal and hexadecimal.
7. decbin(x), decoct(x), dechex(x) converts the given decimal valueinto binary, octal and hexadecimal.
8. Base_convert(val,bastype, convtype) converts the given value ofbasetype has to be converted to the convtype.