SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Using a Constant with a Function?

    I have a "config" file where I define...

    Code:
    	// 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?!

    Here is my current Function...
    PHP Code:
    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

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,233
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    You simply need to replace it with BASE_URL, so you end result would be something like
    PHP Code:
    function generateURL($section){ 
      
    $url BASE_URL $section '/articles/'
      return 
    $url

    Constants have a global scope.

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    You simply need to replace it with BASE_URL, so you end result would be something like
    PHP Code:
    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!

    Thanks!!!


    Debbie

  4. #4
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,233
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Keep in mind, that variable only exists within your function, so $url is representative within the function.
    Right.

    I meant like this...
    PHP Code:
        // (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

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,233
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    Right.

    I meant like this...
    PHP Code:
        // (e.g. "local.debbie/management/articles/" )
        
    $articleBaseURL generateArticleBaseURL($sectionSlug); 
    Doesn't "Article URL" make people think about something like this instead...
    Fair enough $articleBaseUrl works.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •