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 06-21-2008, 03:06 PM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default 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
Dave is offline  
Reply With Quote
Old 06-21-2008, 04:02 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

In short, yes.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Dave (06-22-2008)
Old 06-22-2008, 08:30 PM   #3 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Asterix 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.

Dave
Dave is offline  
Reply With Quote
Old 06-23-2008, 01:37 AM   #4 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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;

Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
The Following User Says Thank You to drewbee For This Useful Post:
Dave (06-23-2008)
Old 06-23-2008, 02:50 AM   #5 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

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
Dave is offline  
Reply With Quote
Old 06-23-2008, 04:02 AM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Dave (06-24-2008)
Old 06-23-2008, 02:52 PM   #7 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
The Following User Says Thank You to drewbee For This Useful Post:
Dave (06-24-2008)
Old 06-24-2008, 03:45 AM   #8 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

when you use includes you MUST use relative paths in order for variables to work between pages.
CoryMathews is offline  
Reply With Quote
The Following User Says Thank You to CoryMathews For This Useful Post:
Dave (06-24-2008)
Old 06-24-2008, 03:50 PM   #9 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

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.

Dave
Dave is offline  
Reply With Quote
Old 06-24-2008, 04:43 PM   #10 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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!
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Old 06-24-2008, 05:10 PM   #11 (permalink)
The Contributor
 
Evulness's Avatar
 
Join Date: Apr 2008
Location: Tampa, FL
Posts: 65
Thanks: 6
Evulness is on a distinguished road
Default

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.
__________________
"Knowledge is power. Abuse it."~Evulness
My portfolio: www.evularts.com
Send a message via AIM to Evulness
Evulness is offline  
Reply With Quote
Old 06-24-2008, 05:19 PM   #12 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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!
Send a message via AIM to drewbee
drewbee 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:01 AM.

 
     

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