Author Topic: Can create new functions then call them from a PHP file in PHP?  (Read 969 times)

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Can create new functions then call them from a PHP file in PHP?
« on: November 18, 2009, 11:33:37 AM »
I know i can do this most other languages that are used for applications but is this possible for PHP?

What i'm talking about doing is something like.

Code: [Select]
<? php
SomeFunction(Data_parsed_through_it);

Some_Function(Data_Parsed_through_it);
/* actual function goes here*/

?>

Is that humanly possible in php? I'm wanting to write up a quick program to give me the amount of EXP required per level as a player levels up so i can see what it looks like. I have the formula down pat i think, but i want to see how it's goign to actually be. And my formula has alot of variables that defined via the character's level. This is of course just simple if it's the level range then define it. Then i was going to put all of that together into my formula. Then put that answer into the DB and keep on rolling along. After it was done i was going to just look through it.

If my idea of functions within the PHP code is too wild. Does anyone know of any other way to do it besides making it pretty ugly code?

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #1 on: November 18, 2009, 11:42:56 AM »
you mean "call function before it's defined in the code"?

yes, afaik *unless* the function definition is conditioned somehow, examples:
Code: [Select]
foo(); // prints "bar"
function foo(){ echo "bar";  }

// ------
$a = 1;
bar();  // <-- error here
if($a==1){ function bar() echo "foo"; }

BUT you should have functions somehow grouped together, let's say make a file with functions related to player or something (higher level is to use OOP)
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #2 on: November 18, 2009, 11:56:14 AM »
well sort of yes. I wasn't going to call it like that. And that's good to know :D. I was planning on once i get the game actually going. I was going to make a full page just full of common functions to help to save me time. That's great to know that i can actually define my own function before i put it in the code. I was going to use quite a few things that called functions before they were in the code. And well just put them down a bit right now.  Really glad to know that PHP lets me define my own functions like that.

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #3 on: November 18, 2009, 12:00:30 PM »
define my own function before i put it in the code
You mean "call", right?

More reading: http://cz2.php.net/manual/en/functions.user-defined.php
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #4 on: November 18, 2009, 12:12:07 PM »
i guess that's what you'd call it. I'm using my "word that makes the most sense to me" terminology right now. I'm still used to things like VB.net and other .net based languages so i'm not used to using their ideas and terminology. As i'm of course starting off small. A simple app, that well does what i'll need later. "calls functions" as you say for it, reads and writes data into the DB. Also it does basic mathmatical functions which'll be used later on.  Thanks for helping me.

Would give you rep but i dont' see a way to do it on this forum.

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #5 on: November 18, 2009, 12:20:41 PM »
That define X declare thing seems to me more like a low-level language issue, like in C you specify prototype in header file (the first row with the function name) and in source file you describe what it does.
"Calling function" means executing it on the certain place

you can only with 100+ posts, but no need, glad I could help
others might still post more good ideas ...so don't switch to another channel, stay with us :)
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #6 on: November 18, 2009, 01:44:30 PM »
PHP will produce a fatal error if you attempt to call a function that has not been defined. You cannot use function prototyping such as can be found in C++. The function must be completely defined at the time of the call.

However, you can utilize a class without having it predefined if you're using PHP5 (which just about everybody is at this point). To do this, you define a function called __autoload ($className) {} which simply attempts to find a definition file for the class. The logic on how you find the class is up to you, however, this allows you to skip putting "require" or "include" declarations in your file.

Additionally, you can utilize interfaces to define the methods available to a class without the need to implement those functions. A class may implement multiple interfaces but can only extend one class.
Idiocy - Never underestimate the power of stupid people in large groups.


Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #7 on: November 19, 2009, 12:00:10 AM »
hmm that interfaces thing they have seems to be really close to what i was trying to do before when i was writing out my formulas in pseudo code. thanks, i'll go and try it once i get back to my page.

Also this something that's still baffling me. How is it that all of these pages have html in them but yet are a php page. I'm guessing that a .php page can if it needs to have html elements. As one of the tutorials i was reading up upon had a form that when you submitted the data it called itself again. So i'm guessing that is the proper way to do such things in that php can use html inside of it's self.

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #8 on: November 19, 2009, 05:08:01 AM »
Well...basicly...you can look at it the way that you have a certain file and you insert the php to some places to make some part dynamic. You can of course have php file that only communicates with database and has no output. But sometimes it's just a whole html page with only a few parts where PHP generates something. Afaik PHP at it's start was rather something like template engine, that would explain a lot.

It also does not have to be (x)html, it can be anything....JS (you can for example pass some PHP values to JS like <script>var foo="<?php echo $mystring?>"; ), CSS, whatever... you even don't have to change an extention of files if you specify that PHP parser should not only parse .php files, but some others of your choice.

That being said, it's not good to mix up a website logic with presentation layer, so if you use PHP for modifying html, it's best just to mostly inject values and all the preparation have already finished before any output (before you write <!doctype in html etc.)
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #9 on: November 19, 2009, 06:10:47 AM »
well i was talking about reading from forms. Such as just the basics. Kind of like the sign up page on "building browser games" where they use html forms which read data from them. That's what i was talking about using it for. Since that was confusing to me atleast.

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #10 on: November 19, 2009, 08:28:46 AM »
You can submit a form anywhere you please. Really, the best way to handle forms, in my opinion, is to create a form processing engine. In this manner, all forms are sent to a single point for processing and it may call additional functions depending upon the form definition.

However, with your current level of experience that's a little beyond where you should be comfortable at the moment. ;)

The CMS Drupal has a really neat way of handling form submissions and definition. It's definitely worth a look once you get some code under your belt.
Idiocy - Never underestimate the power of stupid people in large groups.


Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Can create new functions then call them from a PHP file in PHP?
« Reply #11 on: November 19, 2009, 10:13:35 AM »
well right now i'm just trying to do a simple exp calculator like thing. That'll write the data into the db for me so i can then look back over it and see if my current exp formula requires too much exp towards the end or too little. And that is something i'll try to remember once i start doing something more complex in php than just well simple DB interactions and forms.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal