Echo, Print, die and Return
These are all similar in the fact that they display text if entered into them. That can be done like so:
echo 'text';
print 'text';
return 'text';
die 'text';
So what do these all do?
Echo is very similar to print with the way it acts but the only difference is that it returns text and a value of NULL / FALSE.
eg1
<?php
function Test()
{
echo 'This is a test';
}
$tryTest = Test(); //Define a variable to a function
if( $tryTest )//If there is something in the variable
{
echo '(Y)';//Show a done message
}
else//If not
{
echo '(N)';//Show a not done message
}
?>For this example you would be shown (N) as echo has displayed the text but hasnt told the script that is has done so, therefore thinking nothing has been shown.
Print on the other hand does return TRUE so if we replace all our echo's with print we would get (Y).
eg2
<?php
function Test()
{
print 'This is a test';
}
$tryTest = Test(); //Define a variable to a function
if( $tryTest )//If there is something in the variable
{
print '(Y)';//Show a done message
}
else//If not
{
print '(N)';//Show a not done message
}
?>Now we have gone through the difference between print and echo we are left with return and die...
Return is used mainly in functions when you want to send text but not display it yet. Return also means that the script stops because it is used when you have gotten to the end of an algarithm...
eg3
<?php
function Test()//A new function called test
{
$q = b;
if( 'y' == 'n' )
{
return 'Done';
}
elseif( 't' != $q )
{
return 'Done';
}
else
{
return FALSE;
}
}//End function
if( Test() )
{
print 'This won\'t be shown as the function Test has returned a negative value ';
}
?>Now you maybe wondering what return FALSE is doing...
With return you can return a value without any text involved, so return FALSE; ends the script but it doesnt return a value so if assigned to a variable it wouldnt work; as shown in the above example.
Die must be the most dramatic of all php predefined functions all it basically does is end the script and return a negative value. It is used like so:
eg4
die('Test');or just
die 'Test';
Quotation marks
Now we can move onto the difference between quotation marks and dashes.
When using php with MySQL you may see 3 different marks like so:
`
'
"
The first one ` is used in MySQL to wrap arround table names and field names (it is found on the top left of your keyboard). It is used like so:
SELECT `id`, `name`, `delete` FROM `tablename`;
The above statement would work but in MySQL this would not work
SELECT 'id', 'name', 'delete' FROM 'tablename';
The above would not work as it is telling MySQL that the values are strings relating to field such as
`name` = 'frank';
In PHP the single quotation mark ' is used to display text and only text.
eg5
<?php print 'This is text'; //This would simply show: //This is text $fruit = 'apples' print 'I love my $fruit'; //This would show: //I love my $fruit //The variable is treated as text so is not replaced by the string assigned to it //To make the variable work you can do 2 things... print 'I love my ' . $fruit; //This would show: //I love my apples //The above print function ends the text string by adding the second single quotation //But the print function is not finished untill you add; so you can add a . and the variable will be added after the string infront of it. ?>
Another way to display the variable is to use double quotations, this treats everything as a variable so takes abit longer to render the text (it still works but just abit slower). You can use the double quotations like so...
eg6
<?php $fruit = 'oranges'; print "I love to eat $fruit all the time"; //This will display //I love to eat oranges all the time //A bad thing with this is that $fruit won't be highlighted in the variable colour so it will be harder on the eye to spot in long lines of code so you can do print "I love to eat ".$fruit." all the time"; //This will display //I love to eat oranges all the time ?>
You maybe wondering what happens when you need to use that quotation mark inside itself, this is done like so:
eg7
print "gold killer said: \"Hi\" "; //This would display - gold killer said: "Hi" print 'gold killer said: \"Hi\" '; //This would display - gold killer said: \"Hi\" - This is because the double quotations can be used normaly inside single quotations and visa versa, like so: print "gold killer said: 'Hi' "; //This would display - gold killer said: 'Hi' print 'gold killer said: 'Hi'; //This would not work as the single quotations need a \ infront of them like so: print 'gold killer said: \'Hi\'\; //This would work and would display - gold killer said: 'Hi'
Now you could be thinking thats going to be a pain when displaying big chunks of xhtml, PHP 5 has a simple sollution like below:
eg8
<?php echo<<<END <form method="post" action="test.php"> Forename: <input type="text" name="fname" /><br /> Surname: <input type='text' name='sname' /><br /> <input type="submit" value='GO!' /> </form> END; ?>
Thats the simple and easiest way of displaying big chunks of html in PHP 5. In PHP 4 you can do that like so:
eg9
<?php //PHP here ?> <form method="post" action="test.php"> Forename: <input type="text" name="fname" /><br /> Surname: <input type='text' name='sname' /><br /> <input type="submit" value='GO!' /> </form> <?php //PHP here ?>
But one problem with that is that you can't display PHP variables...
You may think about starting it all up again and doing <?php echo $var; ?>
You maybe thinking, surley php would not be this dumb... its not! Below is the quick echo:
eg10
<?php $var = 'GO!';//Define our variable ?> <form method="post" action="test.php"> Forename: <input type="text" name="fname" /><br /> Surname: <input type='text' name='sname' /><br /> <input type="submit" value="<?=$var?>" /> </form> <?php ?>
So to do a quick echo all you need is
<?=$variableName?>
or
<?=$variableName;?>
\t \s \n
These are used as breaks in the code as php does not automatically add line breaks in your code when using echo or print so to do this you can use
\n
This means when you view source there will be a new line started there!
\t Is a tab value so when added the source gets tabbed accross
\s this is the same as in html!
Below is all of these brought together:
eg11
<?php print ' \t<table>\n \t \t<tr>\n \t \t \t<td>\s</td>\n \t \t</tr>\n \t</table>\n '; ?>
This would display:
<table> <tr> <td> </td> </tr> </table>
Thanks for reading, if you need any help just post
Help




















