Title: Web Application Development
1Web Application Development
- Seminar 2 Part I
- CMWA 6103
- Jaspal Kaur
2Arrays
- An array is a variable that stores a set or
sequence of values. - One array can have many elements, each element
can hold a single value such as text or numbers
or another array.
3Initializing Arrays
- Example of array
- products array(Tires,Oil,Spark Plugs)
- numbers range(1,10)
- odds range(1,10,2)
- letters range(a, z)
4Accessing Array Contents
- To access the contents of a variable, you use its
name. The key or index indicates which of the
values in the array you access. - products0, products1, products2
- Add a new element products3 Fuses
- Displaying the contents echo products0
products1 products2 products3 - Using loops to display array contents
- for (i 0 ilt3 i)
- echo productsi
5Arrays with different Indices
- Initializing an array
- prices array (Tiresgt100, Oilgt10, Spark
Plugsgt 4) - To access the array elements
- foreach(prices as key gt values)
- echo key.gt.value.ltbr /gt
6Multidimensional Arrays
- Two-D array matrix, grid with width and heigth
or row and cols. - Example
- products array (array (TIR, Tires,100),
array (OIL, Oil, 10), - array (SPK,
Spark Plugs,4) )
7Accessing Multidimensional Arrays
- for (row 0 row lt 3 row)
-
- for (column 0 column lt 3 column)
-
- echo .productsrowcolumn
-
- echo ltbr /gt
8Sorting Arrays
- Using sort()- sort into alphabetical order
- products array (Tires, Oil, Spark
Plugs) - sort (products)
- Now the order will be Oil, Spark Plugs, Tires
- Sort() is case sensitive, Capital letters before
lowercase letters, - Sorting in reverse rsort().
9Reordering arrays
- shuffle()-randomly reorders the elements in the
array. - array_reverse() creates a new array in the
reverse order of the original. - Navigating within an array each(), current(),
reset(), end(), next(), pos(), and prev(). - Counting elements in an array count(),
sizeof(), array_count_values().
10String Manipulations
- Formatting Strings
- Presentation
- Joining and Spliting Strings
- Comparing Strings
11Formatting Strings
- Trimming Strings chop(), ltrim(), and trim()
- Example
- name trim (name)
- email trim (email)
- feedback trim (feedback)
12Formatting Strings for Printing
- print() and echo() prints the string as it is.
- printf() and sprintf() prints and returns a
formatted string. - Example
- echo Total amount of order is total.
- printf(Total amount of order is s., total)
13Changing the Case of a String
- strtoupper() turns string to uppercase
- strtolower() turns string to lowercase
- ucfirst() Capitalizes first character of string
if its alphabetic - ucwords() Capitalizes first character of each
word in the string that begins with an alphabetic
character
14Joining and Spliting Strings
- explode() splits a string based on a specified
separator string as an array - implode(), join() reverse of explode()
- strtok() gets pieces (tokens) from string one
at a time - substr() access a substring between given start
and end points of the string
15Comparing Strings
- strcmp() expects two strings and compares them,
if they are equal, returns 0, otherwise if str1
is greater than str2, returns number greater than
0. - strcasecmp() is not case sensitive
- strnatcmp() compares strings according to
natural ordering (2 before 12).
16Object oriented PHP
- Classes and Objects
- object is any item or concept
- o-o software designed and built as a set of
self-contained objects with attributes and
operations that interact - Attributes properties or variables
- Operations methods, actions or functions
17Object-oriented Concepts
- Encapsulation
- Polymorphism
- Inheritance
18Creating classes, atributes, operations in PHP
- Creating a class in PHP
- class classname
-
-
- Attributes and operations
- class classname
-
- var attribute1
- var attribute2
- function operation1()
-
19Constructors Destructors
- Constructors called when an object is created,
used for initialisation - _construct()
- Can be called manually or explicitly
- Can be overloaded
- Destructors executed automatically just before
class is destroyed, or when all references to a
class have fallen out of scope. - _destruct()
- Cannot take parameters (overloaded)
20Instantiating Classes
- to create an object, new keyword is used
- Example
- class classname
-
- function _construct(param)
-
- echo Constructor called with
parameter param ltbr /gt -
-
- a new classname (First)
- b new classname (Second)
- c new classname ()
21Using Attributes Operations
- special pointer this
- accessing attributes a-gtattribute
- _get() and _set() to return and set attributes
- a-gtattribute 5 uses _set()
- a-gtattribute uses _get()
- a-gtoperation1()
- y a-gtoperation2(12, test)
22Public and Private
- Access modifiers control visibility of attributes
and methods - public (default) accessed in and out of class
- private only accessed from within
- protected accessed from within and subclasses
23Implementing Inheritance
- class B extends A
-
- var attribute2
- function operation2()
-
-
- function operation1()
- different codes .
-
- class A
-
- var attribute1
- final function operation1()
-
-
b new B() b-gtoperation1() b-gtattribute1
10 b-gtoperation2() b-gtattribute2 10 a
new A() a-gtoperation1() a-gtattribute1
10 a-gtoperation2() a-gtattribute2 10
24Overriding
- subclass may redeclare the same attributes and
operations - different default value or different
functionality of operations from the superclass - inheritance and overriding can be prevented using
final keyword
25Interfaces
- uses multiple inheritance concept
- specifies a set of functions that must be
implemented by the class - only prototypes operations are listed in the
interface - classes can only extend one superclass but can
implement many interfaces
26End of Part I