How to print a constant in the middle of a string, without concatenating?

Asked

Viewed 887 times

11

Is there any way to print a constant in the middle of a string, without using concatenation, as with PHP variables?

Example:

 $nome = 'wallace';
 echo "Meu nome é {$nome}";

In the case of constants, I know I could print concatenating:

echo 'meu nome é' . NOME_USUARIO;

But I’d like to avoid the concatenation because I find it awkward.

There are other possible ways of doing this?

  • 1

    Inelegant is using constants to display information.. makes no sense at all.

  • 2

    Maybe it’s just your opinion. What about the magic constants? We have to use them (whether we want to or not) to display a line in a file, for example. In this sense the question becomes useful. Example with the @rray response. printf('A linha do arquivo é %s e o arquivo é o %s', __LINE__, __FILE__);

  • 1

    I wouldn’t do that by concatenating!

3 answers

10


You can use the function printf() to print a formatted string and then pass the values to barter or use a comma in that case.

define('NOME_USUARIO', 'lol');

echo 'meu nome é ', NOME_USUARIO;
printf('meu nome é %s', NOME_USUARIO);
  • I thought you were gonna do $constante = NOME_USUARIO

6

A possible way would also be with something half-mad magic that php allows: We can declare the function value constant - that serves to get the value of the constant - and save it in the variable. Then call it in the middle of the string you want.

Behold:

$c = 'constant';

define('nome', 'Wallace');

echo "Meu nome é {$c('nome')}";
  • This one I didn’t know, is there something in the manual? + 1.

  • I didn’t see no, @rray. I’m not sure, but I think I saw it on SOEN. I couldn’t find the question again.

  • xD I was seeing this now, the same idea of javascript, play a function in a variable and then invoke it.

  • It may be that my view is limited in this sense, but in this example I prefer the "TEXT". CONSTANT . ""

  • 1

    Suggestion to add/complement the answer: https://chat.stackexchange.com/transcript/11910?m=46488961#46488961

3

I don’t think it’s possible, at least as far as I read from the documentation:

However doing some tests I realized that it is possible to force/trick the syntax, as the @Wallace did in his reply, however the idea here is to create a variable that is an anonymous function, so you will not need the apostrophes, an example:

<?php
define('FOO', 'bar');

$constant = function ($nome) {
    return $nome;
};

echo "FOO = {$constant(FOO)}";

Another example taking "native constants":

<?php
$constant = function ($nome) {
    return $nome;
};

echo "PHP_VERSION = {$constant(PHP_VERSION)}";

Example in the ideone: https://ideone.com/vSJ05m

If it doesn’t exist as define() will display exactly what was written:

<?php
$constant = function ($nome) {
    return $nome;
};

echo "BAZ = {$constant(BAZ)}"; //Exibe "BAZ = BAZ"
  • I never understood this mechanism of PHP constants, it is different the way the constant is interpolated with the string, it is as if PHP treated a count differently from the variable. In C#6 and above uses the $ followed by an expression $"{constante}" or $"{variavel_comun}" to create a string formatted in the string.

  • @cat I believe is characteristic of every language

Browser other questions tagged

You are not signed in. Login or sign up in order to post.