I need a php variable to be placed in a href link

0

The variable $fa has to be placed inside the link, but I can not do that

This variable takes a data from the bank, is inside the php code

$fa=$aux["celular"];

<a href = "https://///////////phone=$fa&text="> CLIQUE</a>
                    
asked by anonymous 13.07.2018 / 16:46

2 answers

1

Just interpolate strings :

echo "<a href='http://...?phone={$phone}'></a>";

Notice the use of {$phone} within the string , which will be replaced by the value of the $phone variable.

But there are other considerations to be made. For example, what would happen if the $phone value has the & character, type "0000 & 0000"? The output generated would be:

<a href='http://...?phone=0000&0000'></a>

So, the value of phone , in the URL, would be "0000 & 0000" or just "0000"? To avoid unexpected behaviors, you need to encode the value before sending it to the URL:

$phone = urlencode($phone);
echo "<a href='http://...?phone={$phone}'></a>";

Well, this way, the output would be:

<a href='http://...?phone=0000%260000'></a>

Note that the & character has been encoded for %26 and thus the phone value in the URL will be exactly "0000 & 0000" as expected.

If the HTML element is not the result of a PHP expression, you can use <?= ?> to see the value of the variable:

<a href='http://...?phone=<?= $phone ?>'></a>

That would be equivalent to doing <?php echo $phone; ?> , but in a simpler way.

    
13.07.2018 / 17:47
0

If you are putting the html echoing in php, consider doing this by concatenating:

<?php
   $fa=$aux["celular"];
   echo '<a href = "https://///////////phone='.$fa.'&text=">CLIQUE</a>';
?>
    
13.07.2018 / 17:38