TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Grrr Functions (http://www.talkphp.com/absolute-beginners/2451-grrr-functions.html)

Gareth 03-10-2008 06:18 PM

Grrr Functions
 
Hi

I have a config file, conf_global.php with all sorts of settings and variables. The one I am interested at the moment is $siteName = 'A Site Name';

I include conf_global.php on every page.

I also, in this file, link to core.func.php, which is a file with all my functions in it.

I have a function, called title():

PHP Code:


    
function title$sitename$page ){
        echo 
'<title>' $sitename ' - ' $page '</title>';
    } 

Which I refer to, to make the <title> tag easily. I assumed, that because $siteName has been defined in the conf_global.php, that to make it work all I had to do is:

PHP Code:

    <?php 
        title
$siteName'Home');
     
?>

However it doesn't work; I have to use this instead:

PHP Code:

    <?php 
        $site 
$siteName// Needed, for now...
        
title$site'Home');
     
?>

Is there a way I can use the title function to use a variable (siteName) defined in conf_global.php?

Gareth

SOCK 03-10-2008 06:46 PM

How are you including those two files?

It should work like so:
PHP Code:

<?php
require_once('conf_global.php');
require_once(
'core.func.php');

title$siteName'Home');
?>

Is that what you're doing?

Something else to consider, you really shouldn't allow your function to use echo and actually display something to the page. The idea of functions is to create a module or engine that you can plug some value into and have another value spit out the other end. For maximum flexibility, you should do
PHP Code:

function title$sitename$page ) {
    return 
$sitename ' - ' $page;
}

echo 
'<title>'title$siteName'Home' ) .'</title>'

This way you're not tied into using HTML in the function output, and you've just let yourself build a function that can be used in just about any instance. Of course with something so finely grained that it's only job is to output the page title, this may not be the best example, but when you start building more advance functions (and eventually classes) you'll see the difference. Just my opinion, YMMV.

The last thing to consider is continuity across file names, variables, etc. I notice two things in the code you provided; one, you have conf_global.php and core.func.php. Two naming conventions for include files. I would suggest keeping them the same, i.e. rename the functions definitions file to core_func.php (or of course, the other way around). The second thing is similar, in your naming of $siteName and $sitename. It's so easy to get those two mixed up. When I build a function, I use a totally different name for the argument than the actual variable going into it, e.g.
PHP Code:

function doSomething$s ) {
    return 
"Here we're doing something with {$s}";
}

$myString"data";
echo 
doSomething($myString); 

Absolutely no ambiguity as to what value is passed vs. the internal value.

HTH

freenity 03-10-2008 07:20 PM

not sure if it will work but try:

PHP Code:

global $siteName;
title$siteName'Home'); 


ReSpawN 03-10-2008 08:25 PM

In your configurations file;
PHP Code:

$sitename 'My site name'

In your functions file;
PHP Code:

function echoSiteTitle$sitepath '' ) {
    global 
$sitename;
    if ( 
$sitepath ) {
        return 
'<title>'$sitename .' '$sitepath .'</title>';
    } else {
        return 
'<title>'$sitename .'</title>';
    }


In your index file;
PHP Code:

echoSiteTitle(); // Displays "My site name"
echoSiteTitle('title'); // Displays "My site name title" 


Gareth 03-11-2008 09:26 AM

Thank you everyone for your help! I just had to make $siteName global :)

Gareth


All times are GMT. The time now is 07:27 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0