top image
Print(Echo) Statement
id: 4
Category: PHP
Rated: ---
Rate:



The print and echo commands are general the same thing. However, there are multipule ways to display data with either choice.

A simple code example:

<?php
print "Hello peoples";
?>

<?php This is starting the code. It's saying "I am now starting php!".

print "Hello peoples"; print is telling the browser 'I want you to display this'. The ; is ending the print statement. You could easily write echo instead of print. Both do the same thing. Quotes will be explained further in this tutorial.

?> Ends the php code.


In HTML or CSS which quotes you use generally mean nothing. In php however, which quotes you use can mean everything. Below will explain how each will have an effect.

Here is the example code I will use.

<?php
$var = "person";

print "Hello $var, <a href=\"link.html\">Link</a>";
?>

Double Quotes: " " Double quotes can display information exactly as its written. If I was to execute this php code, I would get this:

Hello person, Link



But why do I have \ before each double quote in the link tag? If we didn't add \ before each quote it would think that was where we were ending the print statement and then get an error.
Any time - regardless of the quote type you are using, if you have a quote in the print or echo statement that is the same quote you are using like above, you must add a \ before that quote!

Single Quotes: ' ' Single quotes don't allow you to use php variables in it. If we executed this code with single quotes in exchange for the double we'd get this:

Hello $var, Link

For this code to work we'd have to write it this way:

<?php
$var = "person";

print 'Hello' . $var . ', <a href="link.html">Link</a>';
?>



But why did I add a .? To break out of the print statement and add something like a variable in this code I need to use a period. It's saying this:
I want you to pring Hello, then add the variable to the statement and then return to the print statement and print the link tag.

If I wanted to only wite Hello people and no link code with single quotes I would write:

<?php
$var = "person";

print 'Hello' . $var;
?>

The print statement is a very simple and commonly used php code. The print statement is also no different from the echo command. They both do the same thing it is just a matter of personal preference. I hope this tutorial helped people out and if you have further questions feel free to contact me. If there are any errors or something you think should be in this tutorial also feel free to contact me.


Disclaimer

All contents are © Dice.
If credit for anything is missing please contact me and it will be added. If there is something you wish me to take off the site (that belongs to you) contact me. It will be removed immediatly.

Basic Rules

All tutorials on this site were written by me, Dice. They may not be redistributed without permission! Feel free to link to the videos.

I have used this site also for a project in college :3



home page

Contact Us

About Us