In PHP for output the string, variable, objects, array we use different function like echo, print, print_r,
var_dump. In this tutorial I am trying to elaborate the each function with suitable example.
Echo
The beauty o f Echo it not only uses as a function we can also use it as a constructor without
parenthesis.
Function example:-
echo ("echo function call");
In Echo when we pass the argument in parenthesis then it become function as show above in example.
The return type of Echo function is Void. It is a slightly faster than print function.
Constructor Example:-
If you want to pass more than one parameter then we can also use Echo but for this we should pass the
parameters without parenthesis.
Constructor Example:-
echo "construct 1 param","construct 2 param","construct 3 param";
In constructor we concatenate the parameter with semi colon “,”.
Echo has one more form from which we can out put the string. We can say it’s a shortcut to output the
data. For this it’s necessary to “short_open_tag” configuration option enabled. In short form you can
immediately follow the opening with equal sign.
Shortcut Example
<?=”it is a shortcut example ”?>
Print:-
Print can be also used as a function as well as a constructor (without parenthesis). But we can pass only
one parameter. Beauty of Print function/constructor is it returns the 1 on success.
Function Example:-
print("print function call");
Constructor Example:-
print "print constructor call without parenthesis ";
It can also be used as an expression
if ((print "show value1") && (print "show value2"))
$var = print "Hello World"; //on successful print $var =1
$x ? print "true" : print "false";
Print_r:-
It uses to show the data/result of string, array and object as a human readable form. It’s best for
debugging the code.
Example:-
$a = array ('a' => 'ant', 'b' => 'bike', 'c' => array ('i', 'j', 'k'));
print_r ($a);
Result:-
<pre>
Array
(
[a] => ant
[b] => bike
[c] => Array
(
[0] => i
[1] => j
[2] => k
)
)
</pre>
If you want to capture the output of print_r rather than output of the data
then pass the second parameter with ‘True’ value
$var = array ('m' => 'mango', 'helo' => 'world', 'x' => array ('i', 'j', 'k')
);
$results = print_r($var , true); // $results now contains output from print_r
Var_dump:-
This function show all the information related to variable like value and type etc. The variable
may be a string, int, array, objects. This function has Void return type
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
Result:-
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
Note:-
Please provide your feedback regarding this tutorial. If you have any confusion or feel some thin wrong
then please let us know. We are here for you !!!!
var_dump. In this tutorial I am trying to elaborate the each function with suitable example.
Echo
The beauty o f Echo it not only uses as a function we can also use it as a constructor without
parenthesis.
Function example:-
echo ("echo function call");
In Echo when we pass the argument in parenthesis then it become function as show above in example.
The return type of Echo function is Void. It is a slightly faster than print function.
Constructor Example:-
If you want to pass more than one parameter then we can also use Echo but for this we should pass the
parameters without parenthesis.
Constructor Example:-
echo "construct 1 param","construct 2 param","construct 3 param";
In constructor we concatenate the parameter with semi colon “,”.
Echo has one more form from which we can out put the string. We can say it’s a shortcut to output the
data. For this it’s necessary to “short_open_tag” configuration option enabled. In short form you can
immediately follow the opening with equal sign.
Shortcut Example
<?=”it is a shortcut example ”?>
Print:-
Print can be also used as a function as well as a constructor (without parenthesis). But we can pass only
one parameter. Beauty of Print function/constructor is it returns the 1 on success.
Function Example:-
print("print function call");
Constructor Example:-
print "print constructor call without parenthesis ";
It can also be used as an expression
if ((print "show value1") && (print "show value2"))
$var = print "Hello World"; //on successful print $var =1
$x ? print "true" : print "false";
Print_r:-
It uses to show the data/result of string, array and object as a human readable form. It’s best for
debugging the code.
Example:-
$a = array ('a' => 'ant', 'b' => 'bike', 'c' => array ('i', 'j', 'k'));
print_r ($a);
Result:-
<pre>
Array
(
[a] => ant
[b] => bike
[c] => Array
(
[0] => i
[1] => j
[2] => k
)
)
</pre>
If you want to capture the output of print_r rather than output of the data
then pass the second parameter with ‘True’ value
$var = array ('m' => 'mango', 'helo' => 'world', 'x' => array ('i', 'j', 'k')
);
$results = print_r($var , true); // $results now contains output from print_r
Var_dump:-
This function show all the information related to variable like value and type etc. The variable
may be a string, int, array, objects. This function has Void return type
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
Result:-
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
Note:-
Please provide your feedback regarding this tutorial. If you have any confusion or feel some thin wrong
then please let us know. We are here for you !!!!
0 comments:
Post a Comment