SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about functions

    The Code:

    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    <?php
    function require_css($css) {
    include('config.inc.php');
    $css_root = $root_to_css; // $root_to_css from config.inc.php
    echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"$css_root{$css}\" />";
    }
    require_css('global.css');
    ?>
    </body>
    </html>
    This outputs as:

    <link rel="stylesheet" type="text/css" media="all" href="http://www.YourCompany.com/css/global.css" />
    BUT

    If I code it like this:

    <?php include('config.inc.php');?>
    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    <?php
    function require_css($css) {
    $css_root = $root_to_css; // $root_to_css from config.inc.php
    echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"$css_root{$css}\" />";
    }
    require_css('global.css');
    ?>
    </body>
    </html>

    This outputs as:

    <link rel="stylesheet" type="text/css" media="all" href="global.css" />

    NOTE: It left out "http://www.YourCompany.com/css/"
    I'm a newbie to php and was curious why in the 2nd example it didn't include the "http://www.YourCompany.com/css/" url.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Variable scope

    Code php:
    function require_css($css) {
      global $root_to_css;
      $css_root = $root_to_css; // $root_to_css from config.inc.php
      echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"$css_root{$css}\" />";
    }

    In this case though, you're better off defining a constant:
    Code php:
    //config.inc.php
    define('ROOT_TO_CSS','http://www.YourCompany.com/css/');
    Code php:
    function require_css($css) {
      $css_root = ROOT_TO_CSS; // $root_to_css from config.inc.php
      echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"$css_root{$css}\" />";
    }
    The constants, like super globals ($_GET, $_POST, etc.) are accessible in any scope, and you don't have to worry about that.
    Saul

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I now get the following error:

    Cannot redeclare require_css()

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What are you doing? Why are you redeclaring require_css()? I'm a bit lost, nowhere in my examples I did that.
    Saul

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found what the problem was. I was including a file within the config.inc.php file, which had caused the conflict.

    What you said to do worked perfect


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
  •