Tuesday 29 November 2016

Chapter 2(PHP) Some Basic Concepts of PHP With Program And Explanation


Program 1


How To Assign a value to a user define variable and to display it on execution?


<html>
<head></head>
<body>

<?php
$name = 'Rajat';
$class = 'BTECH';
$serialNumber = 1;

echo " I am $name studying in  $class. My serial number is $serialNumber.";
?>

</body>
</html>

EXPLANATION

In this program $name,$class and $serialNumber are user define variable.
and we will assign values to thses variables like $name = 'Rajat'; it means Rajat is assign to $name means $name contain the name Rajat.
$class = 'BTECH'; it means BTECH is assign to $class means $class contain BTECH.

The text value that should be assigned to user define variable should be writted between'    ' comma and ends by   ;   and if we assign a numeric value to variable than we should not write the numreic value in ' ' comma it will be writen directly as $serialNumber = 1; 

Now as we know echo is used to display combine these variable with text that should be written in echo like.

echo " I am $name studying in  $class. My serial number is $serialNumber."; 

OUTPUT



Program 2


How To Combine two user define variable or more in one userdefine variable  and to display it on execution?

<?php 

$identity = 'James Bond'; 
$car = 'BMW'; 

$sentence = "$identity drives a $car"; 
echo $sentence; 

?> 

EXPLANATION

In this program there are two variable $identity and $car and we assign values to these two variable 
and then assign to the third variable  like $sentence = "$identity drives a $car";  
it mean $sentense contain $identity value and $car value and the sentence become James Bond drives a BMW.
and in echo we will display $sentence.

OUTPUT
                   


Program 3


How To Add two numbers and to display it on execution?


<?php 

$a = 100; 
$b = 20;
$c=$a+$b; 

echo $c; 

?> 

EXPLANATION

In this program we take two user define variable $a and $b and assign values to these two variables .
and we take  the third variable $c in which we can ass both other variable like $c=$a+$b; 
and in echo we will display the variable $c in which we can add both the other variables.

OUTPUT


In same way we can substract,multiply and divide two numbers .






Program 4


How To Combine two user define in echo and to display on execution?

<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?> 

EXPLANATION

In this program we take two variable $txt1 and $txt2 and assign values to it like 
$txt1="Hello World!";
$txt2="What a nice day!";

in $txt1 we will assign Hello World!
in $txt2 we will assign What a nice day!

and combine these two in echo like echo $txt1 . " " . $txt2;
. " " . this is used to combine two variables

OUTPUT




Program 5


How To Count string length and display it on execution?


<?php

echo strlen("rajatsharma");
?> 

EXPLANATION

strlen means string length
strlen is used to count the string length  we have to write strlen in echo like 
echo strlen("rajatsharma");
it will count that how many chracters are there .

OUTPUT


Program 6


How To find position of chracter in string and display it on execution?


<?php
echo strpos("raj","a");
?> 

EXPLANATION

strpos means stringpostion.

In this program it will show the position of chracter a in the name  raj.
as we know count is start from 0 it means position of r is at 0 and a is at 1.
we have to write strpos in echo like 
echo strpos("raj","a"); 
it means tell postion of chracter a in raj

OUTPUT





No comments:

Post a Comment