Sie sind auf Seite 1von 6

In PHP a funciton is basically a hunk of code that you can call on at anytime to execute.

You have already used alot of the built-in functions such as echo(). The great thing about a function is that once you write one, you can call it from anywhere so long as the function is declared before you try to use it. Functions can save you from doing a lot of repetative code, plus they are easy to edit or change afterwards. So lets get started with functions. Basic Function - Without Arguments: PHP Code Example: <? // This first line declares our function. function functionname() { Your php code will go here } // To call the function use this functionname(); ?> Basic Function - With Arguments: PHP Code Example: <? // This first line declares our function and any arguaments it may require. function functionname(argument1, argument2) { Your php code will go here } // To call the function use this functionname(argument1, argument2); ?> Example Function: PHP Code Example: <? // This first line declares our function and any arguaments it may require. function add($number1, $number2) { // Add our two arguments together and assign the result to $result $result = $number1 + $number2; // Print the value of $result to the screen echo $result; } // Here we call our function with our two arguments 2 and 7 which should print 9 to the screen add(2,7); ?> RULES Rule 1: Make sure your new function name does not exhist. You can check PHP.net for a complete list of PHP predefined functions. Rule 2: Do not declare/define the same function more than once inside the same file. You will get an error.

function squareroot($num) { if ($num < 0) { die('error, cannot use negative number in squareroot()'); } else { return sqrt($num); } } <?php function AddAll($number1,$number2,$number3) { $plus = $number1 + $number2 + $number3; return $plus; } echo "123 + 654 + 9 equals " . AddAll(123,654,9); ?> functions in Include Files: Create file : emptyfile.inc <?php function add($number1, $number2) { $result = $number1 + $number2; echo "--->$result"; return $result; } ?> And a php file with the statement include: <? include("emptyfile.inc"); $i=add(2,7); echo "<br>$i"; ?> Call the php file in the browser, what do you see? Exercise: 1) For this PHP exercise, create a function called "hello" that outputs that phrase we all know and love, "Hello, World!" to the browser. Call the function. 2) This PHP exercise has two parts. For the first, you will create a function to accept two arguments, perform a calculation using them, then return a sentence with the result to the browser. The function will calculate the area of a rectangle, with the two arguments being width and height. (Reminder: area = width * height.) The sentence to be returned is "A rectangle of length $l and width $w has an area of $area.", where $l and $w are the arguments and $area is the result. For part two, rewrite your script to produce a return value, the area of the rectangle with length and width as supplied in the arguments. Move the echo statement out of the function, and rewrite. Call

the function inside the output to supply the area value. (The output to the browser should be identical to the output for Part 1.)

You have an html file : <html> <head></head> <body> <form action="myform5.php" method="post"> <p>First name: <input type="text" name="firstname" /></p> <p>Last name: <input type="text" name="lastname" /></p> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> and the php file (myform5.php) <?php echo("First name: " . $_POST['firstname'] . "<br />\n"); echo("Last name: " . $_POST['lastname'] . "<br />\n"); ?> If you call the html file, fill the text boxes and submit the form what will you see? Can you combine both files in one php file? <?php if(isset($_POST['submit'])) { echo("First name: " . $_POST['firstname'] . "<br />\n"); echo("Last name: " . $_POST['lastname'] . "<br />\n"); } ?> <form action="myform5.php" method="post"> <p>First name: <input type="text" name="firstname" /></p> <p>Last name: <input type="text" name="lastname" /></p> <input type="submit" name="submit" value="Submit" /> </form> Exercise:
For this PHP exercise, you will rewrite the rectangle area function from exercise 2 once again, this time to accept user input. Present a form to the user with the message "Please enter the values of the length and width of your rectangle." Below this, supply two text boxes, one for length and one for width. Using your function to process the user supplied values, return the result statement from the previous exercise to the user. Reminder: the statement was "A rectangle of length $l and width $w has an area of $area.", where $l and $w are the arguments and $area is the result. You must use only one file for both the form and the php statements!

Another example with selection box in html : <html> <head></head> <body> <form action="myform5.php" method="post">

<select name="goods"> <option value=1></option> <option value=2></option> <option value=3></option> </select> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> and the php <?php if ($_POST['goods'] =="1") echo ""; else if ($_POST['goods'] =="2") echo ""; else echo ""; ?> Do you understand it? : 1 ) HTML , . PHP : , . ( ) 2) 5 5 3) . 4) PHP , . PHP : 1. . 2. .

3. .

Das könnte Ihnen auch gefallen