id: 4
Category: PHP
Rated: ---
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";
?>
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>";
?>
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;
?>


