Tuesday 9 April 2013

php basic


<html>
<head>
</head>
<body>
<?php
//variable declaration
$a=10;
$b=20;
$c=$a+$b;
//output statement
echo "sum of two variable values are :$c <br/>";
echo "We can write the code here after the line break <br/>";
//variable declaration with string
$text="Here we can write text";
echo $text;
//concatination operator in php
$var1="<br/><b>Hello,Hi</b>";
$var2="<i>This is shankar</i><br/>";
echo $var1." ".$var2;
//string length function
echo strlen("shankar");
print"<br/>";
//string position
echo strpos("Hello shankar","s");
echo "<br/>";
//if statements
$t=date("H");
if($t<"20")
{
echo "now the time is below 20 hours<br/>";
}
//if else statement
$time=date("H");
if($time<"12")
{
echo "Good Morning<br/>";
}
else
{
echo"Good Afternoon</br>";
}
// if -else- if statement
$timing=date("H");
if ($timing<"10")
  {
  echo "Have a good morning!";
  }
else if ($timing<"20")
  {
  echo "Have a good day!";
  }
else
  {
  echo "Have a good night!";
  }
echo"<br/>";
// switch statement
$favcolor="Blue";
switch($favcolor)
{
case "Red":
echo "Fav Color is Red";
break;
case "Blue":
echo "Fav color is Blue";
break;
default:"Nothing was choosen";
}
//PHP Arrays
$cars=array("BMW","Portio","Zaquar");
echo "I like ".$cars[0].", ".$cars[1].",".$cars[2];
//length of array
$naming=array("shankar","haraveer","SaiSrinivas");
echo count($naming);
echo"<br/>";
//Loop through indexed array
$arrLength=count($naming);
for($x=0;$x<$arrLength;$x++)
{
echo $naming[$x];
echo"<br/>";
}
//Associative array
$Age=array("shankar"=>"24","Haraveer"=>"25","SaiSrinivas"=>"23");
echo "shankar has".$Age['shankar']." Years old";
echo "<br/>";
// loop through an associative array
foreach($Age as $name=>$AgeValue)
{
echo "Name is ".$name." and Age is ".$AgeValue;
echo "<br/>";
}
print"<br/>";
//Sorting arrays --- sort array in accending order
$caring=array("Volvo","BMW","Toyota");
sort($caring);
$clength=count($caring);
for($x=0;$x<$clength;$x++)
   {
   echo $caring[$x];
   echo "<br>";
   }
//while loop
$i=1;
while($i<=5)
{
echo "Values are :".$i."</br>";
$i++;
}
//do-while
$j=1;
do
{
echo "number is:".$j."</br>";
$j++;
}
while($j<=5);
//for loop
for ($k=1; $k<=5; $k++)
  {
  echo "The for loop number is " . $k . "<br>";
  }
//php function
function empName()
{
echo "SHANKAR";
}
echo "my name is";
empName();
echo"<br/>";
//php function with parameter
function Name($fname)
{
echo $fname;
}
Name("PARSANAMONI");
//function return values
function Addition($x,$y)
{
$total=$x+$y;
return $total;
}
echo"<br/>";
echo "the values of 10+20 is".Addition(10,20);
?>
</body>
</html>

No comments: