son.of.the.morning Posted September 11, 2010 Share Posted September 11, 2010 I was just wondering if it was possible to have an external php file that can be included in the head of a web page, like a .js file. If this isn't possible maybe have a .js file containing php code that can be executed regarding the JavaScript around it... Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/ Share on other sites More sharing options...
fortnox007 Posted September 11, 2010 Share Posted September 11, 2010 Sure there is you are looking for 4 function in php, require(), require_once() include() include_ones() They are pretty much alike but have some minor differences. http://www.w3schools.com/php/php_includes.asp small tutorial Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110053 Share on other sites More sharing options...
son.of.the.morning Posted September 11, 2010 Author Share Posted September 11, 2010 Thanks dude, i will be shure to check that out in the morning and try have it masted by the night. Cheers Boss! Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110061 Share on other sites More sharing options...
fortnox007 Posted September 11, 2010 Share Posted September 11, 2010 just make 2 files: index.php and fatmonkeyonthebeach.php in index.php you put anywhere you like between <body></body> <?php include("fatmonkeyonthebeach.php"); ?> And in fatmonkeyonthebeach.php you put: <?php for ($i=2; $i<100; $i++){ echo "$i fat monkeys sitting on the beach<br />"; } ?> you can use the same for headers than place it above <html> Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110062 Share on other sites More sharing options...
trq Posted September 12, 2010 Share Posted September 12, 2010 Sure there is you are looking for 4 function in php, require(), require_once() include() include_ones() They are pretty much alike but have some minor differences. http://www.w3schools.com/php/php_includes.asp small tutorial require(), require_once() include() include_once() are not functions, but language constructs (statements). As such, they DO NOT require braces either. eg; include 'somfile.php'; Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110153 Share on other sites More sharing options...
chintansshah Posted September 12, 2010 Share Posted September 12, 2010 But for external file you have to give fullpath of URL like : <?php include("http://www.website.com/include_file.php"); ?> if, file is exist on your server then you can use <?php include("include_file.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110154 Share on other sites More sharing options...
trq Posted September 12, 2010 Share Posted September 12, 2010 But for external file you have to give fullpath of URL like : <?php include("http://www.website.com/include_file.php"); ?> if, file is exist on your server then you can use <?php include("include_file.php"); ?> I'm not sure that is what the OP actually meant as including a file from a remote server doesn't allow you to execute functions within that file, it just includes the output (if any) of that file into the position where include was called. it doesn't import functions, variables or constants into the same scope. Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110157 Share on other sites More sharing options...
son.of.the.morning Posted September 12, 2010 Author Share Posted September 12, 2010 So i couldn't call functions separately from the one external php file? Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110182 Share on other sites More sharing options...
trq Posted September 12, 2010 Share Posted September 12, 2010 So i couldn't call functions separately from the one external php file? Yes you can, just not one that has been included via a url. Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110203 Share on other sites More sharing options...
son.of.the.morning Posted September 12, 2010 Author Share Posted September 12, 2010 Could you explain, please? Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110253 Share on other sites More sharing options...
trq Posted September 12, 2010 Share Posted September 12, 2010 Explain? I just did. If you include a file via a url you only get its output, no functions or variables. If you include a file using a file system path, it is inserted into your script as though it is written within the script itself. Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110256 Share on other sites More sharing options...
son.of.the.morning Posted September 12, 2010 Author Share Posted September 12, 2010 No i meant could you explain how i could call functions from an external file? Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110259 Share on other sites More sharing options...
DarkMantis Posted September 12, 2010 Share Posted September 12, 2010 Hi, If you include a file from a remote server it will just give you the output of the file (if any, like said above). However, if you include it from a local server you can call the function like: <?php //require the class require_once('../classes/Some.Class.php'); //calling the class instance $new = new ClassInstance(); //echoing a specific variable from the class included echo $new->funcName->varName(); //or if static SomeClass::funcName(); ?> Hope this helped. Best Regards, Mantyy Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110271 Share on other sites More sharing options...
son.of.the.morning Posted September 12, 2010 Author Share Posted September 12, 2010 I wish i could say it had, but unfortunately i am still rather confused. Thanks non the less of course. Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110274 Share on other sites More sharing options...
PFMaBiSmAd Posted September 12, 2010 Share Posted September 12, 2010 Do you have a specific example of what you are trying to accomplish? Php include files are typically used to include php code and content (settings, function definitions, class definitions, templates...) that are then used by the main php code on a page. Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110276 Share on other sites More sharing options...
son.of.the.morning Posted September 12, 2010 Author Share Posted September 12, 2010 I just want a separate php file containing all the function for the entire web site, this file will be included in every relevant page on the site so that the page is able to call a function from the external file. The idea of this is for an easier way to edit the primary code of the web site without having to run through every page that contains a specific function. There are more obvious benefits in regards to creating the web site also (not having to write the same piece of code ten times over). Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110288 Share on other sites More sharing options...
PFMaBiSmAd Posted September 12, 2010 Share Posted September 12, 2010 I've got to ask, what problem did you have when you tried it? Because you do exactly what you described - main file - <?php include 'functions.php'; ... $var = 'abc'; echo my_function1($var); // call a function that is defined in functions.php and echo the result it returned ?> functions.php <?php function my_function1($var){ // do some processing on the parameter $var return $result; // return the result } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110306 Share on other sites More sharing options...
son.of.the.morning Posted September 12, 2010 Author Share Posted September 12, 2010 I didn't attempt it at all, i just wanted to know if it was possible so i could look into it. I just thought if i started a discussion on it would give me at least a kick start in a understanding. if you get me lol... Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110341 Share on other sites More sharing options...
son.of.the.morning Posted September 12, 2010 Author Share Posted September 12, 2010 Is a function in php written in the same procedure as a function would be written in JavaScript/actionscript. for example a function is actionscript would be something on the lines off function(myFunction) { //anything that goes here will be executed when this function(myFunction) is called. } I am very new to php so i am sorry if am miss comprehending and sounding retarded. lol Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110353 Share on other sites More sharing options...
PFMaBiSmAd Posted September 12, 2010 Share Posted September 12, 2010 You need to get and read through a php book or a comprehensive php tutorial. You are asking how to do the basics that the php language can do and exptect a comprehensive answer in a couple of hundred of words that would be written in a reply in a forum post. Attempting to learn a programming language that way will leave you with huge gaps. See this link for examples of writing php functions - http://w3schools.com/php/php_functions.asp Quote Link to comment https://forums.phpfreaks.com/topic/213181-an-external-php-file/#findComment-1110356 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.