PHP Filee
PHP Filee
TABLE OF CONTENTS
Output:
StaticVariableProgram
<html>
<head>
<title>staticvariable</title>
</head>
<body>
<?
phpfunctionte
st()
{
static
$x=0;echo"
$x";
$x++;
}
test();echo"
<br>";test()
;echo"<br>"
;test();echo
"<br>";test(
);echo"<br>
";test();
?>
</body>
</html>
Output:
CaseInsensitiveConst
<html>
<head>
<title>caseInsensitiveconst</title>
</head>
<body>
<?php
define("GREETING","Welcomeyouall",true);e
choGREETING ;
echo
"<br>";echogree
ting;
?>
</body>
</html>
Output:
ScopeVariableProgram(LocalAndGlobal)
<html>
<head>
<title>localandglobalvariable</title></head>
<body>
<?php
$x=5;fu
nctionte
st()
{
$y=10;
echo"<p>test variable inside
thefunction:<p>"; echo"variables x is :
$x";echo"<br>";
echo"variableyis
:$y";
}
test();
echo"<p>test variable outside
thefunction:<p>"; echo"variables x is :
$x";echo"<br>";
echo"variableyis
:$y";
?>
</body>
</html>
Output:
2
AccessingAssociativeArrayProgram
<html>
<head>
<title>accessingassociativearray</title>
</head>
<body>
<?php
$product=array("product_id"=>"P01",
"product_name"=>"pen","product_c
ost"=>50,"product_quantity"=>500);f
oreach($productas$key=>$value)
{
echo$key."=".$value."<br>";
}
?>
</body>
</html>
Output:
AccessingIndexArrayUsingEach
<html>
<head>
<title>Accessingindexedarrayeach</title>
</head>
<body>
<?php
$colors=array("red","green","yellow","black");while(list(
$key,$value)=each($colors))
{
echo"$key=>$value<br>";
}
?>
</body>
</html>
Output:
AccessingIndex ArrayUsingForEach
<html>
<head>
<title>accessingindexedarrayysingforeachloop</title>
</head>
<body>
<?php
$fruit=array("mango","apple","orange","grapes");foreac
h($fruitas $value)
{
echo$value."<br>";
}
?>
</body>
</html>
Output:
ProgramToShowFileInclusionStatement“Include”
InPhp
<html>
<head>
<title>includefunction</title>
</head>
<body>
<?php
Include’file.php’;
Echo”<br>fileincludedsuccessfully:p”;
?>
</body>
</html>
Contentoffile.php
<?php
Echo”helloIaminsidefile.php:p”;
?>
Output
AssociativeArray
<html>
<head>
<title>associativearray</title>
</head>
<body>
<?php
$product=array("product_id"=>"P01",
"product_name"=>"Pen","product_c
ost"=>50,"product_quality"=>500);ec
ho"<pre>";
print_r($product);echo"</
pre>";
?>
</body>
</html>
Output:
ConcatenatingAssignment
<html>
<head>
<title>concatenatingassignment</title>
</head>
<body>
<?php
$str="Welcome to php
";echo$str.="programming";
?>
</body>
</html>
Output:
CurrentFunction
<html>
<head>
<title>currentfunction</title>
</head>
<body>
<?php
$colors=array("red","green","blue","cyan");ec
hocurrent($colors);
?>
</body>
</html>
Output:
RequireFunction InPHP
<html>
<head>
<title>requirefunction</title>
</head>
<body>
<h2>requirefunctioninphp</h2>
<?php
require‘file.php’;
echo”<br>fileincludedsuccessfully:pusingrequirefunction!”;
?>
</body>
</html>
Contentoffile.php
<?php
Echo”helloIaminsidefile.php:p”;
?>
Output
EndFunction
<html>
<head>
<title>endfunction</title>
</head>
<body>
<?php
$colors=array("red","green","blue","cyan");
echo"thecurrentelementis".current($colors)."<br>";ech
o" the next element is" . next($colors) . "<br>";echo"
now the next element is" . next($colors) .
"<br>";echo"andthe
lastelementis" .end($colors)."<br>";
?>
</body>
</html>
Output:
FunctionAndVariableScope
<html>
<head>
<title>functionsandvariablescope</title>
</head>
<body>
<?php
$name="puneet";
functionDisplay()
{
global$name;
$name="sushila";echo"my
nameis".$name;
}
Display();
?>
</body>
</html>
Output:
FunctionCallByReference
<html>
<head>
<title>functioncallbyreference</title>
</head>
<body>
<?php
functionabc(&$x)
{
$x=$x+10;
return($x);
}
$a=20;
echo
abc($a)."<br>";echo
($a);
?>
</body>
</html>
Output:
Function CallByValue
<html>
<head>
<title>functioncallbyvalue
</title>
</head>
<body>
<?php
functionabc($x)
{
$x=$x+10;
return($x);
}
$a=20;
echo
abc($a)."<br>";echo
($a);
?>
</body>
</html>
Output:
DeleteUsingArraySplice Function
<html>
<head>
<title>deleteusingarraysplicefunction</title>
</head>
<body>
<?php
$colors=array("red","green","blue","cyan");array_splice($col
ors,1,2,
array("magenta","yellow","black",));foreach($colorsas$value
)
{
echo"$value<br>";
}
?>
</body>
</html>
Output:
DeleteUsingUnsetFunction
<html>
<head>
<title>deleteusingunsetfunction</title>
</head>
<body>
<?php
$color=array("red","blue","green","black");echo"t
heelementsoforiginalarrayare:<br>";foreach($colo
ras$key=>$values)
{
echo"[$key]=$values<br>";
}
unset($color[2]);
echo"<br>thearrayafterdeleted2indexdvalueis:<br>";foreach
($coloras$key=>$values)
{
echo"[$key]=$values<br>";
}
?>
</body>
</html>
Output:
ProgramUsingDoWhile
<html>
<head>
<title>ternaryoperator
</title>
</head>
<body>
<?php
$i=1;
do{
echo"thenumberis".$i."<br>";
$i++;
}
while($i<=5)
?>
</body>
</html>
Output:
EvenOddProgramUsingIfElse
<html>
<head>
<title>evenodd</title>
</head>
<body>
<?php
$number=45;if($n
umber%2==0)
{
echo"$numberisevennumber";
}
else
{
echo"$numberisodd";
}
?>
</body>
</html>
Output:
FactorialNumUsingFunction
<html>
<head>
<title>factorialnumusingfunction</title>
</head>
<body>
<?php
functionfact($n)
{
if($n==0)
{
return1;
}
else
{
return$n*fact($n-1);
}
}
$n=5;
echo"factorialof$nis:".fact($n);
?>
</body>
</html>
Output:
ForLoop
<html>
<head>
<title>ternaryoperator
</title>
</head>
<body>
<?
phpfor($i=1;$i<=10;$
i++)
{
echo"thenumberis:".$i."<br>";
}
?>
</body>
</html>
Output:
ForEach
<html>
<head>
<title>foreach</title>
</head>
<body>
<?php
$color=array("red","green","blue","yellow","orange");for
each($coloras$value)
{
echo"$value<br>";
}
?>
</body>
</html>
Output
Heredoc
<html>
<head>
<title>heredoc</title></head>
<body>
<?php
$my_string=<<<Text
thisislineno.1\nthisislineno.2\nthisislineno.3Text;
echonl2br($my_string);
?>
</body>
</html>
Output
Nowdoc
<html>
<head>
<title>nowdoc</title></head>
<body>
<?php
$one=1;
$two=2;
$three=3;
$my_string=<<<Textt
hisislineno.$one\
nthisislineno.$two\
nthis is line no.
$threeText;
echonl2br($my_string);
?>
</body>
</html>
Output:
SwitchStatement
<html>
<head>
<title>switchstatement</title>
</head>
<body>
<?php
$d=date("D");
switch($d)
{
case"Mon":
echo"TodayisMonday";
break;
case"Tue":
echo"TodayisTuesday";
break;
case"Wed":
echo"TodayisWednesday";
break;
case"Thu":
echo"TodayisThursday";
break;
case"Fri":
echo"TodayisFriday";
break;
case"Sat":
echo"TodayisSaturday";
break;
case"Sun":
echo"TodayisSunday";
break;
default:
echo"wonderwhichdayisthis!!";
}
?>
</body>
</html>
Output:
Swapping
<html>
<head>
<title>swapping</title>
</head>
<body>
<?php
$a=45;
$b=78;
echo"beforeswapping:<br>";
echo"a=".$a."<br>b=".$b;
$third=$a;
$a=$b;
$b=$third;
echo"<br><br>afterswapping:<br>";echo"
a=".$a."<br>b=".$b;
?>
</body>
</html>
Output:
TableOf5 UsingForLoop
<html>
<head>
<title>tableof5</title>
</head>
<body>
<?
phpdefine('
a',5);
for($i=1;$i<=10;$i++)
{
echo
$i*a;echo' &n
bsp;';
}
?>
</body>
</html>
Output:
Reverse UsingWhileLoop
<html>
<head>
<title>revese</title>
</head>
<body>
<?php
$num=23456;
$revnum=0;w
hile($num>1)
{
$rem=$num%10;
$revnum=($revnum*10)+$rem;
$num=($num/10);
}
echo"reversenumberof23456is:$revnum";
?>
</body>
</html>
Output:
SearchingAndReplacing
<html>
<head>
<title>searchingandreplacingstring</title>
</head>
<body>
<?php
$str="hello!welcometophpprogramming.";
$new_str=str_replace("hello","hi",
$str);echo "before replacement :
$str<br>";echo"afterreplacement:
$new_str";
?>
</body>
</html>
Output: