Functions make life easier
Joel Philip on 2002 May 10
    

When you first start learning PHP, there are so many aspects to the language. PHP has hundreds of builtin functions that make it possible to do powerful things. But it does not stop there since you can write your own

.

Lets face it, we become accustom using the same elements over and over and it's the time to explore simple things that will open up your perspective of coding.

I recently wanted to write a script that would convert someone's input of words and replace them with the "Hacker" spelling. So when the user inputted
an "A" letter it would become "@" or the letter "E" would become "3". YOu g3t th3 picture.

I had this in the back of my mind on how to make such function but nothing on paper looked right to me and all smelled as pages of coding. Until I discovered the "strtr" function. This function takes a string and translates it's characters into a given set of characters.

The best part about this function, it led me wrap up the script in only 12 lines of code!

Functions can really make it easier to get things done writing less code, that is why we are choosing PHP over some other languages - it has the reachest and the easiest function resources.




Hello World!

Let's explore how to write our own functions. Functions are really simple. First you declare one by the word "function". Like in this example below:


<?php

    
function hello() {
        echo 
"hello world";
    }


?>


Now, in order to use it, simply place your function in your script and call it like this:


<?php


    
function hello() {
        echo 
"hello world";
    }
    
     
hello();

?>



The output is: hello world


Make me bold!

Now, lets create a function that makes all of our text bold.


<?php


    
function boldtype($string) {
        echo 
"<B>$string</b>";
    }
    

?>


Okay, the rundown on this function is simple. First we decide what we want the function to do for us. Easy - we just want some text to become bold. Now, to make it happen we can pass this text to the function, and in the function it will appear the variable we have between the brackets "()".

Here's an example of using this function:


<?php


    
function boldtype($string) {
        echo 
"<B>$string</b>";
    }
    
    
boldtype("this text is bold");

    
// Prints: "<b>this text is bold</b>"
    

?>




Trim

Some of the builtin functions in PHP are very handy for preparing data. The "trim()" function, for instance, is great for removing "whitespaces" and "newlines" from a form field. Here's how you use it:


<?php


    $email 

        email@provider.com 
    
    "
;

    
$email trim($email);

    
// $email now became: "email@provider.com".
    // no spaces remained at the left and right sides.

    // Very, very useful to filter the user submitted data through a form


?>



If the user types some spaces in front of their email address, the "trim" function will get rid of them and return you the "trimmed" one.

AddSlashes

Another builtin function to use especially with databases, is the good old "addslashes()" function. This adds "slashes" before any quotes in your words as well as some other funny characters that could be risky if left alone. That way, when you retrieve the information, it displays correctly.


<?php


    $messages 
addslashes($messages);


?>



Now, Stip the off, please

If you used the "addslashes()" function with your script then the conpanion function is the one called "stripslashes()".

stripslashes() strips the "slashes" that were put in (logic, no?). That way you wont have parsing problems when the data is retrieved.


<?php


    $messages 
stripslashes($messages);


?>




Magic Quotes

One last function I want to go over since it gave me trouble in my first weeks of coding in PHP. I encountered the problem of extra slashes, everything I coded magically produced these and I could not figure it out why. After searching in forums on the web I found my problem and the cause. It was the PHP's feature called called "magic quotes".

If your server has it enabled, it will add slashes to all of your quotes whether you like it or not. Just as it would put everything you receive through a form through the addslashes() function. A quick way to find out if it is enabled is quite simple. Create a script with this in it:



<?php


    phpinfo
();


?>


Run it in your browser, and, miracle! Do it just for fun if you've never done it before - you'll never believe that such a small function can do that much:-) Anyhow, on that page, look up the information that says "magic_quotes". It probably is enabled to you.

If you have no control over your server, then there is an easy way to work around this problem by adding this small snippet of code in the beginning of your PHP file:


<?php


    set_magic_quotes_runtime
(0);


?>



That's all you need. A rundown on this function. It's setting the magic quotes to "FALSE". Zero equals "FALSE", One equals "TRUE". With that little piece of code, my headaches went away and I could concentrate on working and having one less problem.

My Conclusion

Little things like using functions can really make your life easier, as far as coding goes. Make it a point to learn a new function every day. Then, within exactly 7 years (2557 functions) you can say that you know it all! Actually, now that I think - I better advise you reading the manual or PHPBeginner.com :-)

Joel
a web developer and graphic artist from Southern California
Tentatively planning to Open Soon! (no dates ...) // Doing heavy development now...