Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5

PHP - Part 3

#1
How to Program PHP
Tutorial, Part 3

Welcome to part 3 in this PHP tutorial! So far, we've explored basic concepts such as echo statements, and in part 2 we explored variables and gave a brief introduction to arrays. In this tuturial, we'll introduce yet another very fundamental concept in programming. Luckily, this isn't as complicated as it could be in PHP. This tutorial will introduce the concept of the function.

Chances are, you remember functions from Algebra 1. It's probably a term you are already familiar with, but what do functions actually do?

Well, suppose that in your program, you've written about 200 lines of code to verify a file, then upload it to a server. While you are originally writing this code, it is only needed in one part of the program, but as you continue to develop your software, you find out that this code that you have developed is extremely useful for many tasks in your program. Do you simply copy/paste or rewrite the code? You could do that, but it would turn into a massive mess of thousands of lines of code very quickly.

Furthermore, suppose that you find a massive security hole in your code and need to repair it immediately to protect very important data. If you have your code copied several times all throughout your software, you have to fix the hole several times, making it a difficult fix. What's the solution to the delima? A function, of course! A function allows code to be reused.

In PHP, you first must define a function. When you define a function, you give it a name and put some code inside that will be executed. In PHP, a function will only be executed when it is "called". We'll see how this works through some examples.

<?php

function sendGreeting () {
echo "hello world! <br>";
}

?>

This code is pretty much useless. It's not like we'll be reusing the "hello world" echo statement a whole lot in any real projects, but this code is valid code, and it does define a function. If you run this script on your own, you will notice that nothing happens. Why isn't "hello world" appearing on your screen?

In the code above, we defined the function, but we never "called" it. PHP will never execute a function before it is "called".

<?php

function sendGreeting() {
echo "hello world! <br>";
}

sendGreeting();
?>

The only difference in the code above from our original code is the sendGreeting(); line. This line "calls" the sendGreeting function that we defined before. Run this code in your browser, and you will notice that "hello world!" now appears on your screen.

<?php

function sendGreeting() {
echo "hello world! <br>";
}

sendGreeting();
sendGreeting();
sendGreeting();
sendGreeting();
?>

You probably notice that we are calling the sendGreeting function multiple times in the code above. This is perfectly legal in PHP. The really cool thing about functions is that it basically allows you to reuse code anywhere in your program without having to rewrite it. That's what makes it possible to send you four greetings without four echo statements.

Functions can do a lot more than just send greetings. It turns out that every function can take paremeters, and it can return a value. Now, before you get confused about peremeters and return values...

A paremeter is: just a variable that you send to the function.

A return value is: Just a variable that the function sends back to the script that had called the function.

It turns out that both concepts are incredibly useful. Suppose we want to have two kinds of greetings, but we want to save code and define only one function. Well, since we can pass paremeters to the function, we can pass a number to define which greeting to use.

<?php

function sendGretting($selector) {
if ($selector == 1) {
echo "hello world! <br>";
}
else {
echo "why hello there! This is greeting number 2!<br>";
}

return "success printing greeting!";
}

$status = sendGreeting(1);
echo $status;
?>

First, you may notice something new. I haven't talked about the "if" and the "else" statements before. I won't attempt to explain what these do in detail until later tuturials, but these are basically conditionals that only execute code under certain conditions. Basically, if the $selector variable is equal to 1, it will send the first greeting. If it is not equal to 1, it will echo greeting 2 instead. In this code you will see

Quote:hello world!
success printing greeting!

On your screen when you execute this script.

So... what about the "success printing greeting!" Where did that come from? Is "return" some kind of echo statement?

A return value is just some variable that the function sends back once it is done executing. In this case, we just return "success printing greeting!" When we call the function, notice that we have the $status = in front of the actual function. Basically, whatever the function returns will get stored in the $status variable. Because the function returned "success printing greeting!", $status contains "success printing greeting!" and when we echo $status, you see "success printing greeting!" on your screen.

The function is an incredibly useful concept in programming. In the next tutorials, I will further introduce conditionals, which will be a very useful concept later on.

Next Tutorial: Conditional Statements
Directory of PHP Tutorials
Saturn-Moon.com - Our next project...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP - Part 6 Darth-Apple 2 4,379 November 12th, 2013 at 5:31 PM
Last Post: Damian B.
  PHP - Part 5 Darth-Apple 5 7,801 August 28th, 2013 at 10:10 PM
Last Post: Darth-Apple
  PHP - Part 2 Darth-Apple 2 4,950 August 19th, 2013 at 9:26 PM
Last Post: Darth-Apple
  PHP - Part 1 Darth-Apple 4 7,259 August 19th, 2013 at 7:40 PM
Last Post: Damian B.



Users browsing this thread: 1 Guest(s)

Makestation Theme/Design Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2020
Forum design by Makestation Team © 2020
Saturn-Moon.com - a modern day time capsule | Makestation Ajax Chat Hosting