TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Clarification on INCLUDE() (http://www.talkphp.com/absolute-beginners/2986-clarification-include.html)

Dave 06-21-2008 03:06 PM

Clarification on INCLUDE()
 
Hello all --

A large portion of the PHP program I'm working on is repeated about 2000 times, but before it can be properly initialized at the start of each loop, about 50 variables (counters) have to be set back to 0.

I just want to do this "tidily," if that is a word...:-/

Don't want to do this in a function because I'd have to set all the variables to a global state. Just wondering if I could do this by having the code (setting the variables to 0) in a separate PHP file and include() it at the top of every iteration of the loop...?

Is that an acceptable interpretation of a proper use of include()? I've read the online php.net documentation several times, but I'm still a little unsure on this point.

Thanks!
Dave

Salathe 06-21-2008 04:02 PM

In short, yes.

Dave 06-22-2008 08:30 PM

Thanks for info on INCLUDE()...
 
Great! Thanks for your quick response.

So, a PHP file that is called repeatedly using INCLUDE() is the same as a subroutine or procedure. Good to know. 8-)

Dave

drewbee 06-23-2008 01:37 AM

Basically though, if you initialize your variables properly your function calls of which get included would automatically set them to 0.

IE
PHP Code:

function myFunc()
{
    
var1 0;
    
var2 0;
    
var3 0;



Dave 06-23-2008 02:50 AM

Thanks very much for your response....

But wouldn't I have to declare the variables as "global" in order for the reset variables to be recognized by the calling script?

Or, I may be totally missing what you are pointing out...:-/

Dave

delayedinsanity 06-23-2008 04:02 AM

I'm not sure what he's up to there, but you only need to declare your variables global if you set them in the global scope (ie, outside a function, or inside a function that declares them global) and you want to use them inside of another function. ie

PHP Code:

// included file, echo.tpl
--
echo 
$var;
--

//your script

$var "hello world!";
include (
'echo.tpl'); // automagically recognizes $var, because the include is treated as though it were part of the parent script

function doesntWork()
{
    echo 
$var;
}

function 
works ()
{
    global 
$var

    
echo $var;
}

doesntWork();
works(); 

If you placed those functions inside the include, they'd work (or not) the same as if they were directly part of the parent script.
-m

drewbee 06-23-2008 02:52 PM

Sorry, I missed the part about them being needed to be used globally.

Object Oriented Programming FTW! :)

Once i started doing object oriented, I have sort-of lost sight of procedural.

PHP Code:

class NewClass
{
    var 
$var1 0;
    var 
$var2 0;
    var 
$var3 0;
 
    function 
NewClass()
    {
        
    }
 
    function 
resetVars()
    {
        
$this->var1 0;
        
$this->var2 0;
        
$this->var3 0;
    }
}
 
$NewObject NewClass();
$NewObject->var1 13;
$NewObject->var2 22;
$NewObject->var3 234;
 
echo 
$NewObject->var1;
echo 
$NewObject->var2;
echo 
$NewObject->var3;
 
$NewObject->resetVars();
 
echo 
$NewObject->var1;
echo 
$NewObject->var2;
echo 
$NewObject->var3


CoryMathews 06-24-2008 03:45 AM

when you use includes you MUST use relative paths in order for variables to work between pages.

Dave 06-24-2008 03:50 PM

Thanks CoryMatthews and drewbee for your responses. :-)

The OOP approach just looks to me (an amateur) like the opportunity to drive into a very dense fog -- in the middle of the night -- in a place I've never been before.

Or going from LA to San Francisco, by way of New York.

I know it is not that way, but that's the way it looks from down here.:-P

Dave

drewbee 06-24-2008 04:43 PM

haha, yeah I know what you mean. The language itself isn't to hard to use within PHP... however understanding OOP concepts can take some time to grasp onto. But I tell you, once you get it, its a hole new world of programming. I use OOP far more then what I should, I think. I think its so much easier to maintain though!

Evulness 06-24-2008 05:10 PM

oop isn't really harder than procedural. in fact, i think its easier, in alot of aspects. more organization. idk, just my opinion.
Instead of having to call/include the entire script repeatedly, using a class would only require 1 inclusion, and then you can infinatly call your functions throughout your site. Using something like what drewbee just posted, would be alot more efficent for what you are doing, than the route you are attempting.

IMO helps save on unnessessary page calls, etc... not to mention if you take the time to write out a "framework" so-to-speak of your own, a system of functions, and methods that you use regularly. which would only leave minor additions/edits to your classes, and alows you to do more proceedural stuff.

drewbee 06-24-2008 05:19 PM

Yup. I agree Evulness. I have my own framework integrated with Smarty...

Everyone of my forms (the display or controller; not the html code but the part that tells which html templates to display) consists of about 10 lines of code. This completely handles whether people can access the page (permissions), session handling, and database management.

gotta love oop!


All times are GMT. The time now is 11:21 PM.

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