Difference between Echo and Print in PHPWhat is PHP?PHP stands for Hypertext Preprocessor. It is a multipurpose scripting language that is open-source for its users. It is used by developers for web development because it can be easily embedded into HTML. PHP is easy to understand and provides advanced features for professional users. A major advantage of PHP is that it is executed on the server, generating HTML, which is then sent to the client. The client can see the output by running the script without knowing the underlying code. PHP is a must for students and professional developers to become better Software Engineers, especially in Web Development Domain. Application Areas of PHP
What is a Statement in PHP?A statement can be anything, such as an assignment, loops, calling of a function, a conditional statement, etc. A statement always ends with a semicolon. There are so many functions available for displaying output in PHP. In this article, we will study print and echo functions for displaying output in PHP and also the difference between the two. The basic functions for displaying output in PHP are as follows:
What is an Echo Statement in PHP?An echo statement in PHP is a statement that is used to print the output on the screen. An echo statement can be used with as well as without parenthesis. An echo statement will always end with a semi-colon.
Output: Good Morning! Good Morning! 123 You can see in the above example that the echo statement uses both single as well as double quotes. In the above example, we used an echo statement without parenthesis. Output: Good Morning! HelloMorning! In the above example, we used an echo statement with parenthesis. Also, we can see that an echo statement can also be used to print a group of strings. Output: GoodMorning In the above example, we have concatenated two strings, thus echo can also be used for the concatenation of two strings. Output: 300 In the above example, we have used an echo statement to add two variables and print the result. Output: Good Morning In the above example, we have used an echo statement with HTML. HTML tags should be enclosed in double-quotes. Return type:The echo statement does not have any return type. Thus, no value is returned. What is the difference between using single quotes and double quotes?Single quotes are used to print the variable name and not its value whereas; double quotes are used to print the value of the variable. Here is an example which shows the difference between single quotes and double quotes. Output: sky is blue sky is $color What is a Print Statement in PHP?As the name suggests, the print statement is also used to print the output on the screen. Print statement can be used with as well as without parenthesis.
Output: Good Morning! Good Morning! 123; In the above examples, we can see how a print statement is used to print strings which is similar to the echo statement we studied earlier. Output: 300 In the above example, a print statement is used to add two variables and print the output(similar to an echo statement). Output: Good Morning In the above example, we can see how a print statement is used with HTML tags. Output: GoodMorning The above example shows how a print statement is used for concatenating two strings. Output: PHP Parse error: syntax error, unexpected ',' in line 2 In the above example, when we try to print multiple arguments together, the compiler throws an error. Thus, the print statement does not support multiple arguments, unlike the echo statement which allows the printing of multiple arguments and throws no error. Output: Good Morning.... What is your Roll No.? My Roll No. is : 12 4567.67 Return type of Print StatementThe print statement always returns an integer value that is, the print statement will always return 1. Difference between Print and Echo Statements in PHPSince both print and echo statements are used to display the output, is there any difference between the two or are they same? So the answer is, yes both statements are used to display the output but there are a few differences between the two.
Next TopicDifference between |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India