PHP Basics

1. PHP Simple Program Structure

<?php
/*
* Author : Anthoniraj
* Created on 30-Dec-07
* Program Name : 01pgmstructure.php
*/
while(1){
echo “PHP is a server-side scripting language”;
break;
}
?>

2. String Handling

<?php
/*
* Author : Anthoniraj
* Created on 03-Jan-08
* Program Name : 02string.php
*/
include “check.php”;
echo “Hello Antony”;
echo ‘<br>”I\’ll be there at 10″‘;

?>

3.Array Manipulation

<?php
/*
* Author : Anthoniraj
* Created on 03-Jan-08
* Program Name : 03array.php
*/
//Creating An Array using array() function
$a=array(“name”=>”antony”,
“age”=>26,
“x”=>10
);
//Displaying all values of array
print_r($a);

//Displaying Specific Values from array
echo “<br>The name of the person is $a[name]<br>”;
echo “The age of $a[name]is $a[age]“;

//Two Dimensional Array
$two = array(“myarray”=>array(1,2,3));
echo “<br>”.$two[myarray][0];

//various types of array declaration
$normal =array(1,2,3,4,5,6,7,8,9,0);

$hash = array(“a”=>65 , “b”=> “antony”);

$two = array(“2″=>array(“x”=>11,”y”=>12));

//Updating array values
$hash[]=15;
$hash[11]=20;
print_r($hash);

//Deleting array values
unset($hash[0]);
unset($two);
print_r($hash);
print_r($two);

?>

4. Scope of Variables

<?php
/*
* Author : Anthoniraj
* Created on 03-Jan-08
* Program Name : 04variablescope.php
*/
/*
* Three types of variable scope are there
* 1. Normal Variable
* 2. Global Variable (keyword – global)
* 3. Static Variable (keyword – static)
*/
$sample = 10;

function normalVar()
{
$x=11;
echo “<br>The value is $x”;
echo “<br>This variable x cannot accessible by outside of the function”;
}

function statSample()
{
static $y =0;
echo “<br>The Value of static variable y is $y” ;
$y++;
}

function globSample()
{
global $sample;
$sample++;
}
echo “<br>The Global Value is $sample”;
normalVar();
statSample();
statSample();
?>

5.Constant in PHP

<?php
/*
* Author : Anthoniraj
* Created on Jan 8, 2008
* Program Name : 05constant.php
*/
define(x , 10);
echo x;

$x = 20;
echo $x;

?>

Leave a Response