Search code examples
phpstringstring-interpolation

PHP deferred string interpolation


Is there a way to 'prepare' a string for later interpolation (without using custom expansion function)?
For instance:

class Test {
    private static $STR_VAL = "the val is $v";

    public static function printVal($v) {
        echo self::$STR_VAL;
    }
}

Solution

  • sprintf()

    class Test {
        private static $STR_VAL = "the val is %s";
    
        public static function printVal($v) {
            echo sprintf(self::$STR_VAL, $v);
        }
    }