redarrow Posted April 13, 2009 Share Posted April 13, 2009 How do you call a function within a function please. <?php function plugins(){ function plugin_quiz(){ $quiz_quistion="<center>"; $quiz_quistion.='Do dogs eat human flesh!'; $quiz_quistion.="<br><br>"; $quiz_quistion.="<form method='POST'>"; $quiz_quistion.="<select name'quiz_quistion'>"; $quiz_quistion.="<option value='yes'>Yes</option>"; $quiz_quistion.="<option value='no'>No</option>"; $quiz_quistion.="</select>"; $quiz_quistion.="<br><br>"; $quiz_quistion.="<input type='submit' name='submit' value='send'>"; $quiz_quistion.="</form>"; $quiz_quistion.="<center>"; return($quiz_quistion); } function plugin_message_form(){ $message_form="<center>"; $message_form.="<form method='POST'>"; $message_form.="Subject"; $message_form.="<br>"; $message_form.="<input type='text' name='subject'>"; $message_form.="<br><br>"; $message_form.="Name"; $message_form.="<br>"; $message_form.="<input type='text' name='name'><br>"; $message_form.="<br><br>"; $message_form.="Message"; $message_form.="<br>"; $message_form.="<textarea cols='30' rows='20'></textarea>"; $message_form.="<br><br>"; $message_form.="<input type='submit' name='submit' value='send'>"; $message_form.="</center>"; return($message_form); } } ?> <?php echo plugins(plugin_quiz()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/ Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Why the **** are you doing that? Just define the functions on their own. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808294 Share on other sites More sharing options...
redarrow Posted April 13, 2009 Author Share Posted April 13, 2009 I am trying to design a code, that lets me plug a plugin ( like the ones below) into, a web site, without the need of redesigning the web site. so if i design a plugin i ask the user to just add a page to the project with a include. dynamically. (not using just include lol) but how? so the question is, how do you design a plugin to integrate to a existing web site. <?php $plugin="plugin_message_form"; switch ($plugin){ CASE "plugin_quiz": $quiz_quistion="<center>"; $quiz_quistion.='Do dogs eat human flesh!'; $quiz_quistion.="<br><br>"; $quiz_quistion.="<form method='POST'>"; $quiz_quistion.="<select name'quiz_quistion'>"; $quiz_quistion.="<option value='yes'>Yes</option>"; $quiz_quistion.="<option value='no'>No</option>"; $quiz_quistion.="</select>"; $quiz_quistion.="<br><br>"; $quiz_quistion.="<input type='submit' name='submit' value='send'>"; $quiz_quistion.="</form>"; $quiz_quistion.="<center>"; echo $quiz_quistion; BREAK; CASE "plugin_message_form": $message_form="<center>"; $message_form.="<form method='POST'>"; $message_form.="Subject"; $message_form.="<br>"; $message_form.="<input type='text' name='subject'>"; $message_form.="<br><br>"; $message_form.="Name"; $message_form.="<br>"; $message_form.="<input type='text' name='name'><br>"; $message_form.="<br><br>"; $message_form.="Message"; $message_form.="<br>"; $message_form.="<textarea cols='30' rows='20'></textarea>"; $message_form.="<br><br>"; $message_form.="<input type='submit' name='submit' value='send'>"; $message_form.="</center>"; echo $message_form; BREAK; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808308 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Yeah, why not use a switch statement like you just did? It's probably a better option than attempting to define functions within functions Personally I'd just make a class. Idk, either way though. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808315 Share on other sites More sharing options...
.josh Posted April 13, 2009 Share Posted April 13, 2009 hmm...sounds like what you are wanting to do is use a class. Perhaps you should read up on object oriented programming (OOP). Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808320 Share on other sites More sharing options...
redarrow Posted April 13, 2009 Author Share Posted April 13, 2009 This is what i got so far. oop no good, to hard. <?php $plugin="plugin_message_form"; function function_plugin_quiz(){ $quiz_quistion="<center>"; $quiz_quistion.='Do dogs eat human flesh!'; $quiz_quistion.="<br><br>"; $quiz_quistion.="<form method='POST'>"; $quiz_quistion.="<select name'quiz_quistion'>"; $quiz_quistion.="<option value='yes'>Yes</option>"; $quiz_quistion.="<option value='no'>No</option>"; $quiz_quistion.="</select>"; $quiz_quistion.="<br><br>"; $quiz_quistion.="<input type='submit' name='submit' value='send'>"; $quiz_quistion.="</form>"; $quiz_quistion.="<center>"; return($quiz_quistion); } function function_message_form(){ $message_form="<center>"; $message_form.="<form method='POST'>"; $message_form.="Subject"; $message_form.="<br>"; $message_form.="<input type='text' name='subject'>"; $message_form.="<br><br>"; $message_form.="Name"; $message_form.="<br>"; $message_form.="<input type='text' name='name'><br>"; $message_form.="<br><br>"; $message_form.="Message"; $message_form.="<br>"; $message_form.="<textarea cols='30' rows='20'></textarea>"; $message_form.="<br><br>"; $message_form.="<input type='submit' name='submit' value='send'>"; $message_form.="</center>"; return($message_form); } switch ($plugin){ CASE "plugin_quiz": echo function_plugin_quiz(); BREAK; CASE "plugin_message_form": echo function_message_form(); BREAK; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808322 Share on other sites More sharing options...
.josh Posted April 13, 2009 Share Posted April 13, 2009 so...you think setting up a system to pass a function's name and running it based on the name is somehow easier? Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808325 Share on other sites More sharing options...
redarrow Posted April 13, 2009 Author Share Posted April 13, 2009 well i done it and find it easier then oop thanks. But like you say, oop will be worth studying. SOLVED <?php $plugin="plugin_quiz"; $function_switch_info=array("plugin_quiz"=>"function_plugin_quiz","plugin_message_form" =>"function_message_form"); function function_plugin_quiz(){ $quiz_quistion="<center>"; $quiz_quistion.='Do dogs eat human flesh!'; $quiz_quistion.="<br><br>"; $quiz_quistion.="<form method='POST'>"; $quiz_quistion.="<select name'quiz_quistion'>"; $quiz_quistion.="<option value='yes'>Yes</option>"; $quiz_quistion.="<option value='no'>No</option>"; $quiz_quistion.="</select>"; $quiz_quistion.="<br><br>"; $quiz_quistion.="<input type='submit' name='submit' value='send'>"; $quiz_quistion.="</form>"; $quiz_quistion.="<center>"; return($quiz_quistion); } function function_message_form(){ $message_form="<center>"; $message_form.="<form method='POST'>"; $message_form.="Subject"; $message_form.="<br>"; $message_form.="<input type='text' name='subject'>"; $message_form.="<br><br>"; $message_form.="Name"; $message_form.="<br>"; $message_form.="<input type='text' name='name'><br>"; $message_form.="<br><br>"; $message_form.="Message"; $message_form.="<br>"; $message_form.="<textarea cols='30' rows='20'></textarea>"; $message_form.="<br><br>"; $message_form.="<input type='submit' name='submit' value='send'>"; $message_form.="</center>"; return($message_form); } foreach($function_switch_info as $key=>$val){ switch ($plugin){ CASE "$key": echo $val(); BREAK; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808327 Share on other sites More sharing options...
redarrow Posted April 13, 2009 Author Share Posted April 13, 2009 Here a test, for the dynamic plugin's what it all about. I wanted a way to have dynamic plugin's for each page, so user's only have to update one page. and add the new dynamic plugin to the page. below full example. plugin.php <?php session_start(); $plugin=$_SESSION['pluged_in']; $function_switch_info=array("plugin_quiz"=>"function_plugin_quiz","plugin_message_form" =>"function_message_form"); function function_plugin_quiz(){ $quiz_quistion="<center>"; $quiz_quistion.='Do dogs eat human flesh!'; $quiz_quistion.="<br><br>"; $quiz_quistion.="<form method='POST'>"; $quiz_quistion.="<select name'quiz_quistion'>"; $quiz_quistion.="<option value='yes'>Yes</option>"; $quiz_quistion.="<option value='no'>No</option>"; $quiz_quistion.="</select>"; $quiz_quistion.="<br><br>"; $quiz_quistion.="<input type='submit' name='submit' value='send'>"; $quiz_quistion.="</form>"; $quiz_quistion.="<center>"; return($quiz_quistion); } function function_message_form(){ $message_form="<center>"; $message_form.="<form method='POST'>"; $message_form.="Subject"; $message_form.="<br>"; $message_form.="<input type='text' name='subject'>"; $message_form.="<br><br>"; $message_form.="Name"; $message_form.="<br>"; $message_form.="<input type='text' name='name'><br>"; $message_form.="<br><br>"; $message_form.="Message"; $message_form.="<br>"; $message_form.="<textarea cols='30' rows='20'></textarea>"; $message_form.="<br><br>"; $message_form.="<input type='submit' name='submit' value='send'>"; $message_form.="</center>"; return($message_form); } foreach($function_switch_info as $key=>$val){ switch ($plugin){ CASE "$key": echo $val(); BREAK; } } ?> page1.php <?php include("header.php"); if(isset($_SESSION['pluged_in'])){ unset($_SESSION['pluged_in']); }else{ $_SESSION['pluged_in']='plugin_message_form'; } session_start(); if($_SESSION['pluged_in']='plugin_message_form'){ require_once("plugin.php"); $_SESSION['pluged_in']='plugin_message_form'; include("footer.php"); } ?> page2.php <?php include("header.php"); if(isset($_SESSION['pluged_in'])){ unset($_SESSION['pluged_in']); }else{ $_SESSION['pluged_in']='plugin_quiz'; } session_start(); if($_SESSION['pluged_in']='plugin_quiz'){ require_once("plugin.php"); $_SESSION['pluged_in']='plugin_quiz'; include("footer.php"); } ?> index.php <?php include("header.php"); include("footer.php"); ?> header.php <html> <head> <title>welcome</title> </head> <body> <center> <h1>welcome to my web site</h1> <br><br> <a href="page1.php">message me</a> <br><br> <a href="page2.php">quiz</a> <br><br> footer.php <br><br> <hr> redrrow test <hr> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808378 Share on other sites More sharing options...
.josh Posted April 13, 2009 Share Posted April 13, 2009 unless I"m misunderstanding you, all you are wanting to do is have a central file like functions.php, and include that on all other pages. That way, if you add another function, you can just put it on that one page functions.php, and any page can access it. Is that right? Well then dude, you are way over complicating this... You don't need any of those session vars or conditions or nothing. You don't need to be passing any of that stuff man. Just put the functions on the page and when you include the file on the page, the page automatically has access to whatever is in there. It boggles my mind that you think what you've done makes any kind of sense, whatsoever. I'm not calling you stupid. I just can't for the life of me figure out what the hell goes on inside that head of yours. You are an anomaly. You deserve some kind of on-going study about, well, you. We should start a thread detailing your actions around here. Make a documentary. I bet discovery would eat that shit up. You'll be famous! Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808400 Share on other sites More sharing options...
redarrow Posted April 13, 2009 Author Share Posted April 13, 2009 You make me lath,that was a insult but also a cool joke. I was creating a function file, that sits on a server, where the users have no access and, i can re create there stupid requests, via one page, every think is dynamic. my i dears look crazy, and are crazy but i still get buy. your way is the correct way and i no that but wanted to be diffrent. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808403 Share on other sites More sharing options...
.josh Posted April 13, 2009 Share Posted April 13, 2009 nah man, not trying to be insulting, just honest. I can never understand half the things you say. I know english isn't your first language. I'm pretty sure you've mentioned that, a few times. But seriously, I'm dying to know, Do you know english, at all? Again, not calling you stupid. You don't have to know english to be smart. In fact, learning english probably makes you stupider, if anything. I envy you; all I know is english. I just get the impression sometimes that you take what is posted, run it through google translator, respond to it in your native language, run it through the google translator to english, and post that. Again, no offense. I just find stuff like this interesting. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808405 Share on other sites More sharing options...
redarrow Posted April 13, 2009 Author Share Posted April 13, 2009 yes i am English lol, and from London but blind, so i can not understand every think so quickly like the average person, and yes, i use a speaking program to understand posts. i am not thick or got no brain or a retard as you think. done well in life till i lost all my eye sight but love life. why don't you close your eyes, and sit in the black and type and see it it easy, then try to do programming. then ask agin. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808411 Share on other sites More sharing options...
.josh Posted April 13, 2009 Share Posted April 13, 2009 I'm not 100% blind, but enough to be considered legally blind. I do not attribute my slow thinking to my lack of eyesight. Lord knows, I have plenty of other things I could blame my slow thinking on. As mentioned, not making fun of you; just curious. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808416 Share on other sites More sharing options...
redarrow Posted April 13, 2009 Author Share Posted April 13, 2009 Maybe i find it hard to fully remember all the php functions and codes, there no magic pill to remember every think. i find php hard at times, and frustrating when getting it wrong. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808432 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 You're actually blind and you can type? That's quite impressive. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808555 Share on other sites More sharing options...
.josh Posted April 13, 2009 Share Posted April 13, 2009 umm...what's so impressive about typing without looking at the keyboard... Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808588 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Nothing. That's easy. But not being able to see what you're typing isn't. Well, I wouldn't know, but I'd imagine it isn't particularly helpful. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808681 Share on other sites More sharing options...
.josh Posted April 13, 2009 Share Posted April 13, 2009 I can imagine not catching typos being a problem (close your eyes, type away!), but the point is that being blind does not impair your ability to use proper grammar. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808686 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 I guess not. It could be a bit of motive to not bother typing full sentences though... Idk. Anyway yeah, classes aren't actually that hard. They're basically just containers for functions, but with a bit more useability. Worth learning tbh. Plus apparently, you get paid more for knowing OOP. Not that I've been paid for programming ever before; but that's what I've heard. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808710 Share on other sites More sharing options...
.josh Posted April 13, 2009 Share Posted April 13, 2009 You'd be surprised how many people in the paid-for-web-dev world use classes but don't understand the point, and it coulda just been written as regular functions. You constantly hear how there's a high demand for web developers and you scratch your head wondering how that can be true when it seems like every kid on the planet is an aspiring script-kiddie...and that's the key phrase right there: script-kiddie. A good chunk of people out there getting paid to do this stuff don't really know all the ins and outs of it. So when you hear that there's a high demand for web developers, what people are really saying is there's a high demand for good web developers. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808716 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Lol well, we were all script kiddies once Well...I just made an sql class which basically has all of the "useful" sql functions. At the moment I'm using mysql, but if I wanted to use sqlite or something else, I could easily just change this class a bit and my whole site would be sqlite compatible. That's a good reason for using a class surely? It wouldn't work as individual functions, as they all need to use the same vars. Well...it would, but I'd need to global or define a whole load of stuff. Quote Link to comment https://forums.phpfreaks.com/topic/153798-solved-how-do-you-call-a-function-within-a-function-please/#findComment-808724 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.