Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Output function in PHP:-

PHP uses four different types of functions to display output.


1)  echo:-   It is used to print multiple statements using a PHP script.

<?php

echo "welcome","hello","xyz";
echo ("hello");
?>

it is mostly preferred to display output.


2) print:-   It is used to print a single statement using PHP Script.

<?php

print("welcome");  

print "welcome";

?>

3) printf():-   It is used to print formatted output means if we want to print data using format specifier %d,%f,%s then we can use printf() in PHP.
we can print only a single statement using printf.

<?php

printf("%d",12.345)   //12
printf("%.2f",12.3456789)   //12.34
printf("%s","hello")  //hello



?>


Another Example?

<?php
print 12.2345678;  //not possible till 2digit
print("<br>");
echo 12.2345678,"<br>";    //not possible till 2digit
printf("%.2f",12.2345678)  //possible
?>



sprintf():-  this method is used to return data in String format.

It is used to display data of one file to another file.
It is similar to printf() but it is return type function.

<?php
echo "<body bgcolor='cyan'>";
$a = sprintf("%.2f",12.2345678);
echo $a,"<br>";
$b=sprintf("welcome");
print $b;

echo "</body>";
?>

Complete Explanation of all output function using Single Program:-

Output function in PHP


<?php
echo "hello","world &nbsp;&nbsp;&nbsp;","welcome ","<br>";
print("welcome in php <br>");
print "welcome <br>";
echo ("scs <br>");
$a=12.345566666;
printf("%.2f<br>",$a);
$s = sprintf("%.2f",$a);
echo $s;

?>


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post