Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Tutorial - Introduction to Functions

php



+ Font mai mare | - Font mai mic



Tutorial - Introduction to Functions  

In later tutorials, we'll be using functions a lot, so here's a basic introduction.



What are functions? They are sections of scripts that allow you to do the same thing over and over again very easily.

For example, if we print out the word 'hello' all through a script, we could use a function to make it easier, and if

we wanted to change the word to 'hi', we would only need to change it in one place.

Here's the code to do it.

function a()

Let's look at it in a bit more detail.

The first line tells us we are creating a function called 'a'. You can call a function whatever you wish, as long as

it's not a native PHP function (so you couldn't make a function called 'stripslashes'). The left brace ('') is the function.

Inside the function, we can do anything we like, from printing messages, to calculating things, to extracting information from a

database

Once we've created the function, how do we use it?

a(

as simple as that.

Here's the whole script.

<?php

function a()

a(

?>

If you ran this through your browser, it would print out 'hello'.

We could include this function in another file, so it can be used anywhere.

Now, this isn't a really useful example. Here's one that might be :

function js_alert ($message='hello')

This function is a bit different to the last one.

Inside the brackets, we have a variable, called a parameter. This allows us to pass information to the function (we can have more

than parameter, we can have as many as we like).

The string after the parameter name is a default value, so when we use the function if we don't give it a value, it has one to

use already. The rest is basic javascript.

How do we use this one? Here's the whole script.

<?php

function js_alert ($message='hello')

js_alert(); # this pops up 'hello'.

js_alert('another example'); # this pops up 'another example'.

?>

Functions can also return values. Here's a simple example:

function add_numbers($a,$b)

This simply adds two numbers together, and returns the result. Pretty easy, here's how we use it:

<?php

function add_numbers($a,$b)

$add = add_numbers(1,2);

echo $add;

?>

The last thing we need to look at for functions is global variables. If you define a variable outside a function, it's not

available inside the function, unless we declare it as a global.

What's that mean? If we have the PHP script below:

<?php

$a = 'hello<br>';

function b()

b(

?>

This will display

'a = '

If we want the variable accessible inside the function, we need the line:

global $a;

If we now have the script like this:

<?php

$a = 'hello<br>';

function b()

b(

?>

This will display

'a = hello'

That wraps up functions. They can do anything you like, and make it easier to do the same thing over and over again where-ever

you need to.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 1103
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved