Monday, 30 January 2017

OUTPUT FUNCTIONS IN PHP

1. print – This function displays output on browser and returns value true if output is printed successfully, otherwise it returns false.

Example –
<?php
$rv = print "Rakesh";
print "<br>";
print $rv; // It returns either true or false
?>

Output –
Rakesh
1


2. echo – It is same as print, but it does not return any value so that its performance is faster than print function. We can also print multiple statements with an echo function.

Example –
<?php
echo "Scott"," Smith"," Alex";
?>

Output –
Scott Smith Alex


3. var_dump – Using this function we can display the value of variables along with datatypes.

Example –
<?php
$x=100;
$y="Scott";
var_dump($x);
var_dump($y);
?>

Output –
int(100)
string(5) "Scott"


4. printf – Using this function, we can print variables with the help of format specifier.

Example –
<?php
$x=100;
$y="Scott";
printf("%d ",$x);
printf("%s",$y);
?>

Output –
100 Scott


5. print_r – Using this function, we can display all elements of array and properties of object.

Example –
<?php
$arr=array(10,20,30);
print_r($arr);
?>

Output –
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
)


Note – On viewing the source code, the array is shown systematically on the screen.

No comments:

Post a Comment