PHP Functions 
PHP Functions
by Brad Hussey
Video Lecture 29 of 45
Not yet rated
Views: 854
Date Added: May 14, 2017

Lecture Description

Lecture 29: Intro to PHP Functions

If you've made it this far, congratulations are in order! We've covered a lot of ground, and our PHP coding skills are becoming much more refined and skillful!

I'm going to go ahead and say that functions are the Meat n' Potatoes (vegetarian version: Beans n' Rice) of most programming languages; they are fundamental when cooking a tasty PHP dinner!

In PHP, there is a massive library (over 1000) of baked-in functions—pun intended—that do everything from printing text on your screen to adding information to a database, and much more!

It's good to know that there are two types of PHP functions: Built-in PHP Functions, and Custom Functions (you can write your own custom PHP functions!).

Remember echo and print? Those little guys are functions!

Important Facts about Functions


A function is a block of statements that can be used repeatedly in a program.
A function will not execute immediately when a page loads.

A function will be executed by a call to the function.
— w3schools

Let's take a look at the basic syntax of a function:

function functionName() {

// execute code

}

Please note: A function name can start with a letter or underscore (not a number). — w3schools

Hot tip: You can name the function whatever you wish! Just try and have it reflect what the function actually does.

PHP sort() Function

Let's take a quick look at the built-in PHP sort() Function. This function allows us to sort an array in alphabetical order.

First, let's create an array:

// Custom array

$dinner = array("Meat", "Potatoes", "Beans", "Rice");

Now let's run our array through the sort() function:

// Custom array

$dinner = array("Meat", "Potatoes", "Beans", "Rice");

// Add the array as a parameter to sort() function

sort($dinner);

Now all we have to do is echo our array on the screen, using a Foreach loop:

// Custom array

$dinner = array("Meat", "Potatoes", "Beans", "Rice");

// Add the array as a parameter to sort() function

sort($dinner);

// Echo the sorted array

foreach ($dinner as $ingredient) {

echo "$ingredient - ";

}

If coded correctly, the above will echo the array in alphabetical order:

Beans
Meat
Potatoes
Rice

If you're looking to sort your array in reverse order, you can use the PHP rsort()function. Feel free to read more about the sort function (ca3.php.net/sort), and other sorting functions here (www.php.net/manual/en/function.rsort.php).

We won't be covering all of the built-in PHP functions in this course, because that would take an incredibly long time. Besides, it's fun to be in a situation where you're programming and then you think "Hey! I wonder if there's a PHP function that will do this for me?" — this is why I'll leave you with the curiosity to experiment. Google (bit.ly/17DDE7C) is your best friend, and you may also refer to this directory (www.tutorialspoint.com/php/php_function_reference.htm).

DOWNLOAD COURSE FILES HERE
www.bradhussey.ca/download-php-course

Course Index

Course Description

This course is a Total Beginner's Guide to Coding Your Very Own Dynamic Websites with PHP, so you need no prior knowledge or experience — although it's a good idea that you know some HTML (my beginner's guide "Build a Website from Scratch with HTML & CSS" will get teach you everything you need to know).

So — why learn PHP? Well, PHP is a very powerful scripting language used by millions of websites. Some of the most popular websites and frameworks utilize PHP to build their dynamic websites. PHP works very well with HTML, and therefore will allow you to start coding dynamic websites quickly without having to learn some of the more complicated scripting languages out there.

Comments

There are no comments. Be the first to post one.
  Post comment as a guest user.
Click to login or register:
Your name:
Your email:
(will not appear)
Your comment:
(max. 1000 characters)
Are you human? (Sorry)