We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6
Unit - II
2. Programming with PHP
2.1 Conditional statements: if, if-else, switch, The ? Operator 2.2 Looping statements: while Loop, do...while Loop, for Loop 2.3 Arrays in PHP: Introduction- What is Array? 2.4 Types of Arrays: Indexed Vs. Associative arrays, Multidimensional arrays 2.5 Creating Array, Accessing Array, Manipulating Arrays, Displaying arrays…..use of for.. each as loop 2.6 Using Array Functions 2.7 Including and Requiring Files- use of Include() and Require() 2.8 Implicit and Explicit Casting in PHP - In Php, array can contain either same or different types of data elements. 2.1 Conditional statements: if, if-else, - Array in Php can be following types: switch, The ? Operator [Repeat theory from C/C++/Java notes] – 1) Indexed Array theory explanation, flow chart & syntax. Eg. 2) Associative Array Given in classroom 3) Multi-Dimensional Array 1) Indexed Array – In this type, each 2.2 Looping statements: while Loop, element of array is stored and processed do...while Loop, for Loop using an index. [Repeat theory from C/C++/Java notes] – - Index is predefined and it is as per theory explanation, flow chart & syntax. Eg. following rules: Given in classroom i) index starts with zero i.e. Lower 2.3 Arrays in PHP: Introduction- What is Bound Array? ii) Index of Last element is size -1 i.e. 2.4 Types of Arrays: Indexed Vs. Uppper Bound. Associative arrays, Multidimensional iii) Index always is positive number in arrays increasing order. 2.5 Creating Array, Accessing Array, - Such index array can be created in Manipulating Arrays, Displaying following different ways: arrays…..use of for.. each as loop 1) Using [ ] brackets – this syntax is similar to C like language by which array elements - Programming code needs data elements for can be directly initialized as: it’s successful execution. $AryName=[value list]; - to store any dta element, generally variable e.g. is used which is very easy to create and use. <?php - But the limitation is, one variable at a time $n={10,20,30}; // array with all same type elements. can hold only single value. But in many var_dump($n); cases, progr needs set of data values or elements. $n={10,2.99,"abc"}; // array with difft. Type elements. - For this, array concept is used. var_dump($n); - Array is linear data structure which stores ?> set of data elements. Output- array(3) { [0]=> int(10) [1]=> int(20) [2]=> int(30) } array(3) { [0]=> int(10) [1]=> float(2.99) [2]=> string(3) "abc" } 2) Using array( ) – PhP supports array( ) as a built-in function using which array can be - The key used in associative array shows create and initialized in memory. following features: - This array( ) can be used as: i. key can be integer, float, char or in string $AryName=array(“ list of data items”); e.g. $studcode=array(1001,1002,1003); form. $studName=array(“Peter”,”David”,”Henry”); ii. key can be assigned in random. $studData=array(1001,”David”,67.88); - In this case also, array indexing is predefined iii. If duplicate key is assigned, the value starting with zero as lower bound to upper bound. assigned to previous key will be replaced by e.g. new key as: <?php $data=array(1001,"David",67.88); $data=[99=>1001,2.3=>"David",99=>67.88]; echo "Stud Code=".$data[0]; In above e.g. 99 key first assigned to value 1001 and echo "<br>Stud Code=".$data[1]; re-assigned to 67.99 hence previous value 1001 will echo "<br>Stud Per Marks=".$data[2]; be replaced by 67.88. ?> output:- 3) Multi-dimensional array - In Php, a single array is collection of data elements of same or different types. 2) Associative Array – - In Index array, the array elements are stored and - PhP also allows to create collection of array accessed by index values which are always pre- i.e. Multi-dimensional array. defined such as it starts with zero till size-1. - Such an array cannot be considered secure as the - In other words, a multi-dimensional array is elements can be easily accessible using this collection of arrays or arrays with-in array. predefined index number. - In some cases, the array elements needs more - Thus such array is organized as matrix or security and only the authorized user who have the table in memory using row as well as column key can access it. index. - For such purpose, associative array can be used. - The array in which each data element associated to - Such an array can be created as: user defined key and accessed by user defined key is $AryName=array called Associative Array. - An associative array is also said to be collection of ( key-value pairs. array(value list..), - In such array, the data element and it’s key can be array(value list..), associated using special operator i.e. => as: array(value list..) $AryName=[Key1=>value1,key2=>value2……]; ) e.g. <?php e.g. $data=[99=>1001,2.3=>"David","abc"=>67.88]; <?php echo "Stud Code=".$data[99]; <?php echo "Stud Name=".$data[2.3]; echo "Stud Per Marks=".$data[67.88]; $data=array( ?> array(10,20,30),array(100,200,300),array(60, Output 70,80)); for($i=0;$i<3;$i++) { for($k=0;$k<3;$k++) { 100 20 3 40 15 echo "Data[$i][$k]=".$data[$i][$k]."<br>"; 2.6 Using Array Functions } echo "<hr>"; Array in Php is collection of data } elements stored in memory with index ?> and using this index can be processed. Output:- For performing different operations on array, Php supports following different lib functions. 1) array ( ) – this function takes set of elements to create array in memory as: $AryName=array(value list); * using for…each loop statement E.g. $n=array(10,20,30,40,50); - As array is collection of same or different 2)count($AryName)- this function returns data elements, it can be processed using for number of elements present in given array. loop statement easily. E.g - In case, if array is an indexed array, using <?php any loop counter as it’s index it can be easily $n=array(10,20,30,40,50); accessed as: echo “No of Array Elements=”.count($n); for($i=0;$i<5;$i++) ?> echo $AryName[$i]. Output- - This is possible if the array is using predefined index which are sequential. 3) sort( ) – this function arranges all the - But, in case if, associative array is used, in elements of given array in ascending order. which index is neither pre-defined nor E.g <?php sequential, in such case simple for statement $n=array(100,20,3,40,15); will not be useful. sort($n); - In such case, Php supports a special loop foreach($n as $t) statement i.e. for…each. echo $t." "; ?> - This loop statements iterates the array Output- element from first element to last element as: 4) rsort( ) – this function arranges all the foreach($AryName as $Tempname) elements of given array in reverse or descending order. E.g - Thus, foreach extracts one element at a time <?php and stores it in temp variable for processing. $n=array(100,20,3,40,15); This loop repeated till the last element. rsort($n); e.g. foreach($n as $t) echo $t." "; <?php ?> $n=array(100,20,3,40,15); Output- foreach($n as $t) echo $t." "; ?> Output- 5) array_reverse()- this function reverses all - Also, a single website or web application the array elements of given array and returns having multiple web pages needs some a new array . E.g. common code to be called as like libraries in other languages e.g. In \cc or C++ languages, <?php functions defined ed in header files. $n=array(100,20,3,40,15); - In PhP also it is possible to create such a $m=array_reverse($n); code and can be used and reused as like foreach($m as $t) library.. echo $t." "; - Such library code defined once can be ?> called from any other php code or page using Output- include( ) or require( ) function. 6) array_search(Element,AryName) – this - Thus, such php codee which is created once function searches the given element in given and can be used many times becomes server code and other php code using it becomes array, if the present presentsts it returns it’s client page or code. index/key and if not returns null. e.g. <?php $n=array(100,20,3,40,15); $k=array_search(20,$n); echo $k; ?> - To call the code stored in server code from client code, either include( ) or require( ) function can be used as: Output- include(“ServerCodeFileName”); ServerCodeFileName”); 7) array_intersect(Ary1,Ary2,….)-- this function takes two or multiple array require(“ServerCodeFileName”); arguments and creates a new array of only all e.g. matching elements in given arrays. <?php - If no matching elements then empty array function add($x,$y) will be returned. { e.g. echo "Addition=".$x+$y; <?php } $ary1=array(100,20,3,40,33); function subt($x,$y) $ary2=array(10,60,99,88,333,280); { echo "Subtraction=".$x-$y; $ary3=array(array_intersect($ary1,$ary2)); } echo "No of Matching ?> Elements=".count($ary3)."<hr>"; Above code can be saved as foreach($t as $t1) “arithmetic.php”. print_r($t); - Once it is saved, it can be called from any ?> other php code e.g. Output- P1.php P2.php 2.7 Including and Requiring Files- use of <?php <?php include() and require() include require - As PhP is server side scripting language, it ('arithmetic.php'); ('arithmetic rithmetic.php'); allows to such web applications which are echo add(10,20); echo created once and stored on server. echo subt(10,5); add(10.55,20.88); add(10 - Such web application can be called from ?> echo subt(25.55,12.32); any client machine using browser. ?> - Type-casting is programming process by which for temporary purpose data - Thus above two php codes P1.php and P2. type of variable converted to other Php are accessing same functionalities stored type. in “arithmetic.php” file, hence, - By type casting, the conversion is “arithmetic.php” as server code created once done from source type to target type. can be used from any client code like p1.php, - This type casting also called as type p2.php, etc. conversion and it is of following - The include and require both are used for types: same purpose i.e. to access server code from client code, but it shows following 1. Implicit type casting – in this the difference: type casting is done automatically include( ) require( ) without using any special instruction. - In this, when - In this, the server - It is also called as primitive type server code is variable or casting or built-in type casting. called, in it’s own function .e.g memory space and transferred <?php result is returned temporarily to $x=100; to it’s calling php calling code for print(gettype($x). " value in code. calling. variable=$x<br>"); $x=7.89; - If server code file - In this, if file not echo gettype($x)." value in variable=$x<br>"; not found, it’s, it found, it generated $x='C'; only generates fatal error and print(gettype($x)." value in variable=$x<br>"); warning and still executed $x="Abc"; execution terminated. print(gettype($x)." value in variable=$x<br>"); continues for rest $x=true; of the code. print(gettype($x)." value in variable=$x<br>"); ?> - It is generally used - It is used to access to access local file remote files i.e i.e. server code server code and and client code client code on both on same different machines. machine.
2.8 Implicit and Explicit Casting in PHP
- As PhP is loosely typed language which means a variable can be directly used without - In above e.g. data type of x variable pre-declaration with fixed data type. getting converted into other type. - The datatype of variable is dynamically 2) Explicit type casting – in this, the decided as per the value assigned to it e.g. type casting is done by using special <?php instruction as: $x=100; $var=(type)expr; print(gettype($x). " value in variable=$x<br>"); $x=7.89; - Php allows following explicit type echo gettype($x)." value in variable=$x<br>"; ?> casting: - Thus, in above example x variable is (int) or (integer) casts to an integer behaving as an integer as well as float (bool) or (boolean) casts to a boolean type based on the value assigned. (float) or (double) or (real) casts to a - This, practically means, php also float supports concepts of type casting. (string) casts to a string (array) casts to an array (object) casts to an object e.g <?php $a = 9.99; $b = (int)$a; var_dump($b); echo “<hr>”; $x = "295.95 only"; $y = (double)$x; var_dump($y); ?> Output int(9) float(295.95) - Thus in above example a variable type casted float to integer and x variable type casted from string to float type.