0% found this document useful (0 votes)
12 views

PHP Part2 Array Function

Uploaded by

gulelat.atuu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PHP Part2 Array Function

Uploaded by

gulelat.atuu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

 Besides the built-in PHP functions, we can

create our own functions.


 A function is a block of statements that can
be used repeatedly in a program.
 A function will not execute immediately when
a page loads.
 A function will be executed by a call to the
function.
 A user defined function declaration starts
with the word "function":
function functionName() {
code to be executed;}
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
 A function name can start with a letter or
underscore (not a number).
 Function names are NOT case-sensitive.

<?php
function writeMsg() {
echo "Hello world!";
}

writeMsg(); // call the function


?>

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
<?php
function familyName($fname) {
echo "$fname Refsnes.<br>";
}

familyName("Jani");
familyName("Hege");
familyName("Stale"); Output:
Jani Refsnes.
familyName("Kai Jim"); Hege Refsnes.
familyName("Borge"); Stale Refsnes.
?> Kai Jim Refsnes.
Borge Refsnes.

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
<?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);
?>

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 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 key

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 The following example sorts the elements of
the $cars array in ascending alphabetical
order:

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 The following example sorts the elements of
the $numbers array in ascending numerical
order:

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 The following example sorts the elements of
the $cars array in descending alphabetical
order:

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 The following example sorts the elements of
the $numbers array in descending numerical
order:

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
 The following example sorts an associative
array in ascending order, according to the
key:

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
 addslashes()
 stripslashes()
 strip_tags()
 htmlspecialchars()

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 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.

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
removes backslashes added by
the addslashes() function.

<!DOCTYPE html>
<html> Output:
<body> Who’s Peter Griffin?

<?php
echo stripslashes("Who\'s Peter Griffin?");
?>

</body>
</html>
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Strips a string from HTML, XML, and PHP tags.

(Demo)

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 The htmlspecialchars() function converts
some predefined characters to HTML entities.

 The predefined characters are:


 & (ampersand) becomes &amp;
 " (double quote) becomes &quot;
 ' (single quote) becomes &#039;
 < (less than) becomes &lt;
 > (greater than) becomes &gt;

 (Demo)

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 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';

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 Assume we have a standard footer file called
"footer.php", that looks like this:
<?php
echo "<p>Copyright &copy; 1999-" . date("Y")
. " W3Schools.com</p>";
?>
 To include 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>

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
User requests a particular URL

XHTML Page supplied with Form

User fills in form and submits.


Another URL is requested and the
Form data is sent to this page either in
URL or as a separate piece of data.
User
Web Server
XHTML Response
 HTML Forms are used to select different
kinds of user input.
 Set the form action attribute to
◦ <form action=" echo
htmlspecialchars($_SERVER["PHP_SELF"]); "
method="post"> - or
◦ <form action="script.php" method="post">;
 Make sure that you name each form field
that you want to process as these names
will be available to the processing script as
variables
◦ <input type="text" name="inputtext">
◦ The $_SERVER["PHP_SELF"] is a super global
variable that returns the filename of the
currently executing script.
 Superglobal arrays are associative arrays
predefined by PHP that hold variables acquired
from user input, the environment or the web server
and are accessible in any variable scope.

 The arrays $_GET and $_POST retrieve information


sent to the server by HTTP get and post requests,
respectively.

 These are superglobals, which means that they are


always accessible, regardless of scope - and you
can access them from any function, class or file
without having to do anything special.

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
 Using method = "post" appends form data to the
browser request that contains the protocol and the
requested resource’s URL. Scripts located on the
web server’s machine can access the form data
sent as part of the request.

 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.

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 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.

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 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.

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
 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.

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
 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).

©1992-2012 by Pearson Education, Inc. All


Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.

You might also like