0

Hello I would like to be able to define a URL inside my config file and echo the url inside array:

$options = array( 'results_per_page' => 30,
                  'url' => 'http://localhost/page.php?page=*VAR*',
                  'db_handle' => $dbh
                );

In this case let's say I would like to define a variable:

$url = 'http://localhost/';

or

define( "SERVER_PATH", "http://localhost/" );

and being able to echo it inside the 'url' => "$url" page.php?page=*VAR*'

Any help will be welcome.

4
  • why do you want to echo it inside an array? practically it's not possible. Commented Jul 14, 2016 at 19:23
  • because I have this line in different php files and want to change the url in one place instead of each of them. Commented Jul 14, 2016 at 19:24
  • 2
    You are probably looking for string concatenation Commented Jul 14, 2016 at 19:25
  • Create a temp $tmpUrl = $url then use that 'url' => "$tmpUrl" . "page.php?page=*VAR*'" I don't know I fully understand the issue but it seems like you do not want to overwrite something? Commented Jul 14, 2016 at 19:26

2 Answers 2

2

You can do this way:

Using variable:

$options = array( 'results_per_page' => 30,
                  'url' => $url . 'page.php?page=*VAR*',
                  'db_handle' => $dbh
                );

Using constant:

$options = array( 'results_per_page' => 30,
                  'url' => SERVER_PATH . 'page.php?page=*VAR*',
                  'db_handle' => $dbh
                );
Sign up to request clarification or add additional context in comments.

Comments

2

You can also append the variable to the end of the array value by using the concatenation operator

$option['url'] = $option ['url'] . $var;

Or in less words

$option['url'] .= $var;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.