Skip to main content
Menu
CodeBabes
  • Create new account
  • Request new password

or

  • Sign in with Twitter
  • Code
  • Babes
  • Sign Up
  • Log In

Looks like you missed a quiz. Go back and pass that shit!


NO STRIP FOR YOU!

Share the Love

  • Tweet

PHP Functions

Functions in PHP are a lot like functions in math class. They take some input, do something, and then return some output.

Throughout this course we’ve been using the print function. This is a function PHP provides for us. There are whole bunch of these pre-made functions you can use for all sorts of things, more than a thousand, but you can also write your own functions.

Functions are there to make your life easier. Whenever you find yourself repeating a piece of code, use a function. That way, there's a single place to make changes. Take this for example:(gesture)

Functions are important for organizing code as well. It can get very difficult very quickly if you have all of your code in one long block. Functions allow us to group things logically, and make code easier to understand.

Let's make one.

We start with the word function, then the name, my_function, empty parentheses, and some braces.

We make the function give us back a value, using the return statement:(gesture)

This function is not very interesting. Let's add some arguments. Arguments are variables that get added when you call a function.

This function accepts two arguments, A and B. It then adds them together, and returns the value.

Our function can now be re-used to add any 2 numbers. woohoo

What if we tried to access the variable C, from outside the function, like: (gesture)

$value = my_function(1, 2);
print $c;

This will cause an error.
That’s because the variable c isn’t available from outside the function. This is known as variable scope. Any variables created inside the function are only available from within in it.

There is a way around this. Using the global keyword, you can declare that a variable is available from anywhere.

So, we could make c available like this:

Global variables should be avoided whenever possible. While having a variable available from anywhere seems handy, they make it very difficult to figure out who modified the variable. This leads to problems when debugging, and debugging is something you will be doing a lot of.

You just went all the way with PHP. How does it feel?

If you want some more action, check out our other videos.

Questions or Comments?

Lessons

  • PHP Variables
  • PHP Conditionals
  • PHP Arrays
  • PHP Loops
  • PHP Functions
PHP Virgin

Downloads

SignUp & Subscribe

What the What?

Lessons: 41

Much more to come!

50/50 Entertainment and Education. Learning doesn't have to be boring :)

Our Philosophy

Contact

  • support
  • Tweets!
  • Youtube
  • Google Plus
  • Facebook

Stuff

  • Blog
  • Sign Up
  • All Babes
  • All Courses
  • Become a CodeBabe!
  • MAKE A LESSON WITH CODEBABES!

Recent Courses

Internet Virgin
SEO Virgin
HTML Virgin
CSS Virgin
Programming Virgin with Javascript

Learn to Code with Babes

Copyright © 2025 CodeBabes.com • Made with

  • Privacy Policy
  • Terms of Use
Close Menu
  • Blog

  • All Courses
  • Android Virgin
    • Android Studio Tour
    • App Layout in Android Studio
    • Java Variables, Java Data Types & Android Views
    • Java Event Handling in Android
    • Android Intents, OOP This, Variable Scope
    • Passing the Data with Android Intents & Finish
    • Complete the Android App with Launcher Icons & APK
    • Android Happy Ending
  • Internet Intro
    • Backend Development
    • Front End Web Development
    • Programming Tools you Need
    • Hosting and Domain Names
  • HTML Virgin
    • HTML Text Tags
    • HTML Links and Images
    • HTML Layout with HTML 5
    • HTML Tables
  • CSS Virgin
    • Where to Stick your CSS
    • CSS Properties, Sizes and Colors
    • CSS Selectors
    • CSS Box Model
  • Programming Virgin
    • Variables in Programming
    • Programming Intro Conditional Statements
    • Programming Intro to Arrays
    • Programming with Loops
    • Programming with Functions
  • PHP Virgin
    • PHP Variables
    • PHP Conditionals
    • PHP Arrays
    • PHP Loops
    • PHP Functions
  • SEO Virgin
    • Make Google Love You
    • Understanding Keyword SEO
    • Optimizing for SEO
    • Technical SEO