PHP: How to echo (Can you hear me?)

All web design discussion, including Ultimate Quiz MOD support.

Moderator: CricketMX Forum Moderators

Post Reply
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

After commenting (I'll write one about it later) one of the most useful functions is 'echo'

Basically you write a variable. Variable names must be in this format:
(Dollarsign)(Alpha-numeric characters)

So a valid variable name would be:

Code: Select all

$vari
Invalid would be

Code: Select all

vari
Because it is missing the dollar sign, to end the variable, you must tell PHP what it's supposed to resemble, so you could use a number:

Code: Select all

$vari = 1
Or a word:

Code: Select all

$vari = Hey there
Never forget to end a line of code with a semicolon!! ( ; )

If you forget, you will get an error along the lines of:
Parse Error: Parse error on line xx

So a valid line would be:

Code: Select all

$vari = Hey There;
To start and end a section of PHP code, you must use 2 codes. To start a section:

Code: Select all

<?php
And to end:

Code: Select all

?>
Now on to the echoing. Echoing is very simple, it simply prints and displays a variable or variables that you choose. You can use HTML in them. To start an echo, you write

Code: Select all

echo
Simple, then:

Code: Select all

echo 'Put your text in here';
OR

Code: Select all

echo $vari;
If you're simply echoing a variable you don't need the ' & '.
An example of good code, from top to bottom:
This PHP Script wrote: <?php <<< Start PHP code
$forum_age = 8; <<< Variable

echo 'This forum has been up for '; <<< Note the space between for and '
echo $forum_age; <<< Variable, no '' needed
echo ' '; <<< Space to go after the variable
echo 'months'; <<< Just a word, need ''
echo '<br />'; <<< HTML can be used
?> <<< End PHP Code
Of course you don't include the text which I have highlighted in italic, that will just confuse PHP and throw up a parse error :P
If you want to see it without the italics:
This PHP Script wrote:<?php
$forum_age = 8;

echo 'This forum has been up for ';
echo $forum_age;
echo ' ';
echo 'months';
echo '<br />';
?>
The final product would be:

This forum has been up for 8 months

Used in this example, it's not as effective as HTML but when you start getting SQL (Database) queries they become very useful :D
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
User avatar
StorbinC
Nice Guy
Nice Guy
Posts: 2304
Joined: Sun May 23, 2004 12:43 pm
Location: Sometimes here & sometimes there but never near
Contact:

ok i copied the text above to see if it really works, I saw this raw code on another of Batt's posts and was wondering what was up with it.

I guess maybe he has it working now???

<?php
$forum_age = 8;

echo 'This forum has been up for ';
echo $forum_age;
echo ' ';
echo 'months';
echo '<br />';
?>
User avatar
StorbinC
Nice Guy
Nice Guy
Posts: 2304
Joined: Sun May 23, 2004 12:43 pm
Location: Sometimes here & sometimes there but never near
Contact:

ok i guess not
sometime u feel like a nut
ImageImage
sometimes you don't
Image
quicksilver
Helpful Hands
Helpful Hands
Posts: 1926
Joined: Mon Mar 22, 2004 12:12 am

Hmm interesting Battye, I hope to see more input from other contributors as well to show style differences in the layout .
Its a good start to the month ,well done 8) .
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

StorbinC wrote:ok i copied the text above to see if it really works, I saw this raw code on another of Batt's posts and was wondering what was up with it.

I guess maybe he has it working now???

<?php
$forum_age = 8;

echo 'This forum has been up for ';
echo $forum_age;
echo ' ';
echo 'months';
echo '<br />';
?>
You need to upload it to a PHP enabled server for it to work, example @ http://www.cricketmx.com/testforum/dontuseme.php :wink:
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

btw i just wrote it today, so u cant have seen it before :?
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
User avatar
Grinch
Site Admin
Site Admin
Posts: 2170
Joined: Sat Jan 24, 2004 2:02 pm
Location: Martinsburg, WV

Just to add, php is like html as in you can write you code in notepad or wordpad. When your done with the code you would save it as:

Code: Select all

echo.php
Upload it to your php enabled server and then access the page.

Code: Select all

www.yourdomain.com/echo.php
Post Reply