- Display simple message on php page using 'echo' :
echo 'hello php';
echo "hello php";
but,it is quite confusing to use single quotes and double quotes .let us clear that issue
echo 'hello \n php';
echo "hello \n php";
clarification :
single quotes does not consume '\n' and print as it is on php page . while double quotes consumes and does not shows '\n' on page .
- Simple Variabe usage :
$name='ashwani'; //declaration of variable prefix with $ symbol
echo "hi !! dear, $name"; // display alue stored in name variable .
- Remove data from variable
$name='ashwani'; //declaration of variable prefix with $ symbol
echo "hi !! dear, $name"
unset($name);
echo "after unset , $name"
Note : unset() gives an exception that is based on the error level of php page which can be modified as per requirement.
- Display the type of data using var_dump()
$age=32;
var_dump($age); //output: int
$name="ashwani\n";
var_dump($name); //output:'string' with length in trems of characters
$name='ashwani\n';
var_dump($name); //output:'string' with length in trems of characters
$percentage=78.65;
var_dump($percentage); //output: float
$result=true;
var_dump($result); //output:boolean
- Defining constants in php using define()
define('pi','mathematical pie');//declaration of constant value with name 'pi'
echo "define usage ".pi // display value of 'pi' constants
- Specify the type of data
$name="ashwani";
echo gettype($name); //output:string
- Display table using for loop in php
for($i=0;$i<=10;$i++)
{
echo "<li>2 * $i = 2*$i</li>";
}
- Arrays in php
$fruits=array('orange','mango','banana','apple'); //declaration of array with name fruits
echo "size of array name fruit is :".count($fruits); //counts the size of array
for($i=0;$i<=4;$i++)
{
echo $fruits[$i]."\r\n";//display each and every elements of array .
}
- foreach loop usage :
foreach($fruits as $f)
{
echo $f."\r\n";
}
- Array inside array using keys
$directory=array(
array('name'=>'ashy','phone'=>'5674'),
array('name'=>'ashwani','phone'=>'56')
);
echo "the first array element phone number is :".$directory[1]['phone'];
By using echo command we have display the value stored in array at index position 1 and with key ‘phone’.
- Remove the first element from array
array_shift($names); //$names is the name of array
- Remove the last element from array
array_pop($movies); //$movies is the name of array
- add element at the last in array
array_push($movies, 'Ratatouille');
- add element at the begining in array
array_unshift($movies, 'The Incredibles');
- Transfer data from one form to another form
We are going to transfer a value entered in a atext box and selected from a list
and transfer them on then next page .
Let us create an form in php using html
<html>
<head></head>
<body>
<form action="page2.php" method="post">
Enter name <input type="text" name="name"\><br>
Select month name :<select name="mnthname">
<option>January</option>
<option>February</option>
</select><br>
<input type="submit" value="transfer value onb next page ";
</form>
</body>
Save this file page1.php
Now create another file with name page2.php
<?php
$monthname=$_POST['mnthname']; //retrieve value from parameter
$Name=$_POST['name']; //retrieve value from parameter
if(empty($Name))
{
die('error please provide us a name'); //raise exception if Name is empty
}
else
{
}
?>
Now run page1.php and verifies the result.