Include constant in string without concatenating

✔ Recommended Answer


With Strings, there is no way for PHP to tell string data apart from constant identifiers. This goes for any of the string formats in PHP, including heredoc.

constant() is an alternative way to get hold of a constant, but a function call can't be put into a string without concatenation either.

Manual on constants in PHP


Answered By: Pekka

Method #2

If you want to include a constant value within a string without concatenating, you can use string formatting in Python.

Here's an example:

age = 25 name = "John" # Using string formatting to include constant value message = f"{name} is {age} years old." print(message)

In the above code, we have defined two variables age and name. We then use string formatting to include the value of age within the string without concatenating it separately. The f at the beginning of the string tells Python to use string formatting. We then include the variable names within curly braces {} and Python replaces them with their values. The final output would be:

John is 25 years old.

You can also use other string formatting methods, such as format(), to achieve the same result.


Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755