lightning_missile
lightning_missile

Reputation: 2992

Use of echo in PHP

Consider:

$name = "algs";
<input type="text" name="name" value="<?php echo $name?>">

Now, I am extremely new to PHP, and I'm extremely confused by the code above: I understand that echo is used to output strings according to the manual. But why do we have to do

value="<?php echo $name?>"

and not just

value="<?php $name?>" //if I only want to put it inside a textfield

I thought it just output strings. Based on the result I think it's returning it. The first time I read this I thought php is doing like:

value="std::cout $name << '\n' ?>" //just an example

Can anyone explain this? Why do we have to use echo if we only want to store the variable's value to another variable?

Upvotes: 2

Views: 142

Answers (4)

user3593520
user3593520

Reputation: 141

I think this is important because I think this is a common thing about php that is not well understood when people first learn it. The thing to get is that the html and the php are basically independent. One executes and completes entirely before the next happens:

The html code is being processed by the client's computer. The php is being executed at the server. These two items do not actually interact in the way you are thinking. Consider a normal, static html file at the server. This gets sent to the clients computer and then is rendered by chrome or another browser. What php is doing is pre-processing the html file. In a way you can think of it as custom edits being made to the html file before it gets sent.

So, basically PHP executes, prepares the html, and then it is given to the browser to be rendered. PHP runs, then stops running, then gives the html to the browser. The php variable $name no longer exists by the time the browser has its turn. And, they are usually two seperate computers anyway.

So you have to echo the variable into the html. This technique can be used with javascript as well.

Upvotes: 1

holyknight
holyknight

Reputation: 315

That's because $name is only the variable, you are not telling php to do nothing with it. With echo you tell php to print the value of $name in a html output.

So if the value of $name is "kevin"

<input type="text" name="name" value="<?php echo $name?>"> 

the output will be

<input type="text" name="name" value="kevin">

Otherwise, if you dont write the "echo":

<input type="text" name="name" value="<?php $name?>"> 

will output

<input type="text" name="name" value=""> 

Upvotes: 1

user7000326
user7000326

Reputation:

Since you seem to know a little bit of c++ here is my explanation:

<?php $name = 'Test'; $name ?>

Is equivalent to

std::string name = "Test"; name;

This doesn't output any string does it? This only defines a variable but never prints its content to the screen.

But what you are looking for is:

<?php echo $name ?> or <?= $name ?> (both are the same)

which is equivalent to

std::cout << name;

This prints the variable content to the screen or in your case inside your html.

Upvotes: 5

Ohgodwhy
Ohgodwhy

Reputation: 50767

You need to tell the PHP interpreter that you wish to echo out the variable. It does not assume naturally that the variable is to be written to the output buffer.

In this instance, you are not storing the value of one variable as the value of another variable. You are writing HTML code (to the output buffer), and you also wish to bring along the value of a variable and write it to the output buffer, which requires the usage of echo (or print).

Upvotes: 3

Related Questions