Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] Functions

Joined
May 11, 2008
Messages
513
Reaction score
99
Okay functions are a simple way of organising code in a structured maner, I would always suggest using functions because you don't have to copy and paste the same code over and over again! It's amazing. You can even have two seperate php files linked with the include(); pre-set function (I'll show you in a bit ;) ). Which would save you time copy and pasting, I'd also like to save I've always been one from just looking at the code I learn from it, so i'm sorry if It's not explained that well. :$: any i'm just chatting 'breeze' now, so lets get to the hardcore sheit.


How to make a function
PHP:
<?php
 function YOURFUNCTIONNAME()
{

}
?>
Simples.:thumbup1:

How do you excute a function
PHP:
<?php
 function YOURFUNCTIONNAME()
{

}
// this line below will excute it! Yay ;]
YOURFUNCTIONNAME();
?>
How to get an output from your function
PHP:
<?php
 function YOURFUNCTIONNAME()
{
      return 'Bob';
}
echo YOURFUNCTIONNAME(); // Gets Bob
?>
Amazing Huh?
But thats pointless, not really Lets do some more Advanced stuff(well ish)
PHP:
<?php
 function YOURFUNCTIONNAME($str)
{
       return $str;
}
echo YOURFUNCTIONNAME("Bob");
?>
What about this now an IF statement in a function! :O
PHP:
 <?php
  function YOURFUNCTIONNAME($sr)
 {
      if($str != "Bob")
    {
          return "Who are you?";
    }
    else
    {
         return "Howdy Bob! :)";
    }
 }
// this line below will excute it! Yay ;]
echo YOURFUNCTIONNAME("Bob");
echo YOURFUNCTIONNAME("James");
?>

Enjoy, Any questions just ask below.
 
Junior Spellweaver
Joined
Nov 9, 2008
Messages
178
Reaction score
5
PHP:
   <?php
 function YOURFUNCTIONNAME($str)
{
       return $str;
}
echo YOURFUNCTIONNAME("Bob");
?>

I've just realized I could reduce the length of my code if I start using functions. So this is my first time using it on the second line
"function YOURFUNCTIONNAME($str)" What goes in here?
Is this a varible that has to be declared before i use the function? Or is that where I put the varible? :blush:
 
Infraction Baɴɴed
Loyal Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
ok.

functions are quite easy to understand until you get into more complex libraries of PHP. the basic function should be used to help reduce the amount of code line in a script.

how to understand functions goes as followed:

if you notice this in a script
PHP:
<?php
function displayString($str) {
	return $str;
}
echo displayString("A string to display");
?>

look at the script again and this time look at the function name and the echo line.

why is one displayString($str) and the other displayString("Text")? that is because the text you put in the echo string is the text your inputting into your function.

it is like putting
PHP:
<?php
function displayString("A string to display") {
	return "A string to display";
}
echo displayString("A string to display");
?>

now as to why its $str instead of the text is because it makes that function more dynamic.
ex:
PHP:
<?php
function displayString($str) {
	$output = "This line is before the string you inputted.<br />";
	$output .= $str;
	$output .= "<br />This line is after the string you inputted.";
}
echo displayString("This line is what I inputted");
?>

functions are not only limited to outputting text, have fun with them.

EDIT:
you do not have to declare a variable before using it like in other languages.
 
Last edited:
Skilled Illusionist
Joined
Apr 22, 2009
Messages
301
Reaction score
19
You're tutorial does not go as far as it should about functions, and you could talk about naming conventions because naming a function uppercase is not great.
 
Infraction Baɴɴed
Loyal Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
You're tutorial does not go as far as it should about functions, and you could talk about naming conventions because naming a function uppercase is not great.
its not great because PHP is case-sensitive.

but capping the first letter of the word in the name is good because you can have 2 variables look alike and they are totally different, also its a good way to organize the code.

ex:
$lineOfText != $lineoftext;
 
Skilled Illusionist
Joined
Apr 22, 2009
Messages
301
Reaction score
19
but capping the first letter of the word in the name is good because you can have 2 variables look alike and they are totally different, also its a good way to organize the code.

Yeah great idea so you can mess up with them so easily !

Btw I don't see why you quoted my post ?
 
(oO (||||) (||||) Oo)
Loyal Member
Joined
Aug 6, 2009
Messages
2,132
Reaction score
429
Also you can set default value for input variable like this:
PHP:
<?php
function displayString( $str = "This is dummy string" ) {
    $output = "This line is before the string you inputted.<br />";
    $output .= $str;
    $output .= "<br />This line is after the string you inputted.";
}
echo displayString("This line is what I inputted"); // Displays 1st line, this line, and last line.

echo displayString(); // Displays 1st line, "This is dummy line", and last line.
?>

Also you can have function with optional variables, but I forgot how to get those :rolleyes:
 
Infraction Baɴɴed
Loyal Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
@paulbub
i quoted you because your right. a all caps function name is bad.
 
Joined
Jul 26, 2006
Messages
3,634
Reaction score
1,007
Also you can set default value for input variable like this:
PHP:
<?php
function displayString( $str = "This is dummy string" ) {
    $output = "This line is before the string you inputted.<br />";
    $output .= $str;
    $output .= "<br />This line is after the string you inputted.";
}
echo displayString("This line is what I inputted"); // Displays 1st line, this line, and last line.

echo displayString(); // Displays 1st line, "This is dummy line", and last line.
?>

Also you can have function with optional variables, but I forgot how to get those :rolleyes:

Haha, you just kinda did :p
PHP:
function abc_multiply($a, $b, $c = 0)
{
    return $a * $b * $c;
}

echo abc_multiply(5, 2, 3); //will return 5 * 2 * 3 = 30
echo abc_multiply(5, 2); //will return 0, because $c = 0 and any number * 0 = 0
 
Last edited:
Joined
May 11, 2008
Messages
513
Reaction score
99
This was an example function YOURFUNCTIONAMEHERE() is a replacable text, if you hadn't have figured. ;/

I don't comprehend why having a all cap function is bad, if your stupid enough to think l is the same as L in computing your just asking for an error arn't you?

@Pro,

Nice, never really found the need to have a $str = "Dummy text";

But it's nice to know ;]
 
Joined
Jul 26, 2006
Messages
3,634
Reaction score
1,007
This was an example function YOURFUNCTIONAMEHERE() is a replacable text, if you hadn't have figured. ;/

I don't comprehend why having a all cap function is bad, if your stupid enough to think l is the same as L in computing your just asking for an error arn't you?

@Pro,

Nice, never really found the need to have a $str = "Dummy text";

But it's nice to know ;]

And now I explained how it can be used in creating optional parameters :rolleyes:
 
Skilled Illusionist
Joined
Apr 22, 2009
Messages
301
Reaction score
19
No offense but I honestly think that this tutorial should be removed or wrote again, because of the following : -It does not explain enough about functions in php, it does not explain much in fact.

-It will lead in bad programming practices.

edit: You might want to check this too :
 
Last edited:
Joined
May 11, 2008
Messages
513
Reaction score
99
I did say, I find it hard to explain things so I've shown you the input code and the output from the php.

It's preety simple? Isn't it not?
 
Skilled Illusionist
Joined
Apr 22, 2009
Messages
301
Reaction score
19
It isn't about me not understanding php (wich i already know), but about the way you wrote tutorial and stopped on not showing in wich way functions are useful in php, what kind of things can be achieved using those. I'll take your tutorial word by word, in fact you just showed bad examples why would you do a function to return a string given to it as parameter and then echo it? Of course this is a literal approach, but in fact the goal of a tutorial is showing the potential of the thing the tutorial deals with, not showing something wich is int fact useless, you could have shown something basic, but showing the potential of functions in php.

Yes, I did not give precise answers, because I am not the one who wrote a tutorial about php functions, and if you can't take constructive criticism, then it's sad for you.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
I think you should greatly improve this thread.

You need to answer questions like:
- What is an argument?
- Why would I want to assign a default value to an argument?
- Does PHP support functional programming?
- What does the "Variable Scope" refer to?
- Can variables in the scope of one function be called to from another, in PHP?
- Are function names in PHP case sensitive?
- Do I need to include my file with all the functions when I want to call them, in PHP?
- How much (if at all) do functions change between PHP4, PHP5, and PHP6?

Maybe that's asking for allot, but I expect at least half of those to be answered in a PHP functions tutorial... I'd love for all of them to be answered in this tutorial, so nobody has to make a new one.. -.-
 
Newbie Spellweaver
Joined
Aug 18, 2005
Messages
41
Reaction score
0
as a side note: if you pass a variable in a function, its called a recrusive function.

For better understanding picture it like this:

PHP:
$x = 1
$whatisx = _gimmesome($x)

MsgBox($whatisx) ; outputs 2

Func _gimmesome($x)

	$x=$x+1

	Return $x
EndFunc
 
Junior Spellweaver
Joined
Sep 14, 2005
Messages
119
Reaction score
15
This is your code

PHP:
 <?php
  function YOURFUNCTIONNAME($sr)
 {
      if($str != "Bob")
    {
          return "Who are you?";
    }
    else
    {
         return "Howdy Bob! :)";
    }
 }
// this line below will excute it! Yay ;]
echo YOURFUNCTIONNAME("Bob");
echo YOURFUNCTIONNAME("James");
?>

this is fixed one

PHP:
 <?php
  function YOURFUNCTIONNAME($str)
 {
      if($str != "Bob")
    {
          return "Who are you?";
    }
    else
    {
         return "Howdy Bob! :)";
    }
 }
// this line below will excute it! Yay ;]
echo YOURFUNCTIONNAME("Bob");
echo YOURFUNCTIONNAME("James");
?>
 
Experienced Elementalist
Joined
Apr 13, 2010
Messages
280
Reaction score
18
Nice tutorial for beginners :D nice work though you could add in your tutorial

Code:
<?
include_once('functions.php');
?>

This will clean the code up even more by using an external functions page
 
Back
Top