Exist two variables $x=100 and $y=100. How I may insert its to div tag
<div style="position:absolute; left:100; top:100;">
to get something like this:
<div style="position:absolute; left:$x; top:$y;">
Thanks.
Code: Select all
<?php
$x = 100;
$y = 100;
echo "<div style=\"position:absolute; left:".$x."; top:".$y.";\">";
?>Code: Select all
<?php $x = 100;
$y = 100;?>
<div style="position:absolute; left:<?php echo $x;?>; top:<?php echo $y;?>;">
It's not often that we agree. But yes, how have people tolerated this mess for so long?
You don't need to concatenate the string and variables. The PHP interpreter will parse double quoted strings and expand variables.DougieLawson wrote: ↑Fri Aug 14, 2020 8:35 pmI'd probably build the whole <DIV> tag with a php echo/print command.
Code: Select all
<?php $x = 100; $y = 100; echo "<div style=\"position:absolute; left:".$x."; top:".$y.";\">"; ?>
Code: Select all
<?php
$x = 100;
$y = 100;
echo "<div style=\"position:absolute; left:$x; top:$y;\">";
?>Code: Select all
<div style="position:absolute; left:<?= $x ?>; top:<?= $y ?>;">That exactly explains everything that's wrong with PHP in a single sentence.
DougieLawson wrote: ↑Sat Aug 15, 2020 10:41 amThat exactly explains everything that's wrong with PHP in a single sentence.
After decades of programming in a dozen or more languages, a lot of which are now not only obsolete but unheard of, I have come down to two weapons of choice: