Tutorials

PHP for beginners -Lesson 1

Sending
User Rating 5 (1 vote)

Hey guys,
We will be updating basic to advanced level PHP tutorials by examples which will help all PHP programmers whether he/she is  novice or professional one.
In this lesson we will show you how to print values on browser. Basically there are  5 ways to print the output value on browser.

<?php
// <br /> Break row – Cursor points to next line. Same as \n in c programming

echo "Using echo : Hello aliencoders <br />"; // Output : Using echo : Hello aliencoders
// or
echo ("Using parenthesis  echo : Hello aliencoders <br />");  //Using parenthesis echo : Hello aliencoders
     //with parenthesis & without parenthesis both are Same

print "Using print : Hello aliencoders <br />"; // Output : Using echo : Hello aliencoders
//or
print ("Using parenthesis print : Hello aliencoders <br />"); // output : Using parenthesis print : Hello aliencoders

printf ("Using printf : Hello aliencoders <br />"); // Output : Using echo : Hello aliencoders

print_r ("Using print_r : Hello aliencoders <br />"); // Output : Using echo : Hello aliencoders

var_dump ("Using var_dump : Hello aliencoders <br />"); // Output : string 'Using var_dump : Hello aliencoders <br />' (length=41)

// Same output using variable;

$myvar = "Hello aliencoders";

echo "Using variable echo : $myvar <br />"; //Output :  Using variable echo : Hello aliencoders

print "Using variable print : $myvar <br />"; //Output :  Using variable echo : Hello aliencoders

printf("Using variable printf : %s <br />",$myvar); //Output :  Using variable echo : Hello aliencoders

print_r("Using variable print_r : $myvar <br />"); //Output :  Using variable echo : Hello aliencoders

var_dump("Using variable var_dump : $myvar <br />"); //Output :  string 'Using variable var_dump : Hello aliencoders <br />' (length=50)

?>

Next we will discuss about the difference amongst echo , print, print_r, printf and var_dump.
And which is the best one we should use in our project ?

For more queries , post your comment here.

Share your Thoughts