[PHP][Tutorial]Functions

Joined
Nov 6, 2007
Messages
880
Reaction score
11
A function is just a name we give to a block of code that can be executed whenever we need it. This might not seem like that big of an idea, but believe me, when you understand and use functions you will be able to save a ton of time and write code that is much more readable!
This is how you make a function :D.
Open up notepad, or what ever you use to write your php,
PHP:
<?php
function hello() {
echo "hello world";
}
hello();
?>
On the first line we start the php, then on the second line we create the function "hello()", on the third line we write what the function will do, then on the fourth line we call up the function, and on the last line we close the php, Ok now if you were to execute that you should see "hello world';.
Ok now lets say we wanted it to hello to what ever name we type in :D, so this is ho we do it
PHP:
<?php
function hello($name) {
echo "hello $name";
}
hello("nameyouwanttoputin");
?>
Ok so now on line 2 we added a variable, then on like 3 we echo $name, then on line 5 we execute the function with "nameyouwanttoputin", Ok so what happens is when you execute the function "hello" and put something in bweeten the (), you define $name, So if you were to exeucte hello(), it should say "nameyouwanttoputin", or what ever yoyu put bweeten the () on the 5th line.
You could do as many variables as you like,
PHP:
<?php
 function hello($name, $lastname) {
 echo "hello $name $lastname";
 }
 hello("HotelUnder", "Seige");
 ?>
Ok so we put in 2 variables, then on the 5th line we defined them, it would echo hello HotelUnder Seige, So that was simple enough right? Well hope it helped :D
 
Last edited:
What Makes You Think That Nub im Pro PHP I bet You Dont Even Know What PHP Stands For,
Dont Mess With The Best,
When You Chill With The Rest (Noob Rests Ahahah)

The fact you capitalize everything.
Oh here come some 1337 skillz.
"i knoewz wat php iz!!!!1111!!!One"

I've never used custom functions, so this helped me quite a bit.
Thanks.
 
Anyone who is knowledgeable in an area doesn't act like a bloody twat. Be humble, and thankful for what you do know... Just because YOU think it's easy and not helpful at all doesn't mean that it won't help a newbie. Also, stop using 'nub' and 'Pro.' First of all, I doubt you can tell me the difference between newb and noob without looking it up, secondly, that nub crap is stupid gaylo idiot talk, and third of all, this is a forum where 'programmers' support each other. 'Coders' are welcome, but if you can't teach it, then shut the hell up.

Back to the topic, well done mate. Although, next time, try and include a small php web page to show them what their results would be., you know, like using the input methods to change background color, as well as displaying text. You could even show them basic input boxes, or something similar, and show them how they can use that to trigger and manipulate the method to do on the fly transformations. I'm not as good at PHP as I should, but the function is similar to the way I'd do it in java

PHP:
 private final void setPageProperties (java.awt.Color color, String name) {

    pageBackground = color; // main color of page
    titleBarName = name; // sets title to "My web page, by " + name
    updatePage(); // updates and refreshes content to reflect these changes
}

That is, assuming that I'm using a small webserver in java :3
 
Actualy this tut is stupid.

1) Take vars out of quotes.
2) Do not echo in a function.
3) Use tabbing

You haven't told about Public, private and abstract functions. This is just beginner PHP...
 
Any tutorial is worthy, as not everyone is not an elite coder like you. I don't see why you guys oppose simple tutorials, questions etc, when you once didn't know how to do the same thing. But now that you do, you act like everyone who does not know how to do it is no worthy of knowing. Which is quite selfish of you, regardless how you found out, you should be open to helping those who don't know as much as you, or simply allowing it, rather than opposing it. Anyways, great tutorial, and I'm sure that it'll help some of the not so elite coders we have here.
 
a tip if your going to make a if, foreach, for with only one line.. no need for braces

That's true. But I generally find that code harder to read. Of course it's entirely a personal matter, but I find braced code even for one-liners be more complete and clear. As long as you're consistent with your code-style :)
 
he means:
PHP:
<?php
if($hi==TRUE)
echo ' hello ' ;
else
echo 'hi';
?>

In which case I'd rather have brackets xD

PHP:
<?php

if ($hi)
{
     echo 'Hello';
}
else
{
     echo 'Hi';
}

?>

For me, when code gets too compact, it's really hard to read. I even add rows of slashes (////////) just to separate things and stretch the code.
 
PHP:
<?php
function mathAdd($num1, $num2){
$answer = strval($num1) + strval($num2);
return $answer;
}
?>
 
he means:
PHP:
<?php
if($hi==TRUE)
echo ' hello ' ;
else
echo 'hi';
?>


I personally read simple if statements fine using this method:
bool ? if_value : false_value;
PHP:
echo ($hi==TRUE) ? 'hello' : 'hi';

But that's just me. If I'm using a system that will always only be one line, I'll do this method. If there's a possibility I'll put in more lines in the future, I'll use a longer statement just in case. However, for a function where it only takes a condition and returns true or false.. Like a function that checks if a user is a certain rank

PHP:
function check_rank($rank,$allowed_ranks) //$rank is needed, $allowed_ranks is optional array/PSL
{
    if(strlen($rank)>0)
    {
        if(strlen($allowed_ranks)<1)
        {
            $allowed_ranks = 'Admin';
        } 
        
        $allowed_ranks = ( count($allowed_ranks)>0 ? explode('|',$allowed_ranks) : $allowed_ranks );
        
        $bool = if(preg_match('/'.$allowed_ranks.'/i',$rank)>0;
        return $bool;
    }
}

$permission_to_enter = check_rank($_SESSION['rank'],'Admin|Mod|Registered');
echo ( $permission_to_enter ? 'You May Pass.' : 'You Shall Not Pass!' );

Notice this line:
echo ( $permission_to_enter ? 'You May Pass.' : 'You Shall Not Pass!' );

There's really no need for further work on this line.It's simple, if the rank is allowed, say it's allowed. If not, say it's not allowed.

Simple, no? :w00t:


EDIT: Still, you all should look up and listen hard to Daevius, he pretty much taught me everything, so take me as a grain of salt in comparison. I'm just showing you all some other organization methods that you might personally like too ;)

It is always nice to keep decipherable code. I remember not too long ago I didn't understand the bool?true:false; method, and saw it as Chinese or something.. Now it makes more sense than english to me.. Do whatever is the best compromise between "best for you," and "best for the world," Always ;)
 
Back