Page 1 of 1

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

Posted: Wed Sep 01, 2004 12:13 am
by battye
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

Posted: Wed Sep 01, 2004 12:22 am
by StorbinC
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 />';
?>

Posted: Wed Sep 01, 2004 12:23 am
by StorbinC
ok i guess not

Posted: Wed Sep 01, 2004 12:27 am
by quicksilver
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) .

Posted: Wed Sep 01, 2004 12:32 am
by battye
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:

Posted: Wed Sep 01, 2004 12:33 am
by battye
btw i just wrote it today, so u cant have seen it before :?

Posted: Wed Sep 01, 2004 3:10 am
by Grinch
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