[SOLVED]Making functions..

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Making functions..

Post 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();
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Making functions..

Post 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..
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Making functions..

Post 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
}

?>

User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Making functions..

Post 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' ); 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Making functions..

Post 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.
Fighting for peace is declaring war on war. If you want peace be peaceful.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: [SOLVED]Making functions..

Post 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
Post Reply

Return to “Beginner Help and Support”