2015-10-28 81 views
1

这里是我的代码和它的工作为何此通知错误?

<?php 
     $num = 5; 
     $location = 'tree'; 
     define(format,'There are %d monkeys in the %s'); 
     echo sprintf(format, $num, $location); 
     ?> 

,但我发现

注意使用未定义的常量格式的 - 假设“格式化”的行号6

有树中有5只猴子

为什么?

+1

你用'format'为常量,而不是字符串。用作'define('format','%s中有%d个猴子'); – Gunasegar

回答

1

您需要添加' s到避免通知

define('format','There are %d monkeys in the %s'); 

标准定的名字是用大写字母。你应该做的 -

define('FORMAT','There are %d monkeys in the %s'); 

define()

1

你需要插入“”,而定义一个变量

<?php 

$num = 5; 
$location = 'tree'; 
define('format','There are %d monkeys in the %s'); 
echo sprintf(format, $num, $location); 

?>