PHP functions
Sep 15
Today we’re going to look at what most programmers call functions.
A function is what you call in your program to tell it to perform a series of smaller actions, like echo(), substr(), mysql_query(), or your very own functions like calculate_age() or bake_cookies() (which we will learn how to create in the next tutorial).
I’ll be explaining some of the most basic PHP default functions you’ll definitely come across in 99.9% (with a 30-day money-back gurantee… wait I’m not a hosting service) of all PHP scripts, then guiding you on basic methods of usage of said functions.
Name me some basic ones
You’ve seen echo(), its brother print() and a couple more. Here are some other functions that you’ll probably come across often:
str_replace()- searches a big string and replaces a smaller string/pattern inside it with a new onedefine()- defines a constant value; we’ll learn more some other timedate()- returns a certain date format, of right now or a given timehtmlspecialchars()- converts all <, > and & in a string to their HTML entities, i.e. <, > and &empty()- checks if a variable is empty
We’ll learn more about the above functions in later tutorials, but for now here’s a little, uh, ‘primer’, followed by guides on how to use some other simple functions.
What’s this about language constructs?
You already know echo(). Actually, it’s not really a function, but a language construct, as in a keyword that should be used in a specific way in a PHP statement. Truth is, I can’t quite explain it, but just understand that echo() isn’t exactly a function so you can omit those parentheses:
Example 1: echo() with and without parentheses
<?php $str = 'Hello world!'; // Both are perfectly fine, but I prefer omitting parentheses. echo($str); echo $str; ?>
And yes, print() is a language construct too, as well as control structures like if() (you learned about this in the previous tutorial).
How do I use functions?
Functions can be called or used in a variety of ways, but I’ll just explain some common ones.
The first way, is to just call it by itself:
Example 2: calling a function on its own
<?php my_lovely_function(); exit; ?>
Such functions usually aren’t intended to have a return value so can be called by themselves, like echo(). A return value is a value that a function spits back to the program after doing its job. For example, if you made a function that squares a number, you want the function to return the result after squaring it.
Example 3: return value
<?php $num = 6; // We throw this number in as what programmers call a parameter or an // argument (don't worry, functions settle THEIR arguments pretty ethically), // then our function squares it and returns the result for us to finally echo. $sqr = square($num); echo $sqr; ?>
Most functions require parentheses wrapped around its arguments (I prefer that term to parameters, dunno why), separated by commas:
Example 4: some arguments
<?php // Too many arguments? Line breaks help to clear things up. do_stuff($alpha, 'bravo', $charlie, array( 'delta' => 'echo', $foxtrot, 'golf' ), $hotel); ?>
Functions with return values are just like the different types of data you handle. So you could stick in a function where a data type would go in something like a formula:
Example 5: what return values mean as well
<?php // Instead of this... $title = 'I <3 PHP'; $title = htmlspecialchars($title); $title = "<h2>$title</h2>"; echo $title; // You can always try this! Remember our concatenating operator? $title2 = '<h2>' . htmlspecialchars('I <3 PHP') . '</h2>'; echo $title2; ?>
You can even nest functions, i.e. pass functions as arguments!
Example 6: nesting functions
<?php // Long procedure? $ingredients = array( // I'd list them but it'd make this snippet too long! ); $pasta = prepare('pasta', $ingredients); $g = garnish('spinach'); $dish = serve($pasta, 'hot', $g); // Though the above is a bit more organized, give this a // whirl if you find such long code a hassle. $dish = serve(prepare('pasta', array(/* Your ingredients */)), 'hot', garnish('spinach')); ?>
Well, that’s it for the basics of functions in PHP. I hope you learned something from this tutorial
I heard I can make my own functions!
Like most other programming languages, PHP lets you define your own functions to fulfill your own tasks your way!
You will learn how to create functions in my next PHP post. Stick around ![]()