Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Functions within functions

Posted on 2013-11-24
13
Medium Priority
?
196 Views
Last Modified: 2013-11-25
If I wanted a .php script to run many functions inside of one until it returned a true variable to the main function what would the syntax of that be?
0
Comment
Question by:burnedfaceless
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
13 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 39673542
Bad practise but...

while($some_obscure_variable != some_obscure_value){
      somerandomfunction1();
      somerandomfunction2();
      somerandomfunction3();
}
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39673651
Why is it bad practice?
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 39673739
a.php

<?php

function a1()
{
   b();
   c();
}

function b()
{
}

function c()
{
}

?>

I hope you want some information regarding class and object.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:William Nettmann
ID: 39673764
Maybe you need a recursive function?

function do_something_to($input) {
   // Do something to $input
   
   // Check the value of $input after doing something
   if ($input != "something") {
      do_something_to($input);
   }
   return $input;
}

Open in new window

This function will continue to call itself recursively until $input = something. If you provided more detail on what you want to achieve, I may be able to provide a more specific answer.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39673798
Actually, your question is too general.  Without test data for input and a goal for the output, it is impossible to give a good answer.  

When you consider that virtually all programming methods and structures were invented before 1970, I always search to see if my problem has been answered already.  And it usually has by people smarter than me.  I just have to provide the specific data and conditions for my application.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39674399
+1 for what @DaveBaldwin wrote.

This question makes it sound like you are new to programming or at least you do not have a depth of experience in computer science.  To that end, we have an article here at EE that will help you get the foundation you need.  Don't expect it to come all at once -- it takes time and structured learning to develop any activity to the point of proficiency!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Now to the specifics of your question, as much as can be discerned... Don't ever design an application like this!  The more nesting and complexity you bring to application design, the more likely your application will fail (Obamacare website).  Instead reduce the elements of application design to the SSCCE.  Then build the application back up using that collection of small, correct examples.  When you learn object-oriented design (perhaps the only meaningful advance in programming methods since 1970) you will find that the encapsulation of functionality makes a great deal of sense, and doesn't create anything as complicated as functions within functions.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39674411
@kaufmed
Because it may never leave the loop - and you end up with a infinite loop.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39674975
@GaryC123

You can code just about any loop into an infinite one! Does that mean all loops are bad practice  ; )

The short of it is I don't see anything wrong with the loop. As with anything in programming, care should be taken--in this case to avoid infinite loops (unless that is what you actually need!).
0
 

Author Comment

by:burnedfaceless
ID: 39675017
I'm creating a form validation but it runs into errors if I submit it past the normal amount of time regarding class (color) of what needs to be fixed, not the printing of the variables themselves.

Debugging wise I guessed it must be that the script doesn't run the section that determines variables aren't empty.

So I was thinking programming wise have a main function that has other functions to ensure it does everything it needs to every time.

An infinite loop would be desirable, or a counter that would stop eventually. That's really not necessary though.

edit: determines whether or not variables are empty and assigns the css class
0
 

Author Comment

by:burnedfaceless
ID: 39675081
I just debugged more and that is exactly what is happening because it won't accept valid submissions.

After the second submit it stops working. I'm going to check these codes and whichever one works gets points.
0
 

Author Closing Comment

by:burnedfaceless
ID: 39675097
This will work for what I'm doing. I just thought pass/returning a variable would be better but it's not much different function wise I guess.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39675119
If you want to post a more specific question, perhaps including a code example and some test data, we may be able to share some good design patterns.  Just a thought. ~Ray
0
 
LVL 58

Expert Comment

by:Gary
ID: 39675132
If all loops are bad, and bad is evil, are all loops evil?
Not saying all loops are bad, but without some get out of jail clause they are.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question