|
|
|
Using Functions and Including Files in PHP |
|
Functions
The real power of PHP comes from its functions. In PHP - there are more than 700 built-in functions available. In this article we will show you how to create your own functions.
As your code gets longer, being able to create your own functions is an essential step because it breaks up that code into manageable sections. As you know, functions are sets of statements that can be called by name, which means that they're ideal for packaging your code into manageable chunks. When your scripts get longer than two-dozen statements or so, you should consider breaking up things into functions.
Creating functions also facilitates script development, if you've got a problem in a script that is 2,000 lines long, imagine how hard it will be to find that error. But if you've broken up your code into functions, each of which is only two-dozen lines or so, it'll be much easier and when you know that a given function works as it should, you don't have to worry about it anymore.
Breaking up scripts as they get longer is also a good idea in terms of scope. Scope is the visibility of an item, such as a variable, in your script for example, if you have a 2,000 line script, and you use a variable named $counter at the beginning, but then you forget that you've done so and use another variable with the same name near the end of the script, they're actually the same variable. Thus, you have an unintentional conflict because when you use $counter in one place, it'll set its value everywhere.
Restricting code to functions helps solve this unintentional problem, the variables you use in a function can be restricted to the scope of that function. Thus, you don't have to worry about those variables overlapping other variables with the same name by mistake, they cannot escape the scope of the function.
How do you create your own function? Here's how to do it formally:
function function_name([argument_list...]) { [statements;] [return return_value;] }
|
|
Last Updated ( Thursday, 06 July 2006 )
|
|
Read more...
|
|
|
If until now you have used simple variables to store data, then there’s more to the story. Arrays can hold multiple data items, assigning each one a numeric or text index (also called a key). For example, if you want to store some student test scores, you can store them in an array, and then you can access each score in the array via a numeric index. That's great as far as computers are concerned because you can work through all the elements in an array simply by steadily incrementing that index, as you might do with a loop. In that way, you can use your computer to iterate over all the elements in an array in order to print them out or find their average value, for example. |
|
Last Updated ( Thursday, 06 July 2006 )
|
|
Read more...
|
|
|
Forms have always been one of the quickest and easiest ways to add interactivity to your web site. A form enables you to ask customers if they like your products and casual visitors for comments. PHP can simplify the task of processing webbased forms substantially, by providing a simple mechanism to read user data submitted through a form into PHP variables. Consider the following sample form:
<html>
<head></head>
<body>
<form action="message.php" method="post">
Enter your message: <input type="text" name="msg" size="30">
<input type="submit" value="Send">
</form>
</body>
</html> The most critical line in this entire page is the <form> tag:
<form method="post" action="message.php"> ... </form>
As you probably already know, the method attribute of the <form> tag specifies the manner in which form data will be submitted (POST), while the action attribute specifies the name of the server-side script (message.php) that will process the information entered into the form. Here is what message.php looks like:
<?php
// retrieve form data in a variable
$input = $_POST['msg'];
// print it
echo "You said: <i>$input</i>";
?> |
|
Last Updated ( Thursday, 06 July 2006 )
|
|
Read more...
|
|
PHP comes packed with support for databases, which is good because developers often want to manage and store their data on a server.
PHP has included support for MySQL since version 3.x, although the procedure to activate this support has varied widely between versions. PHP 4.x included a set of MySQL client libraries, which were activated by default. PHP 5.x no longer bundles these libraries, however, due to licensing issues, so you need to obtain and install them separately. Then, you need to explicitly activate the MySQL extension—ext/mysql—by adding the --with-mysql option to PHP’s configure script.
The MySQL API built into PHP is designed to accomplish four primary goals:
• Manage database connections • Execute queries • Process query results • Provide debugging and diagnostic information
|
|
Last Updated ( Thursday, 06 July 2006 )
|
|
Read more...
|
|
| << Start < Prev 1 2 Next > End >>
| | Results 16 - 19 of 19 |
|

|