473,513 Members | 13,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Including functions, scope.

3 New Member
Hi everyone, I'll try to be brief and to the point - i'm starting to incorporate include() into my php pages to clean up the code but seem to run into problems when trying to call functions that are included in different files. I've been trying to do some research on function scope and please excuse my ignorance, but is this at all possible? For example we have two files:

File 1: function.inc

<?php function test() {

switch ( $var ) {
case ( $var < 130 ):
echo " A " ;
break;
case ( $var < 480 ):
echo " B ";
break;
case ( $var < 1230 ):
echo " C ";
break;
case ( $var < 1830 ):
echo " D ";
break;
case ( $var < 1831 ):
echo " E ";
break;
}
}
?>

And to clean up another page, in another file, index.php - I'd like to be able to call the function test() ...

File 2: index.php

<head>
<title>Testing Page</title>

<?php include 'function.inc'; ?>

</head>

<body>

<?php test(); ?>

</body>
</html>

Could anyone please help me? And if this isn't possible, is there any kind of workaround for these kinds of things? Thanks in advance.

Nick
May 11 '07 #1
6 1419
ak1dnar
1,584 Recognized Expert Top Contributor
This is possible and For your test function you have o pass function parameters also.

for a example. we will get your func.inc file

[PHP]<?php
function test($var)
{

switch ( $var ) {
case ( $var < 130 ):
echo " A " ;
break;
case ( $var < 480 ):
echo " B ";
break;
case ( $var < 1230 ):
echo " C ";
break;
case ( $var < 1830 ):
echo " D ";
break;
case ( $var < 1831 ):
echo " E ";
break;
}
}
?>[/PHP]

then from your calling script use like this.

[PHP]<?php
include 'func.inc';
$var = 500; //change the value here and call for the script
test($var);
?>[/PHP]
May 11 '07 #2
Purple
404 Recognized Expert Contributor
Hi,

An alternative (and IMO better) method to achieve what you are looking for would be to put this and any other standard functions within a class - this will provide much more flexibility around how you use, reuse and maintain the functions included within the class.. there are a number of tutorials around the net if you are not familiar with the concept.. You could take a look at http://www.php-editors.com/articles/...hp_classes.php to start you off.

Rgds Purple
May 11 '07 #3
Atli
5,058 Recognized Expert Expert
Hi, methylparabex, and wecome to TSDN.

I have edited the threads title to better describe its topic.
Please read the Posting Guidlines before posting.

MODERATOR
May 11 '07 #4
methylparabex
3 New Member
Thank you for everyone's help and I'll let you know how it goes. I'm definitely going to try those php classes. So thank you and I'll be sure to post accordingly next time.

Nick
May 11 '07 #5
methylparabex
3 New Member
I've gotten the functions to work with parameters, now is there any way to perform this same idea with remote functions without parameters? Thanks again for everyone's help.
May 12 '07 #6
Atli
5,058 Recognized Expert Expert
You can define global variables that can be used in any part of you code.

This can be done like this:
[PHP]
// Create a test function
// this could be included from another file
function testFunc()
{
echo $GLOBALS['MyVar'];
}

// Define a global variable
$GLOBALS['MyVar'] = "I am a global vairable!";

// Call the test function
testFunc();

// This will output: I am a global variable
[/PHP]
May 12 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2329
by: Keiron Waites | last post by:
Hi, I include the following function: <?php function login($members_only) { if (isset($_COOKIE)) { $login = explode("|split|",$_COOKIE); $login =...
76
3686
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax,...
2
6515
by: Koen | last post by:
Hi all, I have a question about something rather common, but I'm a bit lost at the moment: Lat's say you have this: // in A.h Class A {
2
1226
by: Michael G | last post by:
A habit of mine is to include all files at the head of the file requiring them. Is the following bad practice? It works just fine but...??? thanks, mike class ActionFactory { function...
23
3962
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
11
2783
by: Gary Wessle | last post by:
Hi is it right to have a line like #include <path/to/header.hfor a library on my system, in my header file and use some functions provided by this library in the implementation file (file.cpp)...
18
2836
by: sam_cit | last post by:
Hi Everyone, int main() { printf("not included stdio.h"); } Yes, i haven't included stdio.h and my compiler would generate a warning and would assume that it would return a int, my question...
14
5980
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
13
2996
by: Chris Carlen | last post by:
Hi: I have begun learning Python by experimenting with the code snippets here: http://hetland.org/writing/instant-python.html In the section on functions, Magnus Lie Hetland writes: ...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.