Chapter 5 PHP Functions
Chapter 5 PHP Functions
<?php
function writeMsg()
{ echo "Hello
world!";
}
writeMsg(); // call
the function
?>
<?php
function familyName($fname)
{ echo "$fname
Refsnes.<br>";
}
•familyName("Jani"); familyName("Hege");
familyName("Stale"); familyName("Kai Jim");
familyName("Borge");
•?> Output:
Jani Refsnes.
Hege Refsnes.
Stale Refsnes.
Kai Jim
Refsnes. Borge
Refsnes.
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight
<br>";
}
setHeight(350);
setHeight(); // will use the default
value of 50
setHeight(135);
setHeight(80);
?>
🞂 sort() - sort arrays in ascending order
🞂 rsort() - sort arrays in descending
order
🞂asort() - sort associative arrays in
ascending order, according to the value
🞂ksort() - sort associative arrays in
ascending order, according to the key
🞂arsort() - sort associative arrays in
descending order, according to the
value
🞂krsort() - sort associative arrays in
descending order, according to the
🞂The
following example sorts the elements
of the $cars array in ascending
alphabetical order:
🞂The
following example sorts the elements
of the $numbers array in ascending
numerical order:
🞂The
following example sorts the elements
of the $cars array in descending
alphabetical order:
🞂 The following example sorts the elements
of the $numbers array in descending
numerical order:
🞂The
following example sorts an
associative array in ascending order,
according to the key:
🞂 addslashes()
🞂 stripslashes()
🞂 strip_tags()
🞂
htmlspecialchars()
🞂returns a string with backslashes in front of
predefined characters (Demo).
🞂 The predefined characters are:
single quote (')
double quote (")
backslash (\)
NULL
🞂Tip: This function can be used to prepare a
string for storage in a database and
database queries.
removes backslashes added by the
addslashes() function.
<!DOCTYPE
html> Output:
<html> Who’s Peter
<body> Griffin?
<?php
echo stripslashes("Who\'s Peter
Griffin?");
?>
</body>
</html>
Strips a string from HTML, XML, and PHP
tags.
(Demo
)
🞂Thehtmlspecialchars() function converts
some predefined characters to HTML
entities.
(Demo)
🞂The include statement takes all the
text/code/markup that exists in the
specified file and copies it into the file that
uses the include statement.
🞂Including files is very useful when you
want to include the same PHP, HTML, or
text on multiple pages of a website.
🞂It is possible to insert the content of one PHP
file into another PHP file (before the
server executes it), with the include
statement.
🞂 Syntax: include 'filename';
🞂Assume we have a standard footer file
called "footer.php", that looks like this:
<?php
echo "<p>Copyright © 1999-" .
date("Y")
. " W3Schools.com</p>";
?>
🞂Toinclude the footer file in a page, use the
include statement (Demo, Demo2):
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body></html>
User requests a particular URL
🞂Information
sent from a form with the POST
method is invisible to others (all names/values
are embedded within the body of the HTTP
request) and has no limits on the amount of
information to send.
🞂Information sent from a form with the GET
method is visible to everyone (all
variable names and values are displayed
in the URL). GET also has limits on the
amount of information to send. The
limitation is about 2000 characters.
🞂GET may be used for sending non-
sensitive data.
🞂We escape the normal meaning of a character in
a string by preceding it with the backslash
character (\).
🞂Function die terminates script execution. The
function’s optional argument is a string, which
is printed as the script exits.
🞂Function
mysql_connect connects to the MySQL
database. It takes three arguments—
the server’s hostname
a username
a password
and returns a database handle—a representation
of PHP’s connection to the database, or false if
the connection fails.
🞂 Function mysql_select_db selects and opens
the
database to be queried.
🞂The function returns true on success or false on
failure.
🞂To query the database, we call function mysql_query,
specifying the query string and the database to
query.
🞂This returns a resource containing the result of the query,
or false if the query fails.
🞂 It can also execute SQL statements such as INSERT or
DELETE
that do not return results.
🞂The mysql_error function returns any error strings from the
database.
🞂 mysql_close closes the connection to the database
specified
in its argument.
🞂The mysql_fetch_row function returns an array containing
the values for each column in the current row of the
query result ($result).
THANKS!