I remember seeing somewhere using %D or something similar to declare constant where the value of %D is supplied while calling the constant.
define("EVENT_DATE","The开发者_运维百科 event date is %D");
Any information on this, how does it work please?
Check out sprintf()
and printf()
.
Example:
define('EVENT_DATE', 'The event date is %s.');
echo sprintf(EVENT_DATE, 'tomorrow morning');
The above will display:
The event date is tomorrow morning.
If all you want to do is print, and you're absolutely sure that the string does not need htmlspecialchars()
applied to it, then you can use printf()
directly. It works just like sprintf()
, but it echoes the result instead of returning it.
As Mel points out while downvoting, %D
is indeed a strftime()
format. Check that out as well and check Mel's example of how to use it.
You can declare constants for your program in php:
define("CONSTANT_EXAMPLE", 2);
echo CONSTANT_EXAMPLE * CONSTANT_EXAMPLE;
This will echo 4.
The specific %D is used in strftime:
<?php
define('EVENT_DATE', 'The event date is %D');
$eventDate = mktime(0, 0, 0, 5, 31);
echo strftime(EVENT_DATE, $eventDate);
?>
Gives: The event date is 05/31/11
精彩评论