0

Is it possible to call a function directly inline (and get its result) in a string in PHP?

Normally the . concatenate character can be used.

$x = "Class Name : '" . get_class($class) . "'";

I want to do something like this:

$x = "Class Name : '${get_class($class)}'";
CvRChameleon
  • 367
  • 1
  • 5
  • 29
  • you can call function if function return value – Forge Web Design Jun 04 '20 at 09:16
  • you can't, it will re-declare the function again and possibly it may show errors as `cannot re-declare yourFunction()` – Rohit Sahu Jun 04 '20 at 09:34
  • Two contradicting answers, mmmm ;) "you can call function if function return value " - mind giving me an example please :) - Thanks, I am just waiting to see if he can show me an example. – CvRChameleon Jun 04 '20 at 09:42
  • @Adyson, can you confirm if it is possible to for instance use `get_class` inline (similar to what I tried) and in an example? – CvRChameleon Jun 04 '20 at 10:25
  • @CvRChameleon This notation doesn't work with a function call directly unfortunately. There are some examples of the syntax at the following link, note the "won't work" example at the bottom, which is analogous to your case: https://stackoverflow.com/a/2596838/5947043 – ADyson Jun 04 '20 at 10:43
  • 1
    @RohitSahu your comment makes no sense. No functions are being declared here. Your error would only occur if you, in your own code, decided elsewhere to create a function called "get_class" which of course is already the name of a built-in PHP function. That has nothing to do with this question though. – ADyson Jun 04 '20 at 10:44
  • Thank you @Adyson, I am looking at those examples now. – CvRChameleon Jun 04 '20 at 14:27

0 Answers0