1

The following code produces and undefined variable $s instead of "number two"

define("T1","one");
define("T2","two");

$test="number %2$s";

$test=sprintf($test, T1,T2);

echo $test;
1
  • escape the $ with \$ or use single quota Commented Feb 4, 2013 at 18:50

1 Answer 1

7

Single quotes solve your problem. Double quotes cause PHP to interpolate your '$' as a variable.

<?php
define("T1","one");
define("T2","two");

$test='number %2$s';

$test=sprintf($test, T1,T2);

echo $test;

See it working

Sign up to request clarification or add additional context in comments.

2 Comments

Ok so can I not do this in a heredoc statement as that is actually the point of this.
@user1209203: So then why aren't you using a nowdoc instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.