Page 1 of 1

Re: Making functions..

Posted: Sun Jan 15, 2012 11:03 pm
by Chris
I don't quite understand whats up with the variable indexes.

Code: Select all

$bh_template = array(
   'L_TEXT' => $lang['gonope'],
   'L_LINK' => $lang['index'],
   'U_PAGE' => 'index',
   'PID'=> $pid
);

function run_template()
{
    global $bh_template;
    echo $bh_template['L_TEXT'];
}

// Run the template engine
run_template();
 

Re: Making functions..

Posted: Mon Jan 16, 2012 1:14 pm
by Xaleph
That could work, however what I dont understand is why you use globals in the first place. Globals are quite unsafe and memory busters. Localized memory usage in functions for variables is ok, they`ll be cleared after the return of the function, globals are not. Oh well, memory isn`t even the issue, mostly the unsafe part.

However, what I do believe is better is you use CONSTANTS. It`s pretty much the same thing as using globals however you only SET it once. after that, you can use (read) it where ever you want. Like:

define("ROOTPATH", "path/to/templates/");

now, in any subsequent file loaded after the constant, you can use ROOTPATH as a variable ( without having to use the $).

Oh and 1 more thing:

I believe its Genesis, not genisis. Not that I mind, just thought you should now, maybe it was on intention, ya never know..

Re: Making functions..

Posted: Mon Jan 16, 2012 10:56 pm
by Xaleph
ah wait, i misunderstood something, it`s the function itself you want shorter? Well, using the preg_replace is one thing, but loading the array is another. Why not pass it in the function?

Like:

Code: Select all

<?php

function run_template($path, $array){
    // and inside you can use the $array you created before ( the $bh_template. ? ) 
    // do the rest
}

?>


Re: Making functions..

Posted: Tue Jan 17, 2012 12:21 pm
by Chris
Would str_replace not be easier to achieve this?

Code: Select all

define( 'BH_ROOT', 'path/to/my/system' ); 

function run_template($array, $template)
{
    $text = file_get_contents( BH_ROOT . 'template/genisis/' . $template . '.tpl' );
    foreach( $array as $key => $value )
    {
        $text = str_replace( '{' . $key . '}', $value, $text );
    }
    echo $text;
}

$bh_template = array(
   'L_TEXT' => $lang['gonope'],
   'L_LINK' => $lang['index'],
   'U_PAGE' => 'index',
   'PID'=> $pid
);

run_template( $bh_template, 'index' ); 

Re: Making functions..

Posted: Wed Jan 18, 2012 1:59 pm
by Chris
If I have an array with 4 values:

Code: Select all

$array = array( 'one', 'two', 'three', 'four' ); 
I can loop through it in a few different ways, I could count how many values it has using the count() function, then use a for or while loop, but both are rather inconvenient, foreach is designed to make life easier. A foreach loop allows us to easily read the indexes as well as values within the array.

for loop:

Code: Select all

$totalPositions = count($array); // 4

for( $i = 0; $i < $totalPositions; $i++ ) // as long as $i is less than 4, loop and increment $i
{
    echo $array[$i] . '<br />';
}
 
The result would be:

Code: Select all

one<br />two<br />three<br />four<br />
We can achieve the exact same result with a foreach loop using a lot less code:

Code: Select all

foreach( $array as $value )
{
    echo $value . '<br />';
} 
With a foreach loop we can also read the indexes/keys of the positions within the array

Code: Select all

foreach( $array as $key => $value )
{
    echo $key . ' => ' . $value . '<br />' . "\n";
} 

Code: Select all

0 => one<br />
1 => two<br />
2 => three<br />
3 => four<br />

Code: Select all

$array = array( 'green' => 'oranges', 'red' => 'bananas', 'blue' => 'tomatoes' );

foreach( $array as $key => $value )
{
    echo $key . ' => ' . $value . '<br />' . "\n";
} 

Code: Select all

green => oranges<br />
red => bananas<br />
blue => tomatoes<br />
The differences between preg and str_replace aren't very big, they both simply find matches in a string and replace them, preg_replace simply has a lot more functionality when it comes to matching.

Re: [SOLVED]Making functions..

Posted: Wed Jan 18, 2012 2:34 pm
by Xaleph
Pretty much, preg_replace is based on the Perl module for REGEX operations where the p form preg stands for Perl, however that`s not the biggest difference. Not that it matters that much :) Glad you solved it. I recommend using preg_replace, it`s said to be faster, but I don`t know for sure