"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Monday, June 11, 2018

output in php



Types of way of Display Statement in PHP:
There are two basic ways to get output in PHP:-
1.      echo
2.      print
1.PHP echo statement
In PHP ‘echo’ statement is a language construct and not a function, so it can be used without parenthesis. But we are allowed to use parenthesis with echo statement when we are using more than one arguments with it.
The end of echo statement is identified by the semi-colon (‘;’).
The echo statement are used to display the text and to display the variables.

We can use ‘echo’ to output strings or variables.
Below are some of the usage of echo statement in PHP:
i)Displaying Strings: We can simply use the keyword echo followed by the string to be displayed withing quotes. Below example shows how to display strings with PHP:
Example:
<?php
echo "Hello,This is a display string example!";
?>
Output:
     Hello,This is a display string example!
ii)Displaying Strings as multiple arguments: We can pass multiple string arguments to the echo statement instead of single string argument, separating them by comma (‘,’) operator. For example, if we have two strings say “Hello” and “World” then we can pass them as (“Hello”,”World”). Below example shows how to do this:
Example:
<?php
echo "Multiple ","argument ","string!";
?>
Output:
Multiple argument string! 
iii)Displaying Variables: Displaying variables with echo statement is also as easy as displaying normal strings. Below example shows different ways to display variables with the help of PHP echo statement:-  
Example:   
<?php
//defining the variables
$text = "Hello, World!"; 
$num1 = 10; 
$num2 = 20; 
//echo the variables
echo $text."\n"; 
echo $num1."+".$num2."="; 
echo $num1 + $num2;
?>

Output:
Hello,World!
10+20=30 
The (.) operator in the above code can be used to concatenate two strings in PHP and the “\n” is used for a new line and is also known as line-break. We will learn about these in further articles.

2)PHP print statement
The PHP print statement is similar to the echo statement and can be used alternative to echo at many times.It is also language construct and so we may not use parenthesis : print or print(). The main difference between the print and echo statement is that print statement can have only one agrument at a time and thus can print a single string. Also, print statement always returns a value 1.
Like echo, print statement can also be used to print strings and variables.
Below are some examples of using print statement in PHP:
i)Displaying String of Text: We can display strings with print statement in the same way we did with echo statements. The only difference is we can not display multiple strings separated by comma(,) with a single print statement. Below example shows how to display strings with the help of PHP print statement:-
Example:
<?php
//defining the variables
$text = "Hello, World!"; 
$num1 = 10; 
$num2 = 20; 
//echo the variables
echo $text."\n"; 
echo $num1."+".$num2."="; 
echo $num1 + $num2;
?>
Example:
<?php
print "Hello, world!";
?>
Output:
Hello,world!
ii)Displaying Variables: Displaying variables with print statement is also same as that of echo statement. The example below shows how to display variables with the help of PHP print statement:-Example:
<?php
//defining the variables
$text = "Hello, World!"; 
$num1 = 10; 
$num2 = 20; 
//echoing the variables
print $text."\n"; 
print $num1."+".$num2."="; 
print $num1 + $num2;
?>
Output:
Hello,World!
10+20=30

 Difference Between echo and print 


ECHO
PRINT
Type
This is a type of output string
This is a type of output string
Parenthesis
Not written with parenthesis
It can or can not be written with parenthesis
Strings
It can output one or more strings
It can output only one string at a time and returns 1
Functionality
Echo is faster than print
Print is slower than echo
Argument
Echo($arg1[,$arg2…])
Print($arg)



No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template