PHP Tutorial: The function of functions

PHP Tutorial: The function of functions

Functions in PHP, are wonderful tools. They allow you to go with the DRY (don’t repeat yourself) style of writing code.
Functions are good for when you want to run some code often, perhaps a small block. We’ll make a function called “tax()”, you create a function like this:

<?php
/* our function will have two parameters:
$amount which will be put in by you when you call the function, and
$rate which, by default, will be 0.05, but you can change it anytime */
function tax( $amount, $rate = 0.05 ) {
/* now we need to find out how much the tax will be
 $amount is set when the function is called */
$tax = $rate*$amount;
/* now return the value, for use in your script */
return $tax;
} 
/* now you simply call the function: 
the following code will echo:  5 */
echo tax(100);
 
/* adding a new tax rate 
the following code will echo 8 */
echo tax(100,0.08);
?>

Variables outside a function cannot be used inside a function except for three cases: (to my knowledge)

  • The variable is a super global, such as $_POST, $_GET, $_SESSION, $_ENV etc…
  • The variable is not a variable, but a constant, defined earlier in the execution.
  • The variable is defined in the function using the global ; option.

Using a variable inside a function by definition of global ;

<?php
$fruit = 'Apple';
function fruit() {
    global $fruit;
    echo $fruit;
} 
fruit();
/* However */
function rotten() {
    echo "Rotten $fruit";
} 
/* This will only produce the word Rotten, not Rotten Apple. 
in fact you will get an error! something like this: 
Notice: Undefined variable: fruit in yourfilename.php on line 16 */
rotten();
?>

So that’s the basics of functions, I hope that helps!

By Mike on March 25, 2011 | PHP, Tutorials
Tags: , , ,

PHP Tutorial: Feeling loppy for() a while()

In PHP there will often be times when you want to repeat a certain amount of code until a goal is reached. You can accomplish this a number of ways, but I’m only going to cover two of them in this tutorial. The first way will be the while() loop, which will continue to execute the code within it, so long as the expression is returning true. The most common way to exit a while loop is to make the expression return false. Other ways include using ‘break;’ and ‘return;’, but we’ll just cover making the expression return false for now. Let’s get started:

<?php
/* first we're going to set a counter variable
something to keep track of how long our while loop as been running 
we'll call it $i and set it to a value of 0 */
$i = 0;
 
/* now we'll construct our while() loop */
while ( $i < 10 ) {
    echo "Current: $i<br />"; // echo out the current value of $i
    $i++; // add 1 to the value of $i
}
?>

The above block will output something like this:
Current: 0
Current: 1
Current: 2
… …
Current: 8
Current: 9

You may be wondering why the first echo is 0 and the last is 9, well that’s easy. We set our variable to 0 (so that’s the first output), then after the first loop, we added 1 to the value of $i, and 1 each loop after that, until $i equaled 10, and since 10 is not less than ( < ) 10, the loop ends at 9. Simple, right? Right. Now, we'll move on to the for() loop, the for() loop takes 3 expressions, all of which can be left blank, but we won't do that this time. Lets try a simple for() loop to get the same result as our previous while() loop.

<?php
/* unlike the while() loop we don't need to set a counter variable
outside, in order to end it.  We can do that right in the first expression */
 
for ( $i = 0 ; $i < 10 ; $i++ ) {
    echo "Current: $i<br />"; // simply echo the current value of $i
}
?>

So, the first expression is evaluated (executed) once before the loop begins.
The second expression works similarly to the while() loop’s expression, so long as it is true the code in the for() loop block is executed.
The third expression is evaluated (execute) after each iteration (loop) of the for() loop.

By Mike on March 24, 2011 | PHP, Tutorials
Tags: , , , ,

PHP Tutorial: No IF’s, ELSEIF’s, or ELSE’s about it!

Once you’re comfortable with strings and variables, it’s time to really put them into action. You can accomplish this byusing PHP’s if/elseif/else structure, don’t worry I’ll explain. The if() structure evaluates an expression, that expression will return either TRUE or FALSE. Let’s do an example:

<?php
/* we'll set a variable, and use it in an if() statement */
$mynumber = 10;
 
/* now we build the if() {} else {} statement */
if ( $mynumber > 5 ) {
    echo 'The variable $mynumber is greater than 5!';
} else { /*otherwise, it must not be greater than 5 */
    echo 'The variable $mynumber is not greater than 5.';
}
?>

So let’s talk about what just happened. We told PHP to check if the variable $mynumber was greater than. In our case, $mynumber was greater than so that example should display: The variable $mynumber is greater than 5! After that, if the expression ($mynumber > 5) returns false the else code block will come into play, and display: The variable $mynumber is not greater than 5.

“Mike you mentioned elseif, what’s that?” Well the elseif() goes between the if() {} and the else {}, like another level of expression checking. Let’s see a new example:

<?php
/* so first, we'll set a variable
this time we'll use a string */
$apple = 'red';
 
/* now we'll build our if/elseif/else structure 
We're going to use the == (equal to) operator */
if ( $apple == 'green' ) {
    echo 'The apple is green!';
} elseif ( $apple == 'yellow' ) {
    echo 'The apple is yellow!';
} else {
    echo "The apple is not green or yellow, but $apple.";
}
?>

Note: When setting a variable or echoing a string, or ending any statement in PHP be sure to end your statement with a semi-colon ( ; ).

So to go over what just happened: We set a variable named $apple and set it to have a value of red. We then asked PHP to check if that variable had a value of green, and if not check if it has a value of yellow, and once again, if not, execute the code inside the else {} code block, which will echo out what color our $apple actually is.

Hopefully that helps you understand the structure behind an if/elseif/else structure.

Thanks for reading, I hope you found this helpful!

By Mike on March 23, 2011 | PHP, Tutorials
Tags: , ,