TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-10-2008, 06:18 PM   #1 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default 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
Gareth is offline  
Reply With Quote
Old 03-10-2008, 06:46 PM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

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
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Old 03-10-2008, 07:20 PM   #3 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

not sure if it will work but try:

PHP Code:
global $siteName;
title$siteName'Home'); 
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 03-10-2008, 08:25 PM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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" 
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 03-11-2008, 09:26 AM   #5 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

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

Gareth
Gareth is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:00 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design