Jump to content
xisto Community
Sign in to follow this  
KansukeKojima

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

Recommended Posts

PHP EchoDescriptionThe echo() command has one purpose, displaying whatever you put in between the brackets. Learn to use it to your advantage.Try It Out

<?php echo ""; ?>

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

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

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

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

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

Share this post


Link to post
Share on other sites

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.

<?php$var = "Someone's Name";?>Some HTML that you want to do.Some more HTML that has <?php $var; ?>.
That should display:
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:
<?phpfor($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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

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.

Share this post


Link to post
Share on other sites

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

 

<?php//--------------------------//Content itself//--------------------------$content[1] = <<< htmlblablablablahtml;$content[2] = <<< htmlblablabla againhtml;//------------------------//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";?>
Edited by KansukeKojima (see edit history)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

You should have also provided a few other tricks with echo. Such as random numbers or something like that.
Look at this code.

<?php$var1="Hello people!";$var2=str_replace("people","world",$var1);echo $var1." and ".$var2;?>

The above code should display.
Hello people! and Hello world!

So echo can actually be used for several purposes.
Edited by coolcat50 (see edit history)

Share this post


Link to post
Share on other sites

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

<?php$text[1] = "Hey ma whats for dinner";$text[2] = "Go up yo nose and picka winna!";$text[3] = "EWWWwww/admin/;;$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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.