Does this piece of code work for you? And if you make test an empty string instead of 7?PHP Code:function check_rtn ($moves){
if (is_int($moves) && $moves !=''){
echo "Yes yes it's a number and you caught it";
}
}
$test = 7;
check_rtn($test);
| SitePoint Sponsor |

Does this piece of code work for you? And if you make test an empty string instead of 7?PHP Code:function check_rtn ($moves){
if (is_int($moves) && $moves !=''){
echo "Yes yes it's a number and you caught it";
}
}
$test = 7;
check_rtn($test);
No that didn't work. I got this error with both
( ! ) Fatal error: Cannot redeclare check_rtn() (previously declared in C:\wamp\www\userlogin\writing functions.php:39) in C:\wamp\www\userlogin\writing functions.php on line 55
But the way you explained in your last post (commenting out echo and just call the function) did the trick with the other function.
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget
I'm gonna put my head on the block for this but if you add the '&' symbol to the variable arguement (even though it's a global) would that make a difference..?
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget
Good to know Im getting somewhere but on the other hand all this time I thought the string in the "function check_rtn" was displaying because the function thought that $moves was the global from "function_adding" and that it was returning an integer value of 150, which is the value that "function_adding" created.
Thanks again guys![]()
No it didn't "recognize" it. You sent it:
That's what function arguments are for.Code:function check_rtn ($moves) { if (is_int($moves) && $moves !='') echo "Yes yes it's a number and you caught it"; } echo check_rtn($moves);
Sure, global variables may seem easier to use (no need to write them out each time you call the function), but they are easier to cause errors as well.
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget
No it's the variable $moves that you created in the script (before you called the previous function). If it didn't exist in the script already, there would be nothing to become global inside the function.
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget
ahhh man my brain is now completely fried... The only place I declared the global $moves was in a previous function. Ive just deleted the global part of it and the string still displays but im getting another now saying "undefined variable"
I thought the whole point of a global variable was so you can use it out of the local scope of a function, hence call it somewhere else.
Someone please shoot me.
Thanks mate, Im getting all mixed up because Im stressed. I'm sure 'define; is confusing me somewhere in here aswell.
If I was to remove global, '15' wouldn't be parsed...PHP Code:function adding(){
global $moves;
$moves = $moves * 10;
}
$moves = 15;
adding();
echo "$moves <br/>";

Ok, stop. This is very bad.
Functions return values so that they do not affect the state of the outside code. If I call "foo($x);" I expect $x to remain unchanged after the function call. Receiving the argument by reference, changing its value and not issuing a return leads to extremely hard to debug code.
From outside the function, if I want $x to change then I'll assign the returned value of the function back to $x.
$x = foo($x);
Why $x has changed is now clear without having to look up the function definition.
Including global variables in a function is very bad for the same reason - the function's behavior becomes non-intuitive from the outside code.
Fundamentals time. A function is an operation definition - no more or less. The simplest of functions are little more than aliases for operations.
Code php:function sum($a, $b) { return $a + $b; } function isGreater($a, $b) { return $a > $b; }
You can have the computer react to the return by putting it in a conditional
Code php:if (isGreater($a, $b)) { // do this if function returns true } else { // do this if function returns false }
Or you can store the return of the function in a variable for later reference.
Code php:$x = sum($a, $b);
Functions don't have a concept of 'fail' or succeed. A function that echos a bunch of text to the browser doesn't need to return anything.
Since functions are operation aliases at heart, they can be "nested" - that is functions can take the returns of other functions as arguments.
Code php:function double($a) { return $a*2; } $x = sum(double(6), double(2));
When you do this keep in mind that functions near to the top of the order of operations as follows -- parenthesis, functions, exponents, multiplication, addition.
If you do have a function that needs to convey an error state it can throw an exception - but this is an advanced topic for later.
Most important thing to take from this -
Do not get in the habit of having functions change variables that don't belong to them when they are called.
Almost as important: Do not use global. It's a bad habit that will haunt you at the advanced level. For that matter do not use or worry with assign by reference yet. It just confuses the issue. And in my opinion assignment by reference in PHP causes more problems than it solves - the problem it is meant to solve doesn't even exist in PHP anyway so I don't use the darn thing.
Finally, return is for giving the result of the function's operation to the calling code.
Thanks Michael, really helpful advice. Nice to know that functions can actually work quite simply and that is perhaps their best way to use them.
I wrote this just before I read your post.
I've noted from others and yourself that there is no real need to return if just displaying a string but I parsed some values into the variables just to try and see if I could do something that worked.PHP Code:function sarnie($bread , $filling, $sauce){
return "$filling is nice with $sauce in a $bread bread sandwich";
}
echo sarnie("white", "Tuna", "mayonaisse");
I also tried this way which Im a bit unsure about.
I'm not sure how to call the second one though, could someone help me with the syntax...?PHP Code:function sarnie2($bread = "white", $filling = "Tuna", $sauce= "mayonaisse"){
return "$filling is nice with $sauce in a $bread bread sandwich";
}

For that function, what you are doing is setting default values for parameters that aren't passed to the function. So, if I call the function and set bread and filling, sauce will take on its default value because it wasn't passed.
In the first of these cases, you are passing all 3 variables so the default values have no effect. In the second case, $sauce isn't passed, so it automatically assumes the value 'mayonaisse'. In the 3rd example, only $bread is set, so $filling and $sauce take on their default values. In the last example, nothing is set, so everything takes its default value.PHP Code:sarnie2('brown', 'cheese', 'ketchup')
sarnie2('brown', 'cheese');
sarnie2('brown');
sarnie2();
So you'd expect:
Code:cheese is nice with ketchup in a brown bread sandwich cheese is nice with mayonaisse in a brown bread sandwich Tuna is nice with mayonaisse in a brown bread sandwich Tuna is nice with mayonaisse in a white bread sandwich


This may have been answered before...
I wrote the code out for instructional purposes. Basically i wanted to show you what COULD be done.
In programing there are two ways you can JUDGE someones work... functional and elegant. You can be code can be functional, working perfectly, and completely inelegant ( and flame worthy). Strive for elegant, even if you are a bit confused.
so... to explain my rationale:
1) wanted to show that you can have as many RETURN statements in a function as necessary.
2) that if you want to have a function that checks if something sis true or false you need to return at least TRUE or FALSE ( the return statement by itself does nothing BUT escape the function, it doesn't actually send a value back UNLESS you tell it to)
3) Yeah you COULD echo the statement in a function.. in fact some functions do nothing but ECHO things, but...
4) ...it is more elegant and more logical, and also more flexible in the case i was out lining that the function merely checked.
5) a fuction is a mini program that can be used as many times as needed. so, when devising functions try to make them as flexible for OTHER uses as possible... if I had echoed in the function then that means the function could only be used to say "yes its a number..." or that additional code would be needed to tell it that the echoing is not wanted at that particular instance... the way I wrote it I can use that function at any other time in the fututr when I need to check that something is a number...
see.
Oh and expounding on what StromR said...
the another benefit of setting defaults, is that a function will throw an error if it doesn't get all the argument its expecting. Setting defaults at least prevents those errors ( tho it isnt always practical to set ALL your variables to default). You also have to remember that if you need to pad for defaults...
what I mean by that is , well lets say you needed to change the filling and only the filling.
sarnie2( 'ham').. will output:
"Tuna is nice with mayonaisse in a ham bread sandwich"
this is because the first argument always goes to the first default...
so if you change is in the second argument for example.... you need to remember to pad ( that is to include preceding arguments)
sarnie2( 'brown,'ham'); // the last argument doesn't need to be changed in this case...
Brilliant ideas, elegant execution.
Graphic Design, Art Direction, Copywriting and Web Design.
Right ok nearly there lol. Once again guys thanks for all your patience and time. Im not sure there is a place in the world that can cover functions more than in this post, every angle seemed to have been covered.
Today I'm gonna crack on and write some functions and look into why some functions have more than one return (as in phoenix example, cheers man :-) )
Thanks again and like I said before, it's great to know so many people are willing to help when someone doesn't understand.
Have a great day guys wherever you are
If you remove 'global', the variable $moves would be unknown inside the function.
(1) Here you define the variable $moves and give it the value 15. The fact that the function code is positioned before this line means nothing. The code inside the function (and therefore also the global) is executed only when the function is called (see (2)).Code:function adding() { global $moves; // <-- (3) $moves = $moves * 10; } $moves = 15; // <-- (1) adding(); // <-- (2) echo "$moves <br/>";
(2) The function is called without passing the $moves value as an argument
(3) Inside the function, $moves is undefined (out of scope) until you declare it global. From that moment on, this function (and only this one) is able to use the $moves variable you declared in (1).
What you can not do is this (I remember making this mistake):
I thought this would make $moves global so it could be used everywhere. But that's not how it works.Code:function adding() { $moves = $moves * 10; } global $moves = 15; adding(); echo "$moves <br/>";
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget

It might help you understand things by using this analogy:
Think of a function as an expert at one thing.
Let's call one expert Joe and another expert Jane.
Joe is really good at adding numbers, so whenever you need to know the sum of two numbers, you ask Joe.
Jane is really good checking lists to see if they contain a specific item, so when you want a list checked, you ask Jane.PHP Code://Joe function
function addNumbers($first, $second) {
return $first + $second;
}
So who are you? You are the rest of the code in your application.PHP Code://Jane function
function isInList($list, $item) {
if ( in_array($item, $list) ) {
return true;
} else {
return false;
}
}
So let's put this all together.
As you (the application) are being processed, you come across the need to add 32+65. Instead of doing the math yourself, you call out to Joe, "Hey Joe, what's 32+65?" Joe yells back, "97," and you carry on processing.
Then you call out to Jane, "Hey Jane, is that number in my list?" and she calls back, "No"PHP Code://Calling Joe
$sumOfNumbers = addNumbers(32, 65); //97
If Jane says the number is not in your list, you add it to the list.PHP Code://Calling Jane
$myList = array(2, 3, 5, 7, 11, 13);
$inList = isInList($myList, $sumOfNumbers); //false
And check once more with JanePHP Code://Adding it to list
if (!$inList) {
$myList[] = $sumOfNumbers;//$myList = array(2, 3, 5, 7, 11, 13, 97);
}
PHP Code://Calling Jane again
$inList = isInList($myList, $sumOfNumbers); //true
Dude I love examples like this!! Coding can become quite a slug sometimes but keeping it fun and interesting is the key to learning anything, thanks man.
Once again big shout out to Guido aswell, I'm actually getting there now!!
When it's clicked I will post some good functions on here. There is so much good information now that any noob getting stuck with this will find his answers here.
Bookmarks