Jump to content

An external php file?


son.of.the.morning

Recommended Posts

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>

 

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.