1

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?

2 Answers 2

3

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));
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I think you mean urlencode since the $email variable is going into a url.? But otherwise, +1
Changed it. @Alexander, it is. $email is not defined in the language file, but in index.
0

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

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

3 Comments

You're mixing things up. There is a separate language file which should not refer to variables in index. The language file is included before assigning values in index.
Lol. $email not in index. 4email in inclufing file.
It's reffering to $email in the included file, but it shouldn't do that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.