My apologies, but my programming skills revolve around solving algorithmic problems - I'm pretty new to how it all comes together. What would be the best way to define an object for use the way you include a page? Clarification: I have an statement that I use a lot throughout my site. Currently I have it in its own .php file and I include it in each page. I'd like to save on server calls though, and just include it in my common.php - but I'm clueless as how to do this. Here's a simplified form of my statement: <?php if ($handle = opendir($root . $_SERVER['REQUEST_URI'] . '/images/')) { $i = 1; while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { /* a lot of logic you're not interested in goes here */ $i++; } } closedir($handle); } ?> PHP:
I am still not fully there, sorry, long working hours, my brain and thinking straight I think you want to include that script on each page You have a few to choose from include (will add it to page) require (Same as include, except if it fails, the script will die) require_once (Great for connection files and settings, the file has already been called it will not include it again) I hope this helps And yo have to have about 100 includes on a 100 page website with 10,000 visitors a day before worrying about how the includes effect your sever, an include is like openening another file, No big porblem.
I was actually looking for a way to be able to call that code without having to include it from its own page. If not for saving on server calls, just for keeping it all tidy. I didn't know about the lack of strain it put on your server though. That lets me rest a little easier.
There are 3 ways of having code on a page Include it (require, include ect) Manually add it in MySQL. (Be careful with this option,) MySQL queries will put more stress on the server than an include, but again not much look at forums, they are constantly running queries to the server, and including files.
I simply want to be able to include the code (manually as you put it) in the page, but use the functionality of it by calling the code, rather than including it. Does this make more sense? My apologies if I'm not explaining it properly. Can I do this by making it a class? If so, how would I go about doing that?
When using include and require, it adds the code from the page you are including into your script If you have a page called include and on that page you had <? echo "Hello World"; ?> Then on another page you had <? include "include.php" ?> The source code in theory will be the same as include.php It as you wants, manually adds that page into your main page I good use of includes is for Navigation So if you add a page or change the name, you only have to change one file.
I don't mean to sound ungrateful, but I'm not sure you understand what I'm talking about. I'm aware of how includes work and what their benifit is. I simple wanted to know the syntax to make the code above into a function or class to be called - rather than including it in each instance.
Ahh i understand function HandleFunc($handle, $root, $file){ # if ($handle = opendir($root . $_SERVER['REQUEST_URI'] . '/images/')) { # $i = 1; # while (false !== ($file = readdir($handle))) { # if ($file != "." && $file != "..") { # /* a lot of logic you're not interested in goes here */ # $i++; # } # } # closedir($handle); # } } PHP: Then when you want to call it HandleFunc($handle, $root, $file) PHP: I hope that one helps
Now we're getting closer... The only problem is that, I don't need to set the $handle, $root or $file when I call it. I just to need to call an instance of it just the way it is. Can I do something like this?: <?php function handleFunc(){ if ($handle = opendir($root . $_SERVER['REQUEST_URI'] . '/images/')) { $i = 1; while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { /* a lot of logic you're not interested in goes here */ $i++; } } closedir($handle); } } ?> PHP: and then call it with handleFunc(); PHP: ? And thank you very much for your help.
Then you would need to set the Vars as global When working in a function, it only works on vars within its own function $str = "Testing"; function = ShwStr(){ echo $str; } ShwStr THis will show to the browser nothing, as in the function $str does not exsists, You could do your function like this function handleFunc(){ global $handle; global $root; global $file; Then it should work if ($handle = opendir($root . $_SERVER['REQUEST_URI'] . '/images/')) { $i = 1; while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { /* a lot of logic you're not interested in goes here */ $i++; } } closedir($handle); } } PHP:
Actually $root is the only variable that would need to be declared as global. The other two are instantiated in the function. And the only other part would be teh output. If the things you do in the part where you say "a lot of logic you're not interested in goes here" are internal things (nothing is printed to the screen), or you're actually doing the output there (via echo/print), then all should be well, otherwise you would just use the return statement to return the result back to your main script and when calling the function, you would do: $result = theFunction (); And you'd have the result in the $result variable.