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

More Related Content

PPT
Php Reusing Code And Writing Functions
PDF
Perl.predefined.variables
PPT
Class 4 - PHP Arrays
PPT
ODP
Beginning Perl
PPT
Class 5 - PHP Strings
PDF
Improving Dev Assistant
PDF
Sorting arrays in PHP
Php Reusing Code And Writing Functions
Perl.predefined.variables
Class 4 - PHP Arrays
Beginning Perl
Class 5 - PHP Strings
Improving Dev Assistant
Sorting arrays in PHP

What's hot (20)

PPTX
PHP Functions & Arrays
PPTX
Introduction in php
PDF
Perl.Hacks.On.Vim
PPT
Php Using Arrays
ODP
perl usage at database applications
PPTX
Introduction in php part 2
PDF
Swift Programming Language
PPTX
Unit 1-scalar expressions and control structures
PDF
Good Evils In Perl
PPTX
Subroutines in perl
PPTX
Strings,patterns and regular expressions in perl
PDF
Zend Certification Preparation Tutorial
ODP
Programming in perl style
PPTX
Scalar expressions and control structures in perl
PPT
PDF
Wx::Perl::Smart
PPT
Php Chapter 2 3 Training
PDF
Introduction to Swift programming language.
PHP Functions & Arrays
Introduction in php
Perl.Hacks.On.Vim
Php Using Arrays
perl usage at database applications
Introduction in php part 2
Swift Programming Language
Unit 1-scalar expressions and control structures
Good Evils In Perl
Subroutines in perl
Strings,patterns and regular expressions in perl
Zend Certification Preparation Tutorial
Programming in perl style
Scalar expressions and control structures in perl
Wx::Perl::Smart
Php Chapter 2 3 Training
Introduction to Swift programming language.
Ad

Viewers also liked (20)

PPT
Affitta Un Nonno_Lab Design Concept
DOC
J81140 d6 educational curriculum & methods
PDF
PPT
West Africa food crisis
PPSX
Parents as leaders
PDF
C H A P T E R 1 N O T E S
PPT
Student Searches PPT. - Dr. William Allan Kritsonis
PPT
Starchman Coyle What is Project Based Learning
PDF
글로벌 웹사이트 접근성비교 4th 접근성캠프
PPT
11 18 celebration (winter 2011) (nx power-lite)1
PPTX
웹톡스Ex 제안서 web
PPTX
Primary celebration (spring 2011)
PDF
2014 company suzisoft
PDF
ANE parallels to Gen 1-11
KEY
Head lezing 5 juli
ODP
Exercici9pag56
PPT
Picture my World - Promotional slideshow
PPT
Calling All Coaches! Come Learn about the NETS*C
PDF
Encountering NT
PDF
Jesus the Liberator
Affitta Un Nonno_Lab Design Concept
J81140 d6 educational curriculum & methods
West Africa food crisis
Parents as leaders
C H A P T E R 1 N O T E S
Student Searches PPT. - Dr. William Allan Kritsonis
Starchman Coyle What is Project Based Learning
글로벌 웹사이트 접근성비교 4th 접근성캠프
11 18 celebration (winter 2011) (nx power-lite)1
웹톡스Ex 제안서 web
Primary celebration (spring 2011)
2014 company suzisoft
ANE parallels to Gen 1-11
Head lezing 5 juli
Exercici9pag56
Picture my World - Promotional slideshow
Calling All Coaches! Come Learn about the NETS*C
Encountering NT
Jesus the Liberator
Ad

Similar to Php2 (20)

PPT
PHP Workshop Notes
PPTX
Chapter 2 wbp.pptx
PPT
Php Chapter 1 Training
PPT
PPT
Introduction To Lamp
PPT
Basic PHP
DOCX
PHP record- with all programs and output
PPTX
PHP array 1
PPTX
Unit 2-Arrays.pptx
PPT
Php Crash Course
ODP
Php Learning show
PPT
P H P Part I, By Kian
PDF
Php Tutorials for Beginners
PDF
php AND MYSQL _ppt.pdf
PPTX
Php 2
PPT
Introduction To Php For Wit2009
PPTX
UNIT IV (4).pptx
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
PDF
Php tips-and-tricks4128
PPT
Internet Technology and its Applications
PHP Workshop Notes
Chapter 2 wbp.pptx
Php Chapter 1 Training
Introduction To Lamp
Basic PHP
PHP record- with all programs and output
PHP array 1
Unit 2-Arrays.pptx
Php Crash Course
Php Learning show
P H P Part I, By Kian
Php Tutorials for Beginners
php AND MYSQL _ppt.pdf
Php 2
Introduction To Php For Wit2009
UNIT IV (4).pptx
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Php tips-and-tricks4128
Internet Technology and its Applications

More from Keennary Pungyera (6)

DOC
Sap procurement process flow
TXT
DOC
Interest Calculation
PPT
The Simple Life Very Beautiful 19082008
PPT
Fuction call
Sap procurement process flow
Interest Calculation
The Simple Life Very Beautiful 19082008
Fuction call

Recently uploaded (20)

PPTX
How to Manage Global Discount in Odoo 18 POS
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
UTS Health Student Promotional Representative_Position Description.pdf
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
Types of Literary Text: Poetry and Prose
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Sunset Boulevard Student Revision Booklet
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Onica Farming 24rsclub profitable farm business
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
PPTX
Presentation on Janskhiya sthirata kosh.
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PPTX
Strengthening open access through collaboration: building connections with OP...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
How to Manage Global Discount in Odoo 18 POS
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
UTS Health Student Promotional Representative_Position Description.pdf
vedic maths in python:unleasing ancient wisdom with modern code
Open Quiz Monsoon Mind Game Final Set.pptx
Types of Literary Text: Poetry and Prose
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Sunset Boulevard Student Revision Booklet
Software Engineering BSC DS UNIT 1 .pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
Onica Farming 24rsclub profitable farm business
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Presentation on Janskhiya sthirata kosh.
Open Quiz Monsoon Mind Game Prelims.pptx
Strengthening open access through collaboration: building connections with OP...
Week 4 Term 3 Study Techniques revisited.pptx
Module 3: Health Systems Tutorial Slides S2 2025
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
102 student loan defaulters named and shamed – Is someone you know on the list?
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf

Php2

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