Friday, 1 February 2013

Chapter 4 . Output (echo and print statements)

Obviously you guys don't want to write php code which does not output anything !! That's why you're here !! Today i'll cover some of the basic output methods in php . I will cover two of the most basic output methods today , and i will introduce you to some advanced output methods which we'll cover later on .


Firstly the command which i use the most (okay i'm being partial) , ECHO , the echo command can be used in any one of the following ways :-

echo "This is the text that'll be printed !! ";
echo("This is the text that'll be printed");
echo "This is the text "."that'll be printed" ; // The . joins the two strings and prints them

You can also use commas in the echo without parenthesis :-

echo "This is the text","that'll be printed";

However  you can't use commas in the one with parenthesis .

If you want to print variables , you can do it in the following way :-

echo $a;
echo "The value of a is :- ".$a;
echo "The value of a is {$a}";

However if you use single quotes in the last statement , the sentence will be printed as it is without the $a being substituted by the value it's holding .


So what's the basic difference between print and echo ? Well as we saw that echo can take multiple arguments or strings and output them together . However that is not so in case of print . In this case you can have only one argument . Also print returns a value which can be used to determine whether the print statement succeeded or not .

Eg :-

print("Hello World !!");

Other methods :- There are other advanced methods like print_r() -> Used to print out the array details or var_dump() -> Used to print out the object details , i'll cover these when i cover the respective chapters on array and objects !!