Saturday, June 30, 2018

Output Functions in PHP



Output functions in php are used to display some text on browser.

Types of output functions are:

1.Print:

The print statement can be used with or without parentheses: print or print().

Display Text


Example

<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
Run example »
Display Variables

The following example shows how to output text and variables with the print statement:

Example

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>
Run example »



The following example shows how to output text with the print command (notice that the text can contain HTML markup):

This function displays output on browser and returns a boolean value.

Note: The value true is returned if the output is printed sucessfully else it returns false.In php true value is 1 and false doesn't contain any value.We cannot use print function to display multiple functions at once.

<?php

$rval=print("scott");

print $rval;

?>

2.Echo:


The echo statement can be used with or without parentheses: echo or echo().
Display Text
The following example shows how to output text with the echo command (notice that the text can contain HTML markup):

Example

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
Run example »
Display Variables
The following example shows how to output text and variables with the echo statement:

Example

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
Run example »



It is same as print function but doesn't return any value.Hence, it is faster than print function.we can print multiple statements with this function.

<?php

echo("Scott","John","Smith");

?>

3.Var_dump:This function displays variable value along with its data type.

<?php

$sno=123;

$un="Smith";

var_dump($sno); //displays int(123)

var_dump($un); //string(5) "Smith"

?>

4.Print_r:By using this function wecan display all elements of array and properties of objects.Before learning about this print_r function we should have some basic understanding about arrays.

Array:Every array in php is contribution of 'KEY' and 'VALUE".The default key starts from zero.

$arr=array(10,20,30);

Since php is loosely typed language,we can store collection of different datatypes in arrays.The difference between normal variable and array isthat a normal variable can store single value whereas an array can store multiple values.

<?php

$arr=array(10,20,30);

print_r($arr); //prints all array elements

echo $arr[1]; //20

?>

In the above example the line print_r($arr); prints all array elements in the 'KEY' and 'VALUE' style.

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

For More Info: Clickhere