开发者

Pass variable to string in include file - PHP

What I am trying to do is to have an include file with generalized language in a string that will be used across multiple pages, but I also need to pass a variable to the string from the current page.

Here is an example:

Here is a开发者_高级运维 snippet of the index.php page:

<?PHP
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/prefix.php');

echo $GENERAL_STRINGS['user_not_found'];
?>

Here is the include page:

<?PHP
$GENERAL_STRINGS['user_not_found'] = 'User account not found<br /><a href="/register/?email='.$email.'">Register Today for FREE</a>';
?>

The $email variable is always empty when the link is referenced, I assume this is because it is looking for the $email variable from the include page instead of the index page. Is there a way around this?


Use printf() or sprintf():

<?php
$GENERAL_STRINGS['user_not_found'] = 'User account not found<br /><a href="/register/?email=%s">Register Today for FREE</a>';
?>

Use it in this way:

<?php
printf($GENERAL_STRINGS['user_not_found'], urlencode($email));
?>


I have tested it. All good. May be, you forgot assigning of $email.

<?PHP
$email = 'emal@email.com';
$GENERAL_STRINGS['user_not_found'] = 'User account not found<br /><a href="/register/?email='.$email.'">Register Today for FREE</a>';
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜