0

I trying to put user data from PHP in HTML form, like name, email, address, etc. But i don't know what i have to do to work it.

Already tried:

<?php

  session_start();
  $test='some name';
  $_SESSION['name']='some name';

?>

<a href="<?php $_SESSION['name']; ?>"></a>

<!-- AND -->

<a href="<?php $test; ?>"></a>

and nothing happened.

SOLVED CODE:

<?php

  session_start();
  $test='some name';

?>

<a><?= $test ?></a>

1
  • 2
    You need to output it. Try echo Commented Jan 14, 2022 at 17:37

2 Answers 2

1

The PHP manual has a page introducing the PHP tags:

When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them.

Note that "interpreting the code" is not the same as displaying the result of that code. For instance, you could write <?php $test = 'hello'; ?> to assign a new value to a variable, and it won't cause anything to be output.

To output a string, you can use the echo and print keywords. Note that these are not functions, so do not have parentheses around their arguments; as noted in the manual, including redundant parentheses will sometimes work, but can be misleading. So the following output the variable $test into the page:

<?php echo $test; ?>
<?php print $test; ?>

As the PHP tags page I linked to earlier says:

PHP includes a short echo tag <?= which is a short-hand to the more verbose <?php echo.

So you can also use:

<?= $test ?>

However, you also have a second problem, which is nothing to do with PHP, and will happen if you hard-code a URL into your HTML like this:

<a href="https://example.com"></a>

Nothing will appear in the browser! Why? Because you've defined the destination of a link, but haven't defined any text or content to click on; you need it to look like this:

<a href="https://example.com">Click me if you dare!</a>

So put that together, you might write this in PHP:

<a href="<?= $linkUrl ?>"><?= $linkDescription ?></a>
Sign up to request clarification or add additional context in comments.

1 Comment

So thanks for explication, now i know what error i did and how solve it <3
1

Change this

<a href="<?php $test; ?>"></a>

to this

<a href="<?php echo($test); ?>"></a>

or to this if you want to see the data on the html page

<a href="<?php $test; ?>"><?php $test; ?></a>
<a href="<?php echo($test); ?>"><?php echo($test); ?></a>

Do note that there are better ways to display such data. Check this out: https://www.w3schools.com/php/php_echo_print.asp

4 Comments

Exco does not need the brackets. a simple echo $var; is fine
Nothing happened yet, only appear when i put something inside like <a href="<?php echo($test); ?>"> aaa </a>i did something wrong?
Not only does echo not need parentheses, using them can cause confusion and bugs, so is generally a bad habit; see "Note: Using with parentheses" on php.net/echo The same is true for other "language constructs", like print, include, require, etc
Thanks for the parentheses info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.