Jump to content

Outside/inside function


MarcusZ

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.