Thursday 6 September 2012

php basic programs

<?php    // functions with the arguments

function myfun($var1,$var2)
{   
 $output= $var1*$var2;
 //echo "$output".'<br/>';
}
$one=myfun('10','5');
$two=myfun('6','9');

echo "$one".'<br/>';
echo "$two";

// local and global varables with their scopes

$name='3kits';
function fun1()
{
    global $name;
    echo "welcome to $name with in the function ".'<br/><br/><br>';   
}

fun1();
echo "welcome to $name from outside funtion "."<br><br>";

// Arrays declaration and the execution
echo "arrays "."<br>";
$cart_array=array("tea","coffee","Milk");

echo "$cart_array[0] "."FIRST ITEM"."<br>";
echo "$cart_array[1] "."SECOND ITEM"."<br>";
echo "$cart_array[2] "."THIRD ITEM"."<br>"."<br>";

//echo Serializable($cart_array);    //not working (result is fatal error)


$cart_array1[]="raju"."<br>";
$cart_array1[]="suman"."<br>";
$cart_array1[]="tarun";
$cart_array1[]="uday";

echo $cart_array1[0];
echo $cart_array1[1]."<br>";


// associate arrays
echo "associate arrays "."<br>";

$cart_array_assoc["raju"]="25";
$cart_array_assoc["suman"]="30";
$cart_array_assoc["uday"]="35";
$cart_array_assoc["varun"]="40"."<br>";

echo "age of raju:".$cart_array_assoc["raju"]."<br>";
echo "age of suman".$cart_array_assoc["suman"];
echo "other way of the assoc array declaration"."<br>";
$cart_array_assoc_secondway=array("tomoto"=>"3.00",
                                    "potato"=>"5.00",
                                    "beans"=>"10.00","sapotas"=>"12.00","bananas"=>"2.00"   
                                    );


echo "Bananas rate in the market is:".$cart_array_assoc_secondway["bananas"]."<br>";
print_r($cart_array_assoc_secondway); //will print the values in a single lined fashion

//var_dump($cart_array_assoc_secondway)."from the var_dump ariable ";   //will print like the formatted way like their size and the values in the line by lin

// multi dimensional arrays
echo "multi dimensional arrays"."<br>";

$cart_array_multidimen=array("member1" =>array("name"=>"raju",           
                                                "height"=>"5.7",
                                               "weight"=>"55"
                                               ),
                             "member2"=>array("name"=>"raju1",           
                                                "height"=>"5.7",
                                               "weight"=>"55"
                                              ),
                            "member3"=>array("name"=>"raju2",           
                                                "height"=>"5.7",
                                               "weight"=>"55"
                                             )
                            );
                           
echo count($cart_array_multidimen)."<br>";
print_r($cart_array_multidimen);

?>
<?php
$shop = array( array( Title => "rose",
                      Price => 1.25,
                      Number => 15
                    ),
               array( Title => "daisy",
                      Price => 0.75,
                      Number => 25,
                    ),
               array( Title => "orchid",
                      Price => 1.15,
                      Number => 7
                    )
             );
echo '<u><h2 align="center">flowers shopping cart</h2></u>';
echo "<h1>Manual access to each element from associative array</h1>";

for ($row = 0; $row < 3; $row++)
{
    echo $shop[$row]["Title"]." costs ".$shop[$row]["Price"]." and you get minimum ".$shop[$row]["Number"];
    echo "<br />";
}
    echo "<h1>Using foreach loop to display elements</h1>";
    echo "<ol>";   // default it will print the heading values like 1.  2.
for ($row = 0; $row < 3; $row++)   //the variable must start from zero only
{
    echo "<li><b>The row number $row</b>";
    echo "<ul>";
    foreach($shop[$row] as $key => $value)
    {
        echo "<li>".$value."</li>";
    }
    echo "</ul>";
    echo "</li>";
}
echo "</ol>";
?>
<!--arrays in the arithmetic operations -->
<?php
$myArray = array(500.00, 300.00, 200.00, 400.00);
$result = 0;
foreach ($myArray as $key => $value) {
    $result = $result + $value;
}
$output = number_format($result);

?>

<h2>Rs:<?php echo $output; ?></h2>

<?php
$file_x="my_file.txt";
$createFile=touch($file_x);
if(file_exists($file_x))
{
   
echo "file has been created ";
   
}else
echo "file has not been created";


$target_file = "my_file.txt";
// Here we define the string data that is to be placed into the file
$target_file_data = "This is the string data or code I want to place in the newly created file.";
// Here we are creating a file(since it does not yet exist) and adding data to it
$handle = fopen($target_file, "w");
fwrite($handle, $target_file_data); // write it
fclose($handle);
// Here we are opening and appending to the file
$handle = fopen($target_file, "a");
// Here we define the string data that is to be appended to the data already in file
$target_file_data = "Here is more data I want to append to the file.";
fwrite($handle, $target_file_data); // write it
fclose($handle);

// Here we display the file contents by including it
include($target_file);

?>

<?php
//to write the string in the red colored text have to use it.

highlight_string(
  '<?php
     echo "this is the 3kits india ltd ";
 ?>'
 );

?>

<?php
// html encode and decode

$original_input = "<strong><em>Hello World!</em></strong>";
echo $original_input."origanl data" ;
echo "
<hr />
";
$html_encoded = htmlentities($original_input);
echo $html_encoded."after encoding";
echo "
<hr />
";
$html_decoded = html_entity_decode($html_encoded);
echo $html_decoded."after decoding";
?>

No comments:

Post a Comment