Home > database >  PHP: display value of PHP variables inside a string
PHP: display value of PHP variables inside a string

Time:03-12

so I have this code and I need to parse the PHP variables inside the string. the $string here is a response from an API and I have no control over the output. I need to echo it out and display the value of the variables inside the string to an HTML page.

<?php
    $product = "Test Product";
    $lot_number = "123123123";
    
    $string = '<table style="width: 100.378%;" border="\">
    <tbody>
    <tr>
    <td style="width: 10.4834%;"><strong>Product</strong></td>
    <td style="width: 35.6575%;">$lot_number</td>
    <td style="width: 11.9257%;"><strong>Lot No.</strong></td>
    <td style="width: 41.9335%;">$lot_number</td>
    </tr>
    </tbody>
    </table>';

    echo "$string";

?>

This is the output of this code

enter image description here

and this is the output I am trying to achieve.

enter image description here

Any idea how to do this? Thank you!

CodePudding user response:

Make the first single quote ' to double " and vice versa.

<?php
    $product = "Test Product";
    $lot_number = "123123123";
    
    $string = "<table style='width: 100.378%;' border='\'>
    <tbody>
    <tr>
    <td style='width: 10.4834%;'><strong>Product</strong></td>
    <td style='width: 35.6575%;''> $product </td>
    <td style='width: 11.9257%;'><strong>Lot No.</strong></td>
    <td style='width: 41.9335%;''> $lot_number</td>
    </tr>
    </tbody>
    </table>";

    echo $string;

?>

CodePudding user response:

Multiple ways to do it. The simplest way in your current case would be to close and reopen the parts where you will print the php variables. Like "abc " . $var . " def";


<?php
    $product = "Test Product";
    $lot_number = "123123123";
    
    $string = '<table style="width: 100.378%;" border="\">
    <tbody>
    <tr>
    <td style="width: 10.4834%;"><strong>Product</strong></td>
    <td style="width: 35.6575%;">'. $product . '</td>
    <td style="width: 11.9257%;"><strong>Lot No.</strong></td>
    <td style="width: 41.9335%;">' . $lot_number .'</td>
    </tr>
    </tbody>
    </table>';

    echo "$string";

?>

UPDATE

You can then use the str_replace (https://www.php.net/manual/de/function.str-replace.php) function to fill the variables.

<?php
    $product = "Test Product";
    $lot_number = "123123123";
    
    $string = '<table style="width: 100.378%;" border="\">
    <tbody>
    <tr>
    <td style="width: 10.4834%;"><strong>Product</strong></td>
    <td style="width: 35.6575%;"> $product </td>
    <td style="width: 11.9257%;"><strong>Lot No.</strong></td>
    <td style="width: 41.9335%;"> $lot_number </td>
    </tr>
    </tbody>
    </table>';

    #echo "$string";
    


$string = str_replace('$product', $product, $string);
$string = str_replace('$lot_number' , $lot_number , $string);
echo $string;

CodePudding user response:

If you have only two variables just use this code:

<?php

$product = "Test Product";
$lot_number = "123123123";

$stringFromApi = '<table style="width: 100.378%;">
    <tbody>
        <tr>
            <td style="width: 10.4834%;"><strong>Product</strong></td>
            <td style="width: 35.6575%;">$lot_number</td>
            <td style="width: 11.9257%;"><strong>Lot No.</strong></td>
            <td style="width: 41.9335%;">$lot_number</td>
        </tr>
    </tbody>
</table>';

$search = ['$product', '$lot_number'];
$replace = [$product, $lot_number];
$string = str_replace($search, $replace, $stringFromApi);
echo $string;

CodePudding user response:

Since you use single quotes you cannot add variables inside the string unless you use a curly brace or a dot.

Note: using the double quotations method is faster than using the using curly braces or a dot.

If you want to echo a variable and use a single quoted string in php you have to "glue" the parts together.

There are 2 ways to do this, they are both equally as fast and it's just a matter of personal preference what you want to use.

<?php

//Double quotations
$car = "mercedes";
echo "the car brand is a $car<br>";
$type = "Question";
echo "Hi! I just posted a {$type}<br>";

//Single quotations
$name = "StackOverFlow";
echo 'Hi! my name is: ' . $name; 
?>

CodePudding user response:

It works for you. There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments.

<?php
        $product = "Test Product";
        $lot_number = "123123123";
        
        $string = '<table style="width: 100.378%;" border="\">
        <tbody>
        <tr>
        <td style="width: 10.4834%;"><strong>Product</strong></td>
        <td style="width: 35.6575%;">'.$product.'</td>
        <td style="width: 11.9257%;"><strong>Lot No.</strong></td>
        <td style="width: 41.9335%;">'.$lot_number.'</td>
        </tr>
        </tbody>
        </table>';
    
        echo $string;
    
    ?>

For more information: https://www.php.net/manual/en/language.operators.string.php. Example

<?php
$a = '12345';

// This works:
echo "qwe{$a}rty"; // qwe12345rty, using braces
echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used

// Does not work:
echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
echo "qwe$arty"; // qwe, because $a became $arty, which is undefined
?>
  •  Tags:  
  • php
  • Related