PHP Workshop #2

PHP Workshop #2

Functions

Functions are like drawers... you put code into it and use it later.

<?php
function SayHello ()
{
echo("Hello this is lilMahsta @ Warez-BB");
}

SayHello();
?>

This code will tell the script there is a function called SayHello which creates a text message with "Hello this is lilMahsta @ Warez-BB". The good thing of functions is, when you need something special very often you dont have to write the whole routine everytime you want to use it. You can use a simple command.

Variables

PHP uses Variables. 'Variables' are like drawers - you can put something into it and get it out later. Variable names always start with a $ and dont have to be declared. Means you can say:

<?php
// Declaration of a Variables
$a = "hey";
$b = 12;
$c = 12.1;
?>

If you code in PHP you will see you ALWAYS need to use Variables.

There are dynamic and global variables. I.E If you put a file on a harddrive of your home computer... you can only use it at home. If you put it on RapidShare you can use it everywhere! Same with Variables... Global Variables can be anywhere in your script and dynamic variables have to be declared everytime you want to use them.

<?php
// Dynamic Variable
$a = "hey";
echo $a;

// THIS WONT WORK:
function SayHello ()
{
echo $a;
}
SayHello();
?>


You can see.... dynamic variables cannot be used like this... they are only in the function where they have been declared.

With a global variable that error wont happen:

<?php
// Global Variable Example

$textmsg = "Hello i am lilMahsta";

function SayHello ()
{
// Attention look at this!!
//
global $textmsg; // AH!! There is a something ... now you can use the variable in this function
echo $textmsg;
}

SayHello(); // Runs the function

?>


Example Script :: Calculator

<?php
// Let's use Global Variables to do this simple addition

$number1 = 2;
$number2 = 4;

function doCalculate ()
{
// Declaring Globals
global $number1;
global $number2;

//Dynamic Variable... cannot be used outside function doCalculate

$result = $number1 + $number2;

echo $number1 . " + " . $number2 . " = " . $result;
}

doCalculate();
?>

Same with Dynamic Variables? OK:

<?php

function doCalculate ()
{
// Dynamic Variables
$number1 = 2;
$number2 = 4;
$result = $number1 + $number2;

echo $number1 . " + " . $number2 . " = " . $result;
}

doCalculate();
?>


Linking Variables with Text


echo $number1 . " + " . $number2 . " = " . $result;

I'll explain! The "." dots are used to link text with variables

You say "Hello" and you got a variable where your name is stored. How to do this? ANSWER: With this strange dot "."

<?php
$name = "Hans";

echo "Hello " . $name . " !!!";
?>

Result: "Hello Hans !!!"

Operating Symbols :

*
+
-
/


In example:

$a + $b;
$a * $b;
$a - $b;
$a / $b;


Homework

o) Create a function named doCalculate which does the calculation 7 - 3 [use dynamic variables] and prints the calculation with the result. Then call the Function with its name.

Hint :

<?php

// YOUR FUNCTION HERE
// ....
// END OF YOUR FUNCTION

doCalculate();

?>

Good Links :

The best PHP site out there...

Check if your scripts work

Last Words :

Thanks for taking time to read my guide. Dont give PHP up, it isnt that hard. Make sure you check PHP.NET too, great site with manual and everything.

Yours sincerely,
lilMahsta

Credits 'n' Copyright

Tutorial Copyright 2006 by lilMahsta (Mr. Mandrake) .

Lesson 1: