« Return to the blog

Tom - Director

Posted by Tom on 30th March 2012

PHP: How to write and call your own function (part two)

  1. How to call a built-in function in PHP (part one)
  2. How to write and call your own function in PHP (part two)
  3. How to safely call a dynamic function in PHP (part three)

Introduction

PHP has a library of built-in functions. Different functions are available depending on the PHP version you are running, and the extensions you enable. You can extend this library at run-time by writing your own functions.

This article demonstrates how to call a built-in function, how to write and call your own function, and how to safely call a dynamic function.

How to write and call your own function in PHP

You can extend the functions available in PHP by writing your own. Taking the scenario from part one, I'm planning on having lots of BBQs over the summer - but I don't want to have to copy & paste the code lots of times, only to change a couple of numbers. Instead we can encapsulate the code into a neat little function.

<?php 

// Your own function.
function sausageCalculator($attendees,$sausages_each)
{
    
$sausages_required $attendees*$sausages_each;
    
$sausages_required ceil($sausages_required);
    
    return 
$sausages_required;
}

// My first BBQ. 12 sausages.
echo "You need to buy ".sausageCalculator(9,1.25)." sausages!";

// Less people this time, but Joel is coming, so the average
// sausage consumption goes up considerably. 18 this time.
echo "You need to buy ".sausageCalculator(7,2.5)." sausages!";

?>

Note, you can also condense the sausageCalculator function into just one line of code.

<?php 

// This does exactly the same as the above, but
// instead carries out all the calculations in one
// line of code.
function sausageCalculator($attendees,$sausages_each)
{
    return 
ceil($attendees*$sausages_each);
}

?>

That's the end of part two. I hope you've mastered how to write and call your own functions in PHP. If you like this article, please show the love by leaving a comment, liking our Facebook page, or following @switchplane on Twitter. You can also follow me personally on Twitter @tom_fielder.

Read the next part

If you would like to continue reading, you can read the next part: How to safely call a dynamic function in PHP.

Building Maintenance says On 19th June 2013 at 20:50

Helpful info. Fortunate me I found your web site unintentionally,
and I am stunned why this accident did not took place earlier!

I bookmarked it.

Louis Vuitton Pas Cher says On 19th June 2013 at 20:36

Hi, after reading this amazing article i am also happy to share my know-how here with friends.

Tory Burch Flats says On 19th June 2013 at 20:27

whoah this weblog is magnificent i like studying your posts.
Stay up the good work! You know, lots of individuals are looking around
for this information, you could help them greatly.

Add A Comment

:
:
:
: Captcha

Recent Blogs

Tom-Director

PHP: How to redirect to another page

Short tutorial explaining how to redirect a user to another page with PHP. Read More »

Tom-Director

PHP: How to call a function - part three

The final of three articles explaining how to call functions in PHP. This part focuses on how to safely call a dynamic function in an OOP fashion. Read More »

Tom-Director

PHP: How to call a function - part one

Part one of three articles explaining how to call functions in PHP. This part focuses on calling the built-in functions available in PHP. Read More »

Michael-Developer

Twitter Tools

Do you use Twitter as part of your business marketing strategy. Here are some useful tips and tools to help you succeed. Read More »

Evie-Designer

Gradient Meshes in Adobe Illustrator

There's more to Adobe Illustrator than basic shapes and blocks of colour, and there's more to gradients than linear or radial effects. Read More »

Evie-Designer

Web Design Trends 2011

Web designers are shying away from creating gimmicky tricks, but rather clean, accessible, bug-free coding - that works. Read More »

Chris-Project Manager

Let's Do Business - Eastbourne 2011

Find out what Switchplane did at Let's Do Business Eastbourne 2011. Read More »

Joel-Director

Integrating with Kashflow using PHP

Find out how to use PHP's SoapClient class to integrate with Kashflow. Includes sample code and best practice tips. Read More »

Tom-Director

Improving Search with Levenshtein Distance

Find out how we've made Honey Barrett's document management system search facility handle spelling mistakes. Read More »

Joel-Director

The New Switchplane Website

Our new website is ready - read about what we've updated and why, and get ready for Project Awesome! Read More »

Michael-Developer

Website Accessibility

Have you ever considered the importance of accessibility on your website? Read More »

Chris-Project Manager

Networking Tips

Business networking tips and trips featuring the adventures of Norm! Read More »

Michael-Developer

How Amazing Databases Are

A blog post about the usefulness of databases, using a person management system as an example. Read More »

Christian-Designer

Reasons to use Vector Graphics over Rasters

When should you use vectors or rasters, and does it even matter? Read More »

Michael-Developer

The Importance of Variables

Variables are useful - make sure you use them properly! Read More »

Chris-Project Manager

Some thoughts on writing for your business website...

Read some tips on preparing copy for your website. Read More »

Chris-Project Manager

Let's Do Business - Hastings 2010

Switchplane attended LDB Hastings in 2010 - read about our day. Read More »

Chris-Project Manager

Lets Do Business Eastbourne 2010

Switchplane's experience at Let's Do Business 2010 in Eastbourne. Read More »

Evie-Designer

Web Design Trends 2010 Part 1

A guide to web design trends 2010, including hand-drawn and painted layouts, typefaces, modern vectors and large headers and footers. Read More »

Evie-Designer

Common Printing Problems and how to avoid them

Even if it looks good on your screen, it isn't guaranteed to come out like that! I've selected a few of the most common print problems to watch out for. Read More »

Evie-Designer

Web design trends 2009 Part 2: Old and Torn Paper

Make your website a real piece of personal artwork by collecting and scanning-in torn and crumpled paper and using it for the design of your website (or just take a visit to istock photo!). Read More »

Evie-Designer

Web design trends 2009 Part 1: Badges

Subtle elements and new layout ideas for 2009. My guide on how to make your website look fashionable and unique, packed full of information and further resources. Read More »

Tom-Director

The Joys of JQuery

JQuery is a light-weight, cross-browser and feature packed JavaScript library. To explain why JQuery is so good, first you need to know why not using it is so bad! Read More »

Vanessa-Account Manager

Lets Do Business Eastbourne 2009

Last Thursday (25th June) Switchplane attended their first Lets Do Business event at the Winter Gardens in Eastbourne. Read More »

Vanessa-Account Manager

Top 10 Sales Tips

Times are tough. Sales are down. Morale is low. Sound familiar? Well it needn't be. Below are a few tips which are sure to help you re evaluate and succeed in pushing your sales margins. Read More »

Evie-Designer

Create printable artwork in Indesign and Photoshop

A few useful steps to help you successfully prepare your artwork for print, using Adobe Photoshop and Adobe Indesign. Learn about colour modes, resolution, size and other helpful tips! Read More »

Cron-Administrator

Search engine ranking more important than ever

Search engine optimisation is now an essential part of a successful website. And because of the way search engines now work, the focus of SEO is now about producing quality content and getting your name out there so you get incoming links. Read More »

Cron-Administrator

Four Months at Switchplane

I've been at Switchplane for four months now - to celebrate the launch of our new website I'm sharing some thoughts on what it's like working here. Read More »

Joel-Director

How to run a cron job on the first weekday of the month

Often it's useful to generate automatic reports or perform some other task at the start of the month. Find out how to schedule a cron job to run on the first weekday of each month. Read More »