Php Echo - Learn what the echo can do for you. Includes basic echo and variable p

Pages: 1, 2
free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

Php Echo - Learn what the echo can do for you. Includes basic echo and variable p

KansukeKojima
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.

 

 

 


Reply

hippiman
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!

 

 

 


Reply

suberatu
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.

Reply

Saint_Michael
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.

Reply

pop
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);

Reply

Tetraca
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.

Reply

KansukeKojima
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";
?>

Reply

OneMinute
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.

Reply

coolcat50
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 should display.
CODE
Hello people! and Hello world!


So echo can actually be used for several purposes.

Reply

KansukeKojima
More on the note of using the magical skill of parseing variables in echos, here is dandy fine random title/subtitle/whatever code.


CODE

<?php
$text[1] = "Hey ma whats for dinner";
$text[2] = "Go up yo nose and picka winna!";
$text[3] = "EWWWWWW......";

$randomize = rand[1, 3];
echo "$text[$randomize]";
?>


Get it, you have three different options, you create a variable called randomize and use the rand function to select from the previous array. The you echo the text variable and the randomize variable as shown above.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Pages: 1, 2

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for php, echo, learn, echo, includes, basic, echo, variable, p

*MORE FROM TRAP17.COM*
advertisement



Php Echo - Learn what the echo can do for you. Includes basic echo and variable p



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE