Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolate a constant (not variable) into 'heredoc'

Consider:

<?php    define('my_const', 100);    echo <<<MYECHO       <p>The value of my_const is {my_const}.</p> MYECHO; ?> 

If I put a variable inside the braces, it prints out. But not the constant. How can I do it?

like image 525
Michael Avatar asked Apr 06 '12 08:04

Michael


People also ask

What is heredoc give example?

Heredoc's are equivalent to a double quoted string. That means any variables in the string will get substitued for their respective values. We could rewrite the double quoted string example above as a heredoc:- $foo = 'bar'; echo <<<EOT Hello $foo Goodbye! EOT; // Output:- // Hello bar // Goodbye!

What do you meant by interpolating variables in double quoted strings?

When you define a string literal using double quotes or a heredoc, the string is subject to variable interpolation. Interpolation is the process of replacing variable names in the string with the values of those variables.

What is heredoc used for?

A here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor. Choose a limit string sufficiently unusual that it will not occur anywhere in the command list and confuse matters.


2 Answers

You can also approach the problem by assigning the value of the constant to a variable.

Personally I do it that way because if you have lots of constants in your string then your sprintf() call can be quite messy. It's also then harder to scan through the string and see what is doing what. Plus, by assigning the variables individually, you can see what is taking on what value.

An example would be:

$const = CONST; $variable = VARIABLE; $foo = (new Foo)->setFooProperty(12)->getFooProperty(); $bar = (123 - 456) * 10; $ten = 1 + 2 + 1 + (5 - 4); <<<EOD Lorem ipsum dolor sit amet, **$variable** adipiscing elit. Duis gravida aliquet dolor quis gravida. Nullam viverra urna a velit laoreet, et ultrices purus condimentum. Ut risus tortor, facilisis sed porta eget, semper a augue. Sed adipiscing erat non sapien commodo volutpat. Vestibulum nec lectus sed elit dictum accumsan vel adipiscing libero. **$const** vehicula molestie sapien. Ut fermentum quis risus ut pellentesque. Proin in dignissim erat, eget molestie lorem. Mauris pretium aliquam eleifend. **$foo** vitae sagittis dolor, quis sollicitudin leo. Etiam congue odio sit amet sodales aliquet. Etiam elementum auctor tellus, quis pharetra leo congue at. Maecenas sit amet ultricies neque. Nulla luctus enim libero, eget elementum tellus suscipit eu. Suspendisse tincidunt arcu at arcu molestie, a consequat velit elementum. Ut et libero purus. Sed et magna vel elit luctus rhoncus. Praesent dapibus consectetur tortor, vel **$bar** mauris ultrices id. Mauris pulvinar nulla vitae ligula iaculis ornare. Praesent posuere scelerisque ligula, id tincidunt metus sodales congue. Curabitur lectus urna, porta sed molestie ut, mollis vitae libero. Vivamus vulputate congue **$ten**. EOD; 
like image 52
Lukey Avatar answered Sep 23 '22 06:09

Lukey


Use sprintf()

define('my_const', 100); $string = <<< heredoc       <p>The value of my_const is %s.</p> heredoc;  $string = sprintf($string, my_const); 
like image 25
Starx Avatar answered Sep 21 '22 06:09

Starx