46
7/21/2019 php notes http://slidepdf.com/reader/full/php-notes-56da286d75b75 1/46 Php small Program for beginners Quest : Print 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 <?php  for ($i=1; $i<=5; $i++)  {  for($j=1;$j<=$i;$j++)  {  echo $j! !;  "  echo !<#r%!; " ?% &utput: 1  1 2  1 2 3  1 2 3 4  1 2 3 4 5 Print  '  ' '  ' ' '  ' ' ' ' ' ' ' ' ' <?php  for ($i=1; $i<=5; $i++)  {  for ($=5; $%$i; $)  {

php notes

Embed Size (px)

DESCRIPTION

php notes

Citation preview

Page 1: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 1/46

Php small Program for beginnersQuest : Print11 2

1 2 31 2 3 41 2 3 4 5

<?php for ($i=1; $i<=5; $i++) {  for($j=1;$j<=$i;$j++)  {  echo $j! !;  "

  echo !<#r%!;"

?% 

&utput:  1  1 2  1 2 3

  1 2 3 4  1 2 3 4 5

Print  '  ' '  ' ' ' ' ' ' '

' ' ' ' '

<?php for ($i=1; $i<=5; $i++) {  for ($=5; $%$i; $)  {

Page 2: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 2/46

  print one sp*ce throh ht,- ;  echo ! !;  "for($j=1;$j<=$i;$j++)

  {

  echo !'!;  "  echo !<#r%!; "?% 

&utput:  '

  ' '

  ' ' '

  ' ' ' '

  ' ' ' ' '

What will be output of following program :<?php $.=/0; efine(6$.+1); $.=$.+; echo !<#r%!;

 echo $.;

 ?%*ns :90179

Find sum of even number between 1 to 100

Page 3: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 3/46

<?php $su,=7;

 for($i=1;$i<=177;$i++) {

  if($i82==7)  {  $su,=$su,+$i;  " "

 echo $su,;?%

 &utput: 2557

Check given number is even or odd

<?php $nu,=$9P&n; if($nu,82==7) {  echo $nu,! is e>en nu,#er!;"

 e-se {  echo $nu,! is o nu,#er!; "?%

Check given character is vowel or consonent<?php $ch*r=$9P&ch; if($ch*r==!*!) {  echo $ch*r! is >oe-!;"

 e-seif($ch*r==!e!) {  echo $ch*r! is >oe-!; "e-seif($ch*r==!i!)

 {  echo $ch*r! is >oe-!; "e-seif($ch*r==!o!)

 {

Page 4: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 4/46

  echo $ch*r! is >oe-!; "e-seif($ch*r==!u!)

 {  echo $ch*r! is >oe-!;

 "e-se

 {  echo $ch*r !is consonent!; "?% 

Write a program to print !our name 10 times

<?php $n*,e=!Pooj*!; for ($i=1; $i<=17; $i++) { echo !@A B*,e is: !$n*,e!<#r%! "?% 

&utput: @A B*,e is Pooj*  @A B*,e is Pooj*  @A B*,e is Pooj*  @A B*,e is Pooj*  @A B*,e is Pooj* 

Find the sum of 1 to 100

Page 5: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 5/46

<?php $su,=7; for ($i=1; $i<=177; $i++) {  $su,=$su,+$i;

 "echo $su,;

?% 

&utput: 5757

Find all even numbers between 1 to 100 using loop"#ot useconditional statement$

<?php for ($i=2; $i<=177; $i+=2) {

  echo $i! !; "

?% 

&utput: 2 4 C / 17 12 14 1C 1/ 27 22 24 2C 2/ 37 32 34 3C 3/ 47 4244 4C 4/ 57 52 54 5C 5/ C7 C2 C4 CC C/ D7 D2 D4 DC D/ /7 /2 /4 /C

  // 07 02 04 0C 0/ 177

Find all odd numbers between 1 to 100 using loop

Page 6: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 6/46

<?php for ($i=1; $i<=00; $i+=2)

 {  echo $i! !; "

?% 

&utput: 1 3 5 D 0 00

Find the %um of even and odd numbers between 1 to 100

<?php for ($i=1; $i<=177; $i++) {

  if($i82==7)  {  E$e>en=$e>en+$i;  "  e-se  {  E$o=$o+$i;  " "echo !u, of e>en nu,#ers=!$e>en!<#r%!;

 echo !u, of o nu,#ers=!$o;?% 

&utput:  u, of e>en nu,#ers=2557

Page 7: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 7/46

  u, of o nu,#ers=2577

&dd two numbers using loop"#ot use ' operator$

<?php E$f=$9FGf; E$s=$9FGs; for ($i=1; $i<=$s; $i++) {  $f++;

"echo !u, of i>en nu,#ers=!$f;?% 

<#oA% <for,%  Gnter first nu,#er   <input tApe=!te.t! n*,e=!f!%<#r%  Gnter econ nu,#er   <input tApe=!te.t! n*,e=!s!%<#r%  <input tApe=!su#,it! >*-ue=!*!%

 <for,%<#oA% 

&utput:  u, of i>en nu,#ers=1777

  Gnter first nu,#er

Gnter econ nu,#er

%ubtract two numbers using loop"#ot use ( operator$

Page 8: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 8/46

<?php E$f=$9FGf; E$s=$9FGs;

 for ($i=1; $i<=$s; $i++) {  $f;"echo !u#tr*ction of i>en nu,#ers=!$f;

?% 

<ht,-%<#oA% <for,%  Gnter first nu,#er 

  <input tApe=!te.t! n*,e=!f!%<#r%  Gnter econ nu,#er   <input tApe=!te.t! n*,e=!s!%<#r%  <input tApe=!su#,it! >*-ue=!u#tr*ct!% <for,%<#oA% 

&utput:  u#tr*ction of i>en nu,#ers=1777

  Gnter first nu,#erGnter econ nu,#er

Write a program to displa! table of given number

<?php E$t*#=$9FGt*#; $i=1;

 o  {  $t=$t*#'$i;  echo $t! !; 

$i++; "

Page 9: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 9/46

 hi-e ($i<=17);?% 

<#oA% <for,%

  Gnter Hour t*#-e  <input tApe=!te.t! n*,e=!t*#!%<#r%  <input tApe=!su#,it! >*-ue=!*#-e!% <for,% <#oA%

Quest : Print 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 <?php for ($i=1; $i<=5; $i++) { for($j=1;$j<=$i;$j++) {

  echo $j! !; " echo !<#r%!; " ?% &utput: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Online Translatorhis is &n-ine tr*ns-*tor Its e*sA to tr*ns-*te Aour site to *nA -*nu*eHou c*n fi.to *nA position in this tr*ns-*tor it consists *#o>e 47 -*nu*eHou c*n cre*te e*sAr*ns-*tor

 just copA *n p*ste * jJuerA coe *n fi. to *nA p-*ce Aour site

)(*uer!:

<script tApe=!te.tj*>*script! src=!http:.tr*ns-*tethistr*ns-*tethisjs!%<script%<script tApe=!te.tj*>*script%r*ns-*tehis();<script%

+tml:

<i> i=!tr*ns-*tethis! stA-e=!ith:257p.; ,*rintop:3p.;#orer:7 re so-i;f-o*t:riht; te.t*-in:riht;!%

<* stA-e=!ith:1/7p.;heiht:1/p.;isp-*A:#-oc;!c-*ss=!tr*ns-*tethis#utton!href=!http:*#cco,!%<*%

<i>%

Page 10: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 10/46

PHP Array Functions

&rra!

Function What it ,oes

print-r"$ Print the value and inde. of an arra!"in

assciative form$

/php

  strarra!"2welcome232student2$4

  print-r"5str$4

 6

 

utput : &rra!"8062welcome238162student2$

e.plode"$ %plits a string into arra! elements

Page 11: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 11/46

/php

  5str2welcome user how r !ou24

  print-r"5str$4

 

5arre.plode"2 235str$4

  print-r"5arr$4

 

 e.plode

  5val2103;03<03=03>03?024

  5arr1e.plode"23235val$4

  foreach"5arr1 as 5v$

  @

  A5s'5v4

  B

echo 5s4

 6

 

Page 12: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 12/46

 utput : &rra!"8062welcome238162student2$

implode"$ )oins arra! elements into a string

/php

  5arrarra!"2hello232user2$4

  5strimplode"2 235arr$4

  echo 5str4

 6

 

utput : hello user 

count"$ eturns an integer value indicating how

Page 13: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 13/46

man! elements an arra! contains

/php

  5arrarra!"103;03<03=03>0$4

  echo 2#umber of elements 2count"5arr$2/br624

 

 use inside for loop to check condition

  for"5i045i/count"5arr$45i''$

  @

  A5sum5sum'5i4

  B

  echo 2sum25sum4

 6

 

utput : #umber of elements>

  sum1>0

 

Page 14: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 14/46

range"$ Denerates a number range as an arra!

/php

  5rangerange"2&232E2$4

  print-r"5range$4

 6

 

utput : &rra! " 80 6 & 81 6 8; 6 C

8< 6 , 8= 6 G 8> 6 F 8? 6 D 87 6 +

8H 6 I 89 6 ) 810 6 J 811 6 K 81; 6 E $

ma."$ Finds the largest value in an arra!

/php

  5ma.arra!"103;03<03=031003>0$4

  echo ma."5ma.$4

 6

 

Page 15: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 15/46

 utput : 100

 

min"$ Finds the smallest value in an arra!

/php

  5ma.arra!"103;03<03=031003>0$4

  echo min"5ma.$4

 6

 

utput : 10

 

shuffle"$ andoml! rearranges the se*uence of  

elements in an arra!

Page 16: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 16/46

/php

  5arrrange"2a232L2$4

  shuffle"5arr$4

  print-r"5arr$4

 6

 

utput : efresh page3 on ever! refresh it

rearrange

the inde. of arra!

&rra! " 80 6 g 81 6 a 8; 6 c 8< 6 d 8= 6 b 8> 6

  k 8? 6 i 87 6 l 8H 6 f 89 6 M 810 6

e 811 6 m 81; 6 h $

arra!-rand"$ Finds the random inde. of an arra!

/php

  5arrrange"2a232L2$4

Page 17: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 17/46

  5randarra!-rand"5arr$4

  echo 5rand4

 6

 

utput : efresh page3 on ever! refresh it

shows

an random inde. bw a to L

 

arra!-slice"$ G.tracts a segment of an arra!

/php

  5colorarra!"2blue232red232green232pink2$4

  5slicearra!-slice"5color313;$4

  print-r"5slice$4

 6

 

Page 18: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 18/46

 utput : &rra! " 80 6 red 81 6 green $

arra!-shift"$ emoves an element from the beginning

of an arra!

/php

  5cit!arra!"2delhi232patna232noida2$4

  arra!-shift"5cit!$4

  print-r"5cit!$4

 6

 

utput : &rra! " 80 6 patna 81 6 noida $

Page 19: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 19/46

 

arra!-unshift"$ &dds an element to the beginning of an

arra!

/php

  5cit!arra!"2delhi232patna232noida2$4

  arra!-unshift"5cit!32banglore2$4

  print-r"5cit!$4

 6

 

utput : &rra! " 80 6 banglore 81 6 delhi

8; 6 patna 8< 6 noida $

arra!-pop"$ emoves an element from the end of an

arra!

Page 20: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 20/46

/php

  5cit!arra!"2delhi232patna232noida2$4

  arra!-pop"5cit!$4

  print-r"5cit!$4

 6

 

utput : &rra! " 80 6 delhi 81 6 patna $

 

arra!-push"$ &dds an element to the end of an arra!

/php

  5cit!arra!"2delhi232patna232noida2$4

  arra!-push"5cit!32gurgaon2$4

  print-r"5cit!$4

 6

 

Page 21: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 21/46

 utput : &rra! " 80 6 delhi 81 6 patna

8; 6 noida 8< 6 gurgaon $

 

arra!-uni*ue"$ emoves duplicate elements from an

arra!

/php

  5cit!arra!"2delhi232patna232noida232delhi232patna2$4

  5uniarra!-uni*ue"5cit!$4

  print-r"5uni$4

 6

 

utput : &rra! " 80 6 delhi 81 6 patna 8; 6

Page 22: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 22/46

noida $

arra!-reverse"$ everses the se*uence of elements in

an arra!

/php

  5cit!arra!"2delhi232patna232noida2$4

  5revarra!-reverse"5cit!$4

  print-r"5rev$4

 6

 

utput : &rra! " 80 6 noida 81 6 patna 8; 6

delhi $

arra!-merge"$ Combines two or more arra!s

Page 23: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 23/46

/php

  5cit!arra!"2delhi232patna2$4

  5cit!1arra!"2gurgaon232banglore2$4

  5mergarra!-merge"5cit!35cit!$4

  print-r"5merg$4

 6

 

utput : &rra! " 80 6 delhi 81 6 patna 8; 6

  gurgaon 8< 6 banglore$

arra!-intersect"$ Calculates the common elements

between two or more arra!s

/php

  5arr1arra!"2red232blue232pink2$4

  5arr;arra!"2black232blue232white2$4

Page 24: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 24/46

  5commonarra!-intersect"5arr135arr;$4

  print-r"5common$4

 6

 

utput : &rra! " 81 6 blue $

arra!-diff"$ Calculates the difference between two

arra!s

/php

  5arr1arra!"2red232blue232pink2$4

  5arr;arra!"2black232blue232white2$4

 

5diffarra!-diff"5arr;35arr1$4

  print-r"5diff$4

 6

 

Page 25: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 25/46

utput : &rra! " 80 6 black 8; 6 white $

in-arra!"$ Checks if a particular value e.ists in an

arra!

/php

  5arr1arra!"2red232blue232pink2$4

  echo in-arra!"2red235arr1$4

 6

 

utput : 1

 

arra!-ke!-e.ists"$ Checks if a particular ke! e.ists in an

Page 26: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 26/46

arra!

/php

  5arr1arra!"2red232blue232pink2$4

  echo arra!-ke!-e.ists"135arr1$4

 6

 

utput : 1

 

sort"$ %orts an arra!

/php

  5arrarra!"1003113>31;31>030$4

  sort"5arr$4

  print-r"5arr$4

 6

 

Page 27: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 27/46

utput : &rra! " 80 6 0 81 6 > 8; 6 11

8< 6 1; 8= 6 100 8> 6 1>0 $

rsort"$ everse %orts an arra!

/php

  5arrarra!"1003113>31;31>030$4

  rsort"5arr$4

  print-r"5arr$4

 6

 

utput : &rra! " 80 6 1>0 81 6 100 8; 6 1;

  8< 6 11 8= 6 > 8> 6 0 $

Page 28: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 28/46

 

asort"$ %orts an associative arra! b! value

/php

  5colarra!"2a262red232L262blue232!262green2$4

  asort"5col$4

  print-r"5col$4

 6

 

utput : &rra! " 8L 6 blue 8! 6 green 8a 6 red

$

arsort"$ everse(%orts an associative arra! b!

value

/php

  5colarra!"2a262red232L262blue232!262green2$4

Page 29: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 29/46

  arsort"5col$4

  print-r"5col$4

 6

 

utput : &rra! " 8a 6 red 8! 6 green 8L 6 blue

$

ksort"$ %orts an associative arra! b! ke!"inde.$

/php

  5colarra!"2a262red232L262blue232!262green2$4

  ksort"5col$4

  print-r"5col$4

 6

 

Page 30: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 30/46

utput : &rra! " 8a 6 red 8! 6 green 8L 6 blue

$

krsort"$ everse(%orts an associative arra! b!

ke!"inde.$

/php

  5colarra!"2a262red232L262blue232!262green2$4

  krsort"5col$4

  print-r"5col$4

 6

 

utput : &rra! " 8L 6 blue 8! 6 green 8a 6 red

$

Page 31: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 31/46

arra!-product"$ Calculates the product of an arra!

/php

  5aarra!">3>3>3=$4

  echo"arra!-product"5a$$4

 6

 

utput : >00

arra!-sum"$ Calculates the sum of an arra!

/php

  5aarra!"06>3161>3;6;>31>$4

  echo arra!-sum"5a$4

 6

 

Page 32: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 32/46

utput : ?0

arra!-search"$ %earch an arra! for a value and returns

the ke!

/php

  5aarra!"2a262,og232b262Cat232c262+orse2$4

  echo arra!-search"2+orse235a$4

 6

 

utput : c

Page 33: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 33/46

arra!-splice"$ emoves selected elements from an

arra! and replaces it with new

elementsIt also returns an arra! with

the removed elements

/php

  5a1arra!"062,og23162Cat23;62+orse23<62ird2$4

  5a;arra!"062Niger23162Kion2$4

  print-r"arra!-splice"5a1303;35a;$$4

 6

 

utput : &rra! " 80 6 ,og 81 6 Cat $

current"$ eturns the value of the current element

Page 34: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 34/46

in an arra!It returns F&K%G on empt!

elements or elements with no value

/php

  5arrarra!"2dog232cat232horse2$4

  echo current"5arr$4

 6

 

utput : dog

ke!"$ Fetch a ke! from an arra!

/php

  5arra! arra!"

  Ofruit1O 6 OappleO3

  Ofruit;O 6 OorangeO3

  Ofruit<O 6 OgrapeO3

Page 35: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 35/46

  Ofruit=O 6 OappleO3

  Ofruit>O 6 OappleO$4

  while "5fruit-name current"5arra!$$

@

  if "5fruit-name OappleO$

  @

  echo ke!"5arra!$O

O4

  B

  ne.t"5arra!$4

  B

 6

 

utput : fruit1

  fruit=

  fruit>

Page 36: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 36/46

Php ArrayArray :- A data structure that stores one or more similar type of values in a single value.

Use array() function to create array

Example:-

+ere !ou can find the code please click below

,GE

Three Types Of Array :-

• Numeric array - An array with a numeric index. These arrays can store numbers,

strings and any object but their index will be prepresented by numbers. By default array

index starts from ero!"#.

$%php

 &' (st method to create array. '&

)num * array! (, +, , , ,/,0,1,2#3

foreach! )num as )value #

4

  echo 56alue is )value $br &753

8

 &' +nd method to create array. '&

)num9": * 5one53

)num9(: * 5two53

)num9+: * 5three53

)num9: * 5four53

)num9: * 5five53

)num9: * 5six53

)num9/: * 5seven53

)num90: * 5eight53

)num91: * 5nine53

Page 37: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 37/46

foreach! )num as )value #

4

  echo 56alue is )value $br &753

8

%7

• Associative array - An array with strings as index. This stores element values in

association with ;ey values rather than in a strict linear index order.Associative array is very

similar to numeric arrays in term of functionality but they are different in terms of their

index.

<ote=- >on?t ;eep this array inside the double @uote while printing it otherwise it will not

give any return value.

$%php

 &' (st method to associate create array. '&

)salaries * array!5ritu5 *7 +""",5raj5 *7 +"""",5;umar5 *7 ("""#3

echo 5alary of ritu is 5. )salaries9?ritu?: . 5$br &753

echo 5alary of raj is 5. )salaries9?raj?:. 5$br &753

echo 5alary of ;umar is 5. )salaries9?;umar?:. 5$br &753

 &' +nd method to create array. '&

)salaries9?ritu?: * 5high53

)salaries9?raj?: * 5medium53)salaries9?;umar?: * 5low53

echo 5alary of ritu is 5. )salaries9?ritu?: . 5$br &753

echo 5alary of raj is 5. )salaries9?raj?:. 5$br &753

echo 5alary of ;umar is 5. )salaries9?;umar?:. 5$br &753

%7

• Multidimensional array - An array containing one or more arrays and values are

accessed using multiple indices.ach element in the main array can also be an array,And

each element in the sub-array can be an array.

$%php

  )mar;s * array!5ritu5 *7 array!5physics5 *7 ,5maths5 *7 ",5chemistry5 *7 2#,

  5raj5 *7 array

  !

  5physics5 *7 ",

Page 38: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 38/46

  5maths5 *7 +,

  5chemistry5 *7 +2

  #,

  5;umar5 *7 array

  !

  5physics5 *7 (,

  5maths5 *7 ++,

  5chemistry5 *7 2

  #

  #3

  &' Accessing multi-dimensional array values '&

  echo 5Car;s for ritu in physics = 5 3

  echo )mar;s9?ritu?:9?physics?: . 5$br &753

echo 5Car;s for raj in maths = 53

  echo )mar;s9?raj?:9?maths?: . 5$br &753

echo 5Car;s for ;umar in chemistry = 5 3

  echo )mar;s9?;umar?:9?chemistry?: . 5$br &753

%7

To Find The Factorial Of A Number Using PHPA factorial of a number n is the product of all the numbers between n and 1.The easiest way to calculate it is with a for !loop which one that starts at n and counts down to 1. "ach time the loop runs#the pre$iously calculated product is multiplied by

the current $alue of the loop counter and the result is the factorial of the number n. 

To find the factorial number# you can use a loop to count down and multiply the number by allthe numbers between itself and 1 such as%

&'php(num ) *+(factorial ) 1+for (,)(num+ (,-)1+ (,! /  (factorial ) (factorial 0 (,+  echo (,.&2br-+3

echo Factorial of (num is (factorial+'-

Output will be :

432

Page 39: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 39/46

1Factorial of 4 is 24

To find the factorial of a number using form in php

index.php

<?php/* Function to get Factorial of a Number */function getFactorial($num)  $fact ! 1"  for($i ! 1" $i <! $num "$i##)  $fact ! $fact * $i"  return $fact"?%<&'octpe html%<html%<hea'%<title%Factorial rogram using </title%</hea'%<bo'%<form action ! ++ metho'!+post+%,nter the number -hose factorial re.uires <r /%<input tpe!+number+ name!+number+ i'!+number+ ma0length!+4+ autofocus re.uire'/%<input tpe!+submit+ name!+submit+ alue!+ubmit+ /%</form%

<?phpif(isset($567submit78) an' $567submit78 !! +ubmit+)  if(isset($567number78) an' isnumeric($567number78))    echo 7Factorial f Number9 <strong%7:getFactorial($567number78):7</strong%7" 

?%</bo'%

</html%

 Output will be :

if you enter 5 and click to submit then :

Page 40: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 40/46

,nter the number -hose factorial re.uires

Factorial f Number9 120

To Find 4econd 5argest 6alue From An ArrayHere is the e,ample to find the second largest $alue from an array.

array.php

&78O9T:P" html PU;5<9 22=>9228T8 ?HT@5 1. Transitional22"Nhttp%22www.w>.org2TB2,html128T82,html1transitional.dtd-&html ,mlns)http%22www.w>.org21CCC2,html-&head-

&meta httpeDui$)9ontentType content)te,t2html+ charset)utfE 2-&title-Array&2title-&2head-

&body-&h1-To Find 4econd 5argest 6alue From An Array&2h1-&'phpfunction secondlargest(arr!/sort(arr#4OBTNU@"B<9!+return(arrGcount(arr!I!+3

echo 4econd 5argest 6alue From Array is %.secondlargestarrayJ#*#>#1#E#C#1#!!+'-&2body-&2html-

OUTPUT :To Find Second Largest Value From An Array4econd 5argest 6alue From Array is %C

To Find The 5argest And 4mallest 6alues From An Arrayma."$ function is a properties of an array and is used to find the largest $alue from an array.

!ample :ma!.php

&78O9T:P" html PU;5<9 22=>9228T8 ?HT@5 1. Transitional22"Nhttp%22www.w>.org2TB2,html128T82,html1transitional.dtd-&html ,mlns)http%22www.w>.org21CCC2,html-&head-

u#,it

Page 41: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 41/46

&meta httpeDui$)9ontentType content)te,t2html+ charset)utfE 2-&title-Array&2title-&2head-&body-&h1-To Find 5argest 6alue From An Array&2h1-&'php

(array)array#>#J#*#E#1#1J#1!+(a)ma,(array!+echo 5argest 6alue From An Array is %.(a.&2br-+'-&2body-&2html-

"ote % To Find The 4mallest 6alue From An Array we use min#$ function.min"$ function is a properties of an array and is used to find the smallest $alue from an array.

!ample :&'php

(array)array#>#J#*#E#1#1J#1!+(a)min(array!+echo 4mallest 6alue From An Array is %.(a.&2br-+'-

Add A 9alendar 8ate PicKer To A Form Using LMueryere is an e0ample; tr it:

n this co'e -hen ou clic= in te0t bo0 then calen'er is automaticall generate':

 inde!.html

&7doctype html-

&html-

&head-

&meta charset)utfE-

&title-&2title-

 &linK href)http%22aLa,.googleapis.com2aLa,2libs2LDueryui21.E2themes2base2LDueryui.css

rel)stylesheet type)te,t2css2-

&script type)te,t2La$ascriptsrc)http%22aLa,.googleapis.com2aLa,2libs2LDuery21..2LDuery.min.Ls-&2script-

&script src)http%22aLa,.googleapis.com2aLa,2libs2LDueryui21.E2LDueryui.min.Ls-&2script-

  &script-

  (document!.readyfunction! /

  (datepicKer!.datepicKer!+

  3!+&2script-

Page 42: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 42/46

&2head-

&body-

 8ate% &input type)te,t id)datepicKer-

 

&2body-

&2html-

",amples of PHP Program Using Array

rray can be intiali!ed in three ways in "#".

1st $ay : $col!arra(+blue+;+re'+;+green+;+-hite+;+pin=+)"

2nd $ay :  $col6 8!+blue+"

  $col6 8!+re'+"  $col6 8!+green+"  $col6 8!+-hite+"  $col6 8!+pin=+"

%rd $ay : $col6>8!+blue+"  $col618!+re'+"  $col628!+green+"  $col638!+-hite+"  $col648!+pin=+"

+ere !ou can find the G.ample please click below

G.ample

TO &ind the 'um Of n rray (sing "#"

<?php$sum!>"  $arr!arra(1;2;3;4;)" for($i!>"$i<count($arr)"$i##)

    $sum!$sum#$arr6$i8"

   echo +um f @ien Arra is9+ :$sum"?%

To &ind The 'um of )*en nd Odd +umbers

&'php

Page 43: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 43/46

(arr)array1#11#1#1>#1*#1J!+

 

for(i)+(i&count(arr!+(i!

  /

  if(arrG(iIQ))!

  /

  R(e$en)(e$en(arrG(iI+

  3

  else

  /

  R(odd)(odd(arrG(iI+

  3

3

 

echo 4um of e$en).(e$en.&br2-+

 

echo 4um of odd).(odd+

'-

PHP Program To Find Be$erse Of A Number Or 4tring&'php(int ) Bitu BaL+22 here you can use any number or string(re$erse ) strre$(int!+ 22 for re$ersing the stringecho 4tring% . (int . &br-+echo Be$erse 4tring % . (re$erse +'-

OutPut%

4tring% Bitu BaLBe$erse 4tring % LaB utiB

PHP Program To Find A AnagramAn anagram is a rearrangement of the letters of one word or phrase to form another word or

phrase.%n other words %Any word or phrase that e,actly reproduces the letters in another order is ananagram.For e,ample# orchestra is a transposition of carthorse#  dog is a transposition of god#  army is a transposition of mary."ote % The original word or phrase is Known as the subLect of the anagram.

Page 44: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 44/46

A simple PHP Program for Anagram is gi$en below

&'phpifempty(word!! (word)dog+for(i)+(i&strlen(word!+(i! /  (lettersG(iI)substr(word#(i#1!+

3shuffle(letters!+(anagram)implode(letters!+echo One anagram of (word is (anagram.+'-

Output %

One anagram of dog is godOne anagram of dog is gdo#One anagram of dog is dgoand more....

Program to find a string is Palindrome or Not in Php&'php(word ) Malayalam+ 22 declaring a $aribaleecho 4tring% . (word . &br-+(re$erse ) strre$(word!+ 22 re$ersing wordif (word )) (re$erse! 22 comparing if original word is same as the re$erse of the same word  echo SOutput% The string is a palindromeS+else  echo SOutput% This is not a palindromeS+

'-

Find the string palindrome or not in P&P without using strre'#$ (unction.

&'php(mystring ) madam+ 22 set the stringecho 4tring% . (mystring+(myArray ) array!+ 22 php array(myArray ) strsplit(mystring!+ 22 split the array(len ) sieof(myArray!+ 22 get the sie of array(new4tring ) +

for (i ) (len+ (i -) + (i! /  (new4tring.)(myArrayG(iI+3echo &br-+if (mystring )) (new4tring! /  echo Output% . (mystring . is a palindrome+3 else /  echo Output% . (mystring . is not a palindrome+

Page 45: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 45/46

3'-

4earch A @:4M5 8atabase Using php

+ere !ou can find the code please click below

,GE

Form.php&78O9T:P" html-&html dir)ltr lang)enU4 -&head-&meta charset)UTFE 2-&title-4earch&2title-&2head-&body-

&h-search by name&2h-&form action)search.php method)get-  &label-Name%  &input type)te,t name)name 2-  &2label-  &input type)submit $alue)4earch 2-

&2form-&2body-&2html-

Search.php

&'php

(searchTerm ) trim("TGSnameSI!+if(searchTerm )) !/ echo "nter name +

 e,it!+3

22database connection info(host ) localhost+(db ) dbname+(user ) root+(pwd ) ubuntuRrituraL+(linK ) mysDliconnect(host# (user# (pwd# (db!+

Page 46: php notes

7/21/2019 php notes

http://slidepdf.com/reader/full/php-notes-56da286d75b75 46/46

(Duery ) 4"5"9T 0 FBO@ student =H"B" name 5<V" SQ(searchTermQS+(results ) mysDliDuery(linK# (Duery!+ifmysDlinumrows(results! -) 1!/ (output ) + while(row ) mysDlifetcharray(results!!

 /  (output .) <8% . (rowGSidSI . &br 2-+  (output .) Name% . (rowGSnameSI . &br 2-+  (output .) 9ourse% . (rowGScourseSI . &br 2-+  (output .) Age% . (rowGSageSI . &br 2-&br 2-+ 3 echo (output+3else echo No record Found for Name . (searchTerm+

'-

)atabase

dbname #database name!

stuent (t*#-e n*,e)

id name course age

1 Kitur*j u,*r ech 23

2 Ko#i to,*r nroi 24

3 hih* ch*uh*rA  spBet 23

4 L*rshu G& 21

5 Beer*j MI Ne>e-oper   23