MarcusZ Posted June 26, 2010 Share Posted June 26, 2010 Hi, I have some problems with my code which does not work when it is inside a function. When i test the same code "outside" the function it works fine. Any ideas? $shorttable = strstr($table, "</tr>"); I have checked that the function is runned, since it echoes some text. But it the strstr() function does not work in the function. table(); function table(){ $shorttable = strstr($table, "</tr>"); } Very strange. thanks Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/ Share on other sites More sharing options...
MarcusZ Posted June 26, 2010 Author Share Posted June 26, 2010 I solved it myself. The vairables outside the function aren't avalible inside the function unless you set them in a parameter. table($table); function table($table){ $shorttable = strstr($table, "</tr>"); } Is it possible to make all variables avalible inside the function without setting them as a parameter? Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1077594 Share on other sites More sharing options...
bluejay002 Posted June 26, 2010 Share Posted June 26, 2010 who is $table? is it a variable inside the function? I suppose that $table is not initialized inside the function but rather outside the function. pass it as an argument to the function, receive it as a parameter $table inside the function declaration and it should work fine. bluejay, EDIT: oh okay... you already got it. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1077595 Share on other sites More sharing options...
bluejay002 Posted June 26, 2010 Share Posted June 26, 2010 I don't advise you do it that way because it beats the purpose the way function should be used. I even suggest to use Object-Oriented approach in doing so and avoid using "variable anywhere". My two cents. bluejay, Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1077596 Share on other sites More sharing options...
PFMaBiSmAd Posted June 26, 2010 Share Posted June 26, 2010 MarcusZ, the whole point of functions is you can write any code using local variables inside the function that you need to accomplish the task you have designed that function to do, without needing to worry or keep track of what the main program is doing. The last thing you want in a 1000 line program that is using dozens of functions is to need to keep track of and keep separate all the variables that are being used inside of and outside of functions and to rewrite function definitions just because you happen to be using the same name variable or parameter in more than one function. So, no the main program variables are not automatically available inside functions and you are supposed to pass any values into a function as parameter(s) when you call the function. You are also supposed to return the value that the function produced to be used or assigned in the main code. Doing the above allows you to write and test a function once, then use it in any code needing that function without needing to keep track of what variables are being used inside the function(s) in your code or rewriting functions just to change the name of variables to avoid conflict in larger programs. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1077610 Share on other sites More sharing options...
MarcusZ Posted June 26, 2010 Author Share Posted June 26, 2010 Thank you very much for your replies. I now understand that functions are "isolated" from the main program. What if I would like to return multiple variables from a function. Is an array the only solution in that case? Are functions and methods equivalent? Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1077621 Share on other sites More sharing options...
bluejay002 Posted June 28, 2010 Share Posted June 28, 2010 Yes, in PHP, we call that functions. In Java, we call that methods but the whole idea is the same. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1078083 Share on other sites More sharing options...
trq Posted June 28, 2010 Share Posted June 28, 2010 Yes, in PHP, we call that functions. In Java, we call that methods but the whole idea is the same. Methods are functions that belong to a class or object. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1078085 Share on other sites More sharing options...
bluejay002 Posted June 28, 2010 Share Posted June 28, 2010 Methods are functions that belong to a class or object. A rather more precise answer in general terms. In PHP, functions inside a class is still called function but are somewhat different with the one declared outside so calling the functions inside a class as methods would make things easier to understand. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1078090 Share on other sites More sharing options...
Adam Posted June 28, 2010 Share Posted June 28, 2010 What if I would like to return multiple variables from a function. Is an array the only solution in that case? Yeah generally you'd use an array to return multiple values, but you could also pass back an object with properties set. Methods are functions that belong to a class or object. A rather more precise answer in general terms. In PHP, functions inside a class is still called function but are somewhat different with the one declared outside so calling the functions inside a class as methods would make things easier to understand. So.. 'the whole idea' isn't the same then, is it? Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1078095 Share on other sites More sharing options...
bluejay002 Posted June 28, 2010 Share Posted June 28, 2010 @Mr. Adam: The whole idea is the same since methods are functions are essentially the same if you look at it at the functions point of view. But it will be no if we look at it at the perspective of a method as a functions inside a class. Well, to make things easier for you, let's just settle to no, its not. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1078097 Share on other sites More sharing options...
Adam Posted June 28, 2010 Share Posted June 28, 2010 The whole idea isn't the same. A method IS a function within a class or object, just like a property is a variable within a class or object. They have different names to disassociate them from one another. I don't get what you mean when you say "look at it at the functions point of view"? Sorry but your last comment is completely meaningless. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1078101 Share on other sites More sharing options...
bluejay002 Posted June 28, 2010 Share Posted June 28, 2010 Well, yeah, maybe so but PHP call both of them as functions, not as functions and methods. Hope this clears up. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1078103 Share on other sites More sharing options...
trq Posted June 28, 2010 Share Posted June 28, 2010 Well, yeah, maybe so but PHP call both of them as functions, not as functions and methods. Hope this clears up. Um, functions declared within a class in php are called methods. I hope this clears things up on your end. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1078109 Share on other sites More sharing options...
bluejay002 Posted June 28, 2010 Share Posted June 28, 2010 Oh, is that so? My reading must have been faulty, my bad, correction well taken. thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/205930-outsideinside-function/#findComment-1078121 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.