PDA

View Full Version : PHP Echo Tutorial



Rhys
28-01-2010, 11:30 PM
one of the first ever things you will/should learn is Echo in php

its extremely simple once you get to know it and easy to learn, but theres a few bits which you need to learn.

---

Right now to show the most simple form of an echo.


<?PHP
echo "This will be shown on the page";
?>
Now in a little detail

<?PHP
This will open the php script

echo "This will be shown on the page";
echo tells the script to show what is put in "..". its like the body of a html file but put into php. ; will tell the script to go onto the next line or variable.


?>
this closes the php script.

now thats the easiest thing you could ever do and it'll show just the text youve put in between "..".

Now to get onto the more complicated bits and to make the text formated.

Now if u wanted you could simply do this by adding a css bit like

<div
style="
font-family: Verdana;
font-size: 12px;
">

now its not always that easy to implament it, if you showed it like this in the php it wouldnt work.

<?PHP
echo "<div style="font-family: Verdana; font-size: 12px;">This will be shown on the page</div>";
?>
No ofcourse php made it all complicated.
If you open and echo tag with ", it must not have anouther in the way, this will make php think that the code has ended and the rest of the script will not be included untill it is opened again. It can also be done with '..' . Its confusing and will make you think but i believe most should get used to it after a while.

For it to work it should be outputted like this

<?PHP
echo "<div style=\"font-family: Verdana; font-size: 12px;\">This will be shown on the page</div>";
?>
OR

<?PHP
echo "<div style='font-family: Verdana; font-size: 12px;'>This will be shown on the page</div>";
?>---

Have a little practice and get used to using php and having to use \'s and then finally then we can get onto using strings

---

Strings are good if u want to write out something but dont want to show it till later.
Strings use $'s. after the $'s they have a name. lets call ours $show1 and $show2.
Showing both is a piece of cake.

<?PHP
$show1 = "<b>This is the first line</b><br />";
$show2 = "This is the second line";
echo "Read below<br /><br />";
echo $show1;
echo $show2;
?>
That will show what you put in the string show1 and show2 + above it Read Below.

lets say you put something important in $show1 but dont wana show it and put a date till u show it on $show2 then u simply dont echo $show1. and when u want to. only echo $show1 and not $show2.

I think you may be a little confuzed atm but make yourself a few php pages and go threw trying a few techniques with the normal echoing onto the string echoing and it should be a doddle.

Ill do anouther tut soon on php.

Hope you learnt something new and any q's, just ask :D
I wrote this along time ago, well back in feb last year. If u do get a bit confused with how i wrote it (since i was just starting out then) then do feel free to comment

Ian.H
28-01-2010, 11:50 PM
Actually, to be more efficient, use single quotes:


<?php echo '<strong class="foo">' . $bar . '</strong>'; ?>

for example.

Reason for this is single quotes are literal strings, double-quotes get parsed as non-literal strings, meaning they get checked to see if there's any variables within them that need to be parsed as well as whatever static text there is.

A bit anal maybe, especially in this day and age of hardware speeds, but if nothing else, it's a **** sight easier to read and type than lots of lines looking like:


<?php echo "<strong class=\"foo\">$bar</strong>";

IMO, anyway :)



Regards,

Ian

Rhys
28-01-2010, 11:53 PM
very true.
it was mainly for starters and i have missed out some bits which u pointed out above.

its been nearlly a year since i wrote it and that was when i was a complete nub.
ive gone past just using echos and have gotten into coding in OOP and atm am contemplating releasing my news script ive recently finished with a full admin with userlevels categories comments etc etc.

Ian.H
29-01-2010, 12:05 AM
Cool. I realise it was aimed at the beginner, and my code was far from the best when I started with PHP 10 years ago. Certainly don't want to discourage you :)

Just make sure that your news script is secure (in particular, SQL injections) and release away.. code / project sharing == a great thing™ IMO :D


Regards,

Ian

Naphta
29-01-2010, 12:27 AM
Tizag for all things like this. Its very very good :)

adammark
29-01-2010, 04:18 AM
Hi Dear.

This is really nice tutorial for all who are beginner in php.
This is best way to spread out this information. keep it up and post the latest information regarding php. Thank you.

Rhys
29-01-2010, 04:35 PM
Just make sure that your news script is secure (in particular, SQL injections) and release away.. code / project sharing == a great thing™ IMO :D


Regards,

Ian

ive cut out all ways of injection by using html entities + i dont use cookies so no cookie stealers.
ill release it once i add a few more bits since ive thought up a few more.
Its been in the works since august so hopefully should be fine.