Like many others, I wish to use technical PHP as a template language. While PHP’s syntax makes it a practical choice for this, there is a problem with embedding dynamic content. Most PHP applications produce HTML output, so you end up writing <?php echo htmlspecialchars($foo);?> a lot, using this technique. Or you forget it, and make your application horizontal to all sorts of mean XSS attacks.
Apart from the bother of superfluous typing, there is a danger of getting lazy, seeing that <?php echo $foo;?> is remarkably shorter to type. In some situations, it won’t manifest itself as a problem either, since some content-types never contains HTML special characters (Numbers for example). This is particularly nasty, because errors in the view layer are notoriously hard to track down, and unlike SQL-injections — a analogous problem — the consequences tend to hurt the users of a site, rather than the site directly.
PHP Functions
In this lesson we will show you how to create your own functions.For a situation and examples of the built-in functions, please visit our PHP Reference.
Create a PHP Function
A function is a block of code that can be executed whenever we need it.
(1) All functions start with the word “function()”
(2) Name the function – It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
(3) Add a “{” – The function code starts after the opening curly brace
(4) Insert the function code
(5) Add a “}” – The function is finished by a closing curly brace
Example
A simple function that writes my name when it is called:
<html><body>
<?php
function writeMyName()
{
echo “chintan”;
}
writeMyName();
?>
</body></html>
Use a PHP Function
Now we will use the function in a PHP script:
<html><body>
<?php
function writeMyName()
{
echo “chintan”;
}
echo “Hello world!<br />”;
echo “My name is “;
writeMyName();
echo “.<br />That’s right, “;
writeMyName();
echo ” is my name.”;
?>
</body>
</html>
The output of the code above will be:
Hello world!
My name is chintan.
That’s right, chintan is my name.
PHP Functions – Adding parameters
Our first function (writeMyName()) is a very simple function. It only writes a static string.
To add more functionality to a function, we can add parameters. A parameter is just like a variable.
You may have noticed the parentheses after the function name, like: writeMyName(). The parameters are specified inside the parentheses.
Example 1
The following example will write different first names, but the same last name:
<html><body>
<?php
function writeMyName($fname)
{
echo $fname . ” Refsnes.<br />”;
}
echo “My name is “;
writeMyName(“chintan”);
echo “My name is “;
writeMyName(“Den”);
echo “My name is “;
writeMyName(“James”);
?>
</body></html>
The output of the code above will be:
My name is chintan Refsnes.
My name is Den Refsnes.
My name is James Refsnes.
Example 2
The following function has two parameters:
<html><body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . ” Refsnes” . $punctuation . “<br />”;
}
echo “My name is “;
writeMyName(“chintan”,”.”);
echo “My name is “;
writeMyName(“Den”,”!”);
echo “My name is “;
writeMyName(“James”,”…”);
?>
</body>
</html>
The output of the code above will be:
My name is chintan Refsnes.
My name is Den Refsnes!
My name is James Refsnes…
PHP Functions – Return values
Functions can also be used to return values.
Example
<html><body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo “1 + 10 = ” . add(1,10);
?>
</body></html>
The output of the code above will be:
1 + 10= 11
Creating Your First PHP Function
When you make a function, you first need to give it a name, like my Company It’s with this function name that you will be able to call upon your function, so make it easy to type and understand.
The actual syntax for creating a function is pretty self-explanatory, but you can be the judge of that. First, you must tell PHP that you want to create a function. You do this by typing the keyword function followed by your function name and some other stuff (which we’ll talk about later).
Here is how you would make a function called my company . Note: We still have to fill in the code for my Company Motto.
PHP Code:
<?php
function mycompany(){
}
?>
Note: Your function name can start with a letter or underscore “_”, but not a number!
With a properly formatted function in place, we can now fill in the code that we want our function to execute. Do you see the curly braces in the above example “{ }”? These braces define where our function’s code goes. The opening curly brace “{” tells php that the function’s code is starting and a closing curly brace “}” tells PHP that our function is done!
We want our function to print out the company motto each time it’s called, so that sounds like it’s a job for the echo function!
PHP Code:
<?php
function mycompany(){
echo “We deliver quantity, not quality!<br />”;
}
?>
That’s it! You have written your first PHP function from scratch! Notice that the code that appears within a function is just the same as any other PHP code.
Using Your PHP Function
Now that you have completed coding your PHP function, it’s time to put it through a test run. Below is a simple PHP script. Let’s do two things: add the function code to it and use the function twice.
PHP Code:
<?php
echo “Welcome to Phpguru.com <br />”;
echo “Well, thanks for stopping by! <br />”;
echo “and remember… <br />”;
?>
PHP Code with Function:
<?php
function mycompany(){
echo “We deliver quantity, not quality!<br />”;
}
echo “Welcome to phpguru.com <br />”;
mycompany();
echo “Well, thanks for stopping by! <br />”;
echo “and remember… <br />”;
mycompany();
?>
Display:
Welcome to Phpguru.com
We deliver quantity, not quality!
Well, thanks for stopping by!
and remember…
We deliver quantity, not quality!
Although this was a simple example, it’s important to understand that there is a lot going on and there are a lot of areas to make errors. When you are creating a function, follow these simple guidelines:
* Always start your function with the keyword function
* Remember that your function’s code must be between the “{” and the “}”
* When you are using your function, be sure you spell the function name correctly
* Don’t give up!
PHP Functions – Parameters
Another helpful thing about functions is that you can send them information that the function can then use. Our first function mycompany isn’t all that useful because all it does, and ever will do, is print out a single, unchanging string.
However, if we were to use parameters, then we would be able to add some extra functionality! A parameter appears with the parentheses “( )” and looks just like a normal PHP variable. Let’s create a new function that creates a custom greeting based off of a person’s name.
Our parameter will be the person’s name and our function will concatenate this name onto a greeting string. Here’s what the code would look like.
PHP Code with Function:
<?php
function myGreeting($firstName){
echo “Hello there “. $firstName . “!<br />”;
}
?>
When we use our myGreeting function we have to send it a string containing someone’s name, otherwise it will break. When you add parameters, you also add more responsibility to you, the programmer! Let’s call our new function a few times with some common first names.
PHP Code:
<?php
function myGreeting($firstName){
echo “Hello there “. $firstName . “!<br />”;
}
myGreeting(“Dan”);
myGreeting(“Ros”);
myGreeting(“Sam”);
myGreeting(“Madi”);
?>
Display:
Hello there Dan!
Hello there Ros!
Hello there Sam!
Hello there Madi!
It is also possible to have multiple parameters in a function. To separate multiple parameters PHP uses a comma “,”. Let’s modify our function to also include last names.
It is a solution fatal error “Fatal error: Call to undefined function mysql_connect()”
I tried to solve that by following those steps :
1. Made sure “c:\php” is in the PATH string
2. Put “libmySQL.dll” in the “c:\php” directory
3. Set: extension_dir = “c:\php\ext” (which is where “php_mysql.dll” and “php_mysqli.dll” are) in the “Paths and Directories” Section of php.ini
4. Enabled those DLL’s by adding:
extension=php_mysql.dll
extension=php_mysqli.dll
to the Dynamic Extensions section of php.ini
After that I tried to run a simple program but instead of the fatal error I conventional the following notice ” Notice: Object of class mysql result could not be converted to into in C:\PHP\phpFiles\mysql_up.php on line 15″