Php2

1,557 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,557
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
46
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php2

  1. 1. <ul><li><?php print &quot;Hello World!&quot;; </li></ul><ul><li>?> </li></ul><ul><li>In this script PHP tags are used to separate the actual PHP content from the rest of the file. </li></ul><ul><li>You can inform the interpreter that you want it to execute your commands by adding a pair of such tags: </li></ul><ul><ul><li>standard tags “<?php ?>”; </li></ul></ul><ul><ul><li>short tags “<? ?>”; </li></ul></ul><ul><ul><li>ASP tags “<% %>”; </li></ul></ul><ul><ul><li>script tags “<SCRIPT LANGUAGE=”php”> </SCRIPT>”. </li></ul></ul><ul><ul><li>The standard and the script tags are guaranteed to work under any configuration, the other two need to be enabled in your “php.ini” </li></ul></ul>
  2. 2. print and echo <ul><li>Both are used to print data on screen. </li></ul><ul><li>Difference between print and echo is that print returns value indicating success or failure, whereas echo doesn’t return any such value. </li></ul><ul><li>echo() can take multiple expressions. Print cannot take multiple expressions. echo &quot;The first&quot;, &quot;the second&quot;; </li></ul><ul><li>echo has the slight performance advantage because it doesn't have a return value. </li></ul>
  3. 3. Terminating Execution <ul><li>exit() and die() are used to terminate script execution. </li></ul><ul><li>exit() takes either string or number as an argument, prints that argument and then terminates execution of script. </li></ul><ul><li>The die() function is an alias for exit() . </li></ul><ul><li>$filename = '/path/prog1.php'; </li></ul><ul><li>$file = fopen($filename, 'r') or exit(&quot;unable to open file ($filename)&quot;); </li></ul><ul><li>$connection=mysql_connect(“192.168.0.1”,”user”,”pass”) ; </li></ul><ul><li>if ( ! $connection ) die (“Connection not established.”); </li></ul>
  4. 4. functions <ul><li>Syntax: </li></ul><ul><li>function function_name() </li></ul><ul><li>{ </li></ul><ul><ul><li>/* </li></ul></ul><ul><ul><ul><li>function statements </li></ul></ul></ul><ul><ul><li>*/ </li></ul></ul><ul><ul><li>return result; </li></ul></ul><ul><li>} </li></ul><ul><li>Variables defined in a function are local by default. </li></ul><ul><li>To access any variable of function out of that function, use global variables. </li></ul><ul><li>function sum($a,$b) </li></ul><ul><li>{ </li></ul><ul><li>global $c; </li></ul><ul><li>$c=$a+$b; </li></ul><ul><li>} </li></ul><ul><li>$c=0; </li></ul><ul><li>sum ( 5 , 1 ); </li></ul><ul><li>print $c; </li></ul><ul><li>o/p - > 6 </li></ul>
  5. 5. <ul><li>Static Variables </li></ul><ul><li>If you don't want  to alter value of a function’s variable outside your function, and you still want to retain your variable, you can use the static variable. </li></ul><ul><li>A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. </li></ul><ul><li>function sum($a,$b) </li></ul><ul><li>{ </li></ul><ul><li>static $c=0; </li></ul><ul><li>$c=$a+$b; </li></ul><ul><li>print “ Value of $c in function is $c ”; </li></ul><ul><li>} </li></ul><ul><li>$c=3; </li></ul><ul><li>sum ( 5 , 1 ); </li></ul><ul><li>print “ Value of $c outside the function is $c ”; </li></ul><ul><li>o/p </li></ul><ul><li>Value of $c in function is 6 </li></ul><ul><li>Value of $c in outside the function is 3 </li></ul>
  6. 6. Arrays <ul><li>PHP arrays are associative arrays because they associates keys with values. </li></ul><ul><li>You can use it either as a simple c like array or as an associative array. </li></ul><ul><li>It is similar to perl’s hash. </li></ul><ul><li>Here array indices are enclosed into [] rather than {}. </li></ul><ul><li>Rather than having a fixed number of slots, php creates array slots as new elements are added to the array. </li></ul><ul><li>You can assign any type for keys and values .such as string, float ,integer etc. </li></ul>
  7. 7. <ul><li>Syntax to create an array: </li></ul><ul><li>For simple array: </li></ul><ul><li>$arr=array(“ele1”,”ele2”,”ele3”); </li></ul><ul><li>OR </li></ul><ul><li>$arr[0]=“ele1”; </li></ul><ul><li>$arr[1]=“ele2”; </li></ul><ul><li>$arr[2]=“ele3”; </li></ul><ul><li>OR </li></ul><ul><li>$arr[]=“ele1”; </li></ul><ul><li>$arr[]=“ele2”; </li></ul><ul><li>$arr[]=“ele3”; </li></ul><ul><li>OR </li></ul><ul><li>$arr=( 0 => “ele1” , 1=> “ele2” , 2 => “ele3” ); </li></ul><ul><li>For associative array : </li></ul><ul><li>$arr[“key1”]=“val1”; </li></ul><ul><li>$arr[“key2”]=“val2”; </li></ul><ul><li>$arr[4]=“val3”; </li></ul><ul><li>OR </li></ul><ul><li>$arr=(“key1”=>”val1” , “key2”=>”val2” , 4 => “val3” ); </li></ul>
  8. 8. <ul><li>To create empty array , </li></ul><ul><li>$arr=array(); </li></ul><ul><li>After creating array like this, you can add elements using any of the above methods. </li></ul><ul><li>You can print the array with print. </li></ul><ul><li>print $arr; </li></ul><ul><li>To retrieve array element: </li></ul><ul><li>$val=$arr[0]; </li></ul><ul><li>OR </li></ul><ul><li>$val=$arr[“key1”]; </li></ul><ul><li>OR You can assign your array values to list of scalars. </li></ul><ul><li>list($val1,$val2,$val3)=$arr; </li></ul><ul><li>List is reverse of array because array packages its arguments into array and list takes array and assign its values to list of individual variables. </li></ul>
  9. 9. Array Functions <ul><li>is_array() </li></ul><ul><li>syntax : [true/false] = is_array(array ); </li></ul><ul><li>If variable is of type array, then is_array function will return true otherwise false. </li></ul><ul><li>count() </li></ul><ul><li>syntax: [no. of eles.] = count ( array ); </li></ul><ul><li>It returns number of elements in the given array. </li></ul><ul><li>in_array() </li></ul><ul><li>syntax: [true/false] = in_array( array , value ) ; </li></ul><ul><li>It checks if value exists in given array or not. </li></ul><ul><li>Isset ( $arr[$key] ) . Returns true if key $key exists in array. </li></ul>
  10. 10. <ul><li>Functions to traverse through an array: </li></ul><ul><li>current() function returns stored value that the current pointer points to. </li></ul><ul><li>Initially, current() will point to the first element in the array. </li></ul><ul><li>next(array) function returns the current value and then advances array pointer. Returns false if no next element is available. </li></ul><ul><li>reset() function sets the pointer to the first element & returns the stored value. </li></ul><ul><li>prev() sets the pointer to the next element. </li></ul><ul><li>end() sets the pointer to the last element. </li></ul><ul><li>key() returns key of current element. </li></ul><ul><li>each() returns the current key and value pair from an array and advances the array pointer. </li></ul>
  11. 11. <ul><li>e.g. </li></ul><ul><li>$transport = array(‘bus', 'bike', 'car', 'plane'); </li></ul><ul><li>$mode = current($transport); // $mode = ‘bus'; </li></ul><ul><li>$mode = next($transport);    // $mode = 'bike'; </li></ul><ul><li>$mode = current($transport); // $mode = 'bike'; </li></ul><ul><li>$mode = prev($transport);    // $mode = ‘bus'; </li></ul><ul><li>$mode = end($transport);     // $mode = 'plane'; </li></ul><ul><li>$mode = reset($transport); // $mode = 'plane'; </li></ul><ul><li>$mode = key($transport); // $mode = 3 ; </li></ul><ul><li>$array_cell=each($transport); </li></ul><ul><li>// $array_cell[‘key’] will be 3 and </li></ul><ul><li>// $array_cell[‘value’] will be plane </li></ul>
  12. 12. <ul><li>Traversing an array with while loop . </li></ul><ul><li>$arr = array(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;); reset ($arr); while ( list( , $value) = each ($arr)) {     echo &quot;Value: $value<br> &quot;; } </li></ul><ul><li>reset ($arr); </li></ul><ul><li>foreach ($arr as $value) {     echo &quot;Value: $value<br> &quot;; } </li></ul><ul><li>For both loops , o/p </li></ul><ul><li>Value: one </li></ul><ul><li>Value: two </li></ul><ul><li>Value: three </li></ul>
  13. 13. <ul><li>Traversing an associative array with loop . </li></ul><ul><li>$a = array (     &quot;one&quot; => 1,     &quot;two&quot; => 2,     &quot;three&quot; => 3,     &quot;seventeen&quot; => 17 ); while ( list( $key , $value) = each ($a)) { </li></ul><ul><li>     echo “$key => $value<br> &quot;; } </li></ul><ul><li>reset($a); </li></ul><ul><li>foreach ($a as $key => $value ) </li></ul><ul><li> {     print &quot;$a[$key] => $value. &quot;; } </li></ul><ul><li>o/p </li></ul><ul><li>one => 1 </li></ul><ul><li>two => 2 </li></ul><ul><li>three => 3 </li></ul>
  14. 14. <ul><li>array_keys() </li></ul><ul><li>array array_keys ( array input [, mixed search_value]) array_keys() returns the keys from the input array. </li></ul><ul><li>If the optional search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the input are returned. </li></ul><ul><li>array_values () </li></ul><ul><li>array array_values ( array input) array_values() returns all the values from the input array and indexes the array numerically. </li></ul><ul><li>array_count_values () </li></ul><ul><li>$array = array(1, &quot;hello&quot;, 1, &quot;world&quot;, &quot;hello&quot;); </li></ul><ul><li>print_r(array_count_values($array)); </li></ul><ul><li>Returns an array using the values of the input array as keys and their frequency as values. </li></ul><ul><li>o/p </li></ul><ul><li>Array ( </li></ul><ul><li> [1] => 2 , </li></ul><ul><li>[hello] => 2 , </li></ul><ul><li>[world] => 1 ) </li></ul>
  15. 15. <ul><li>array_flip () </li></ul><ul><li>array array_flip ( array trans) Exchanges all keys with their associated values in an array </li></ul><ul><li>If a value has several occurrences, the latest key will be used as its values, and all others will be lost. </li></ul><ul><li>array_flip() returns FALSE if it fails. </li></ul><ul><li>$trans = array(&quot;a&quot; => 1, &quot;b&quot; => 1, &quot;c&quot; => 2); </li></ul><ul><li>$trans = array_flip($trans); </li></ul><ul><li>o/p -> 1=>b , 2=>c </li></ul><ul><li>array_reverse () </li></ul><ul><li>array array_reverse ( array array [, bool preserve_keys]) </li></ul><ul><li>array_reverse() takes input array and returns a new array with the order of the elements reversed, preserving the keys if preserve_keys is TRUE . </li></ul><ul><li>array_merge () </li></ul><ul><li>array array_merge ( array array1, array array2 [, array ...]) </li></ul><ul><li>It merges two or more arrays. </li></ul><ul><li>$arr1= (“a”=>1,”b”=>2); </li></ul><ul><li>$arr2= (“C”=>3, “D”=>4); </li></ul><ul><li>$arr_result=array_merge($arr1,$arr2); </li></ul><ul><li>OR </li></ul><ul><li>$arr_result= $arr1 + $arr2 ; </li></ul>
  16. 16. <ul><li>array_slice() Extract a slice of the array . </li></ul><ul><li>array array_slice ( array array, int offset [, int length]) </li></ul><ul><li>Returns the sequence of elements from the array as specified by the offset and length parameters. </li></ul><ul><li>If offset is positive, the sequence will start at that offset in the array . If offset is negative, the sequence will start from the end of the array . </li></ul><ul><li>If length is given and is positive, then the sequence will have that many elements in it. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array . </li></ul><ul><li>$input = array(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;); $output = array_slice($input, 2);      // returns &quot;c&quot;, &quot;d&quot;, and &quot;e&quot; $output = array_slice($input, 2, -1);  // returns &quot;c&quot;, &quot;d&quot; $output = array_slice($input, -2, 1);  // returns &quot;d&quot; $output = array_slice($input, 0, 3);   // returns &quot;a&quot;, &quot;b&quot;, and &quot;c&quot; </li></ul>
  17. 17. <ul><li>array_splice() Remove a portion of the array and replace it with other elements . </li></ul><ul><li>array array_splice ( array input, int offset [, int length [, array replacement]]) array_splice() removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied. It returns an array containing the extracted elements. </li></ul><ul><li>$input = array(&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;yellow&quot;); array_splice($input, 2); // $input is now array(&quot;red&quot;, &quot;green&quot;) </li></ul>

×