Using a Constant with a Function?

I have a “config” file where I define…


	// Base URL (**Virtual Location)
	define('BASE_URL', ENVIRONMENT === 'development'
					? 'http://local.debbie'
					: 'http://www.MySite.com');

I have been using this Constant with great success over the past year or so.

However, now I want to incorporate it into a Function that I am working on, and I’m not sure how its scope works?! :blush: :blush:

Here is my current Function…


function generateURL($section){
  $url = 'http://local.debbie/' . $section . '/articles/';
  return $url;
}

What do I need to do to replace that static URL with reference to my Constant so that when I switch between “Development” and “Production”, everything still works"

Can my Constant - defined in my “config” file - be seen INSIDE my Function? (Or do I need to pass it into the Function? Or something else?)

Thanks,

Debbie

You simply need to replace it with BASE_URL, so you end result would be something like

function generateURL($section){ 
  $url = BASE_URL . $section . '/articles/'; 
  return $url; 
} 

Constants have a global scope.

Thanks for the response.

Yeah, I sorta knew that, and my code - which looks like yours - seems to work, but I was still skeptical.

Now that I have “Jedi Approval”, I’m good! :smiley:

Thanks!!!

Debbie

cpradio,

BTW, what would be a better name for this than “URL”…

http://local.debbie/finance/articles/

I am leaning towards $articleBaseURL

What do you think?

Thanks,

Debbie

Keep in mind, that variable only exists within your function, so $url is representative within the function. Outside of the function if assigning the value returned by genereateUrl to a variable, I’d use $articleUrl.

Right.

I meant like this…


	// (e.g. "local.debbie/management/articles/" )
	$articleBaseURL = generateArticleBaseURL($sectionSlug);

Outside of the function if assigning the value returned by genereateUrl to a variable, I’d use $articleUrl.

Doesn’t “Article URL” make people think about something like this instead…

http://local.debbie/finance/articles/postage-meters-can-save-you-money

:-/

Debbie

Fair enough :slight_smile: $articleBaseUrl works.