将变量传递给包含文件中的字符串 - PHP [英] Pass variable to string in include file - PHP

查看:106
本文介绍了将变量传递给包含文件中的字符串 - 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.

以下是一个例子:

这是index.php页面的一个片段:

Here is a snippet of the index.php page:

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

echo $GENERAL_STRINGS['user_not_found'];
?>

以下是包含页面:

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

当引用链接时,$ email变量始终为空,我认为这是因为它正在寻找从包含页面而不是索引页面的$ email变量。有办法吗?

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?

推荐答案

使用 printf() sprintf()

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

以这种方式使用它:

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

这篇关于将变量传递给包含文件中的字符串 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆