Welcome Guest ( Log In | Register)



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Php Echo, Learn what the echo can do for you. Includes basic echo and variable p
KansukeKojima
post Nov 14 2007, 11:13 PM
Post #1


Privileged Member
*********

Group: [HOSTED]
Posts: 528
Joined: 13-October 06
From: Alberta, Canada
Member No.: 31,584



PHP Echo

Description
The echo() command has one purpose, displaying whatever you put in between the brackets. Learn to use it to your advantage.

Try It Out
CODE

<?php echo ""; ?>


There is your echo command. For now, it does nothing. It has no value to display.


CODE
<?php echo "This is my text."; ?>

Outcome
This is my text.

Now this echo command has a value to display. Whatever you replace "This is my text." with will be displayed.

Alright, lets move onto the next step. This time, we will include a variable in the echo command.


CODE
<?php
$var = "This is a variable";
echo "$var. Congratualations, you just parsed a variable in an echo!";
?>

Outcome
This is a variable. Congratualations, you just parsed a variable in an echo!

Got it? The variable ($var) had a value, and when you placed $var in the echo command, it was displayed. Sounds like a different language, right? Its ok if you don't understand yet, with some practice you will eventually understand it.

These are the basics of the echo() comand. Not much to it if you are a beginner, but as you progress, there is much more.
Go to the top of the page
 
+Quote Post
hippiman
post Nov 15 2007, 02:42 AM
Post #2


Premium Member
********

Group: Members
Posts: 150
Joined: 9-April 07
From: Nebraska
Member No.: 41,301



This is something that could help if you actually have more HTML on your page than PHP.

The echo tag is pretty much exactly like typing in HTML outside of the php tags, so you can also just end the php, and start it and end it whenever you have variables again or something.

CODE
<?php
$var = "Someone's Name";
?>
Some HTML that you want to do.
Some more HTML that has <?php $var; ?>.

That should display:
CODE
Some HTML that you want to do.
Some more HTML that has Someone's Name.


You might have to put <?php echo $var; ?> instead of <?php $var ?>
But I'm not really sure. Just try it, and it should work. At leas one of them will.
That would only be better if you have a bunch of HTML and only a little PHP code.

You could also use that to copy HTML over and over again:
CODE
<?php
for($i=0;$i<100;$i++) {
?>
some HTML that you want to loop 100 times.
<?php
}
?>


I think I've tried that before, and it worked. If I have any errors in that, feel free to correct me.
I hope this helps!
Go to the top of the page
 
+Quote Post
suberatu
post Nov 15 2007, 04:34 AM
Post #3


Advanced Member
*******

Group: Members
Posts: 132
Joined: 23-September 07
Member No.: 50,511



I don't have any experience with php, but at least now I know how to output basic text (or variable text) to the screen. Thanks for the brief, but useful tut.
Go to the top of the page
 
+Quote Post
Saint_Michael
post Nov 15 2007, 04:54 AM
Post #4


$p4m 0n j00 $h4m3 m3 0nc3 $p4m 0n m3 $h4m3 m3 7\/\/1c3
*********************

Group: [HOSTED]
Posts: 6,444
Joined: 21-September 04
From: 9r33|\| 399$ 4|\|D 5P4/\/\
Member No.: 1,218
T17 GFX Crew



Echo's are a great way to start off in learning php as it will help you understand how variables will work and when you start working with php includes when designing full scripts.
Go to the top of the page
 
+Quote Post
pop
post Nov 15 2007, 11:54 AM
Post #5


Advanced Member
*******

Group: Members
Posts: 102
Joined: 13-October 07
Member No.: 51,530



instead of echo sometimes it's usefull to use printf, like in C language.

$value1 = "my test var";
$value2 = 200;

printf ("%s has size of %d", $value1, $value2);
Go to the top of the page
 
+Quote Post
Tetraca
post Nov 15 2007, 12:23 PM
Post #6


Privileged Member
*********

Group: Members
Posts: 628
Joined: 20-May 06
Member No.: 23,968



The different types of quotes are equally important as well. Without them you'd have to sit there trying to print values in ASCII character codes you wanted to see.
CODE
echo "These basic quotation marks allow you to use the apostrophe and are useful for basic text.";
echo 'These single quotes allow you to use standard quotemarks and are useful for dispensing HTML code';
echo `These grave accents allow you to put both single and double quotes`;

They work for variables in this language as well. I end up using them a lot within my code. I don't often end up using the printf function, though. It reminds me of C and why I dislike it.
Go to the top of the page
 
+Quote Post
KansukeKojima
post Nov 15 2007, 03:29 PM
Post #7


Privileged Member
*********

Group: [HOSTED]
Posts: 528
Joined: 13-October 06
From: Alberta, Canada
Member No.: 31,584



One thing that I have recently learned and that I will be using for my website once I remake it (I've shut it down for now) is using echo to help create a sweet layout... bassically program all your variables at the top of the page, code them into another variable called template and then echo the template variable... I estimate that it will work quite nice because you can make large changes easily and quickly... and I think this method would work quite well.

EDITED IN AFTER POST:
It would look like this

CODE
<?php
//--------------------------
//Content itself
//--------------------------
$content[1] = <<< html
blablablabla
html;
$content[2] = <<< html
blablabla again
html;
//------------------------
//layout design with content in it
//------------------------
$template = <<< html
<table width=500>
<td>
$content[1]</td>
<td>
$content[2]</td>
html;
//--------------------
//show the page
//--------------------
echo "$template";
?>


This post has been edited by KansukeKojima: Nov 15 2007, 07:20 PM
Go to the top of the page
 
+Quote Post
OneMinute
post Nov 16 2007, 02:05 AM
Post #8


Member [Level 2]
*****

Group: [HOSTED]
Posts: 80
Joined: 18-October 07
Member No.: 51,724



In PHP, we can also use Print? (Yes, indeed) But it's just a matter of preferences however. The print is also setted to display the output. Please correct me if i am wrong; i just started learning php.
Go to the top of the page
 
+Quote Post
coolcat50
post Nov 16 2007, 09:09 PM
Post #9


Super Member
*********

Group: Members
Posts: 290
Joined: 5-October 07
From: Random Places
Member No.: 51,171
Spam Patrol



You should have also provided a few other tricks with echo. Such as random numbers or something like that.
Look at this code.
CODE
<?php
$var1="Hello people!";
$var2=str_replace("people","world",$var1);
echo $var1." and ".$var2;
?>


The above code shou