PHP Functions 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.

Slides:



Advertisements
Similar presentations
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Advertisements

Objectives Connect to MySQL from PHP
PHP and MySQL Web Development tMyn1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in.
PHP Scripts HTML Forms Two-tier Software Architecture PHP Tools.
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
MS3304: Week 4 PHP & HTML Forms. Overview HTML Forms elements refresher Sending data to a script via an HTML form –The post vs. get methods –Name value.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance web Programming Chapter 3: MySQL Date: 28 April 2014 Advance web Programming Chapter 3: MySQL Date: 28 April 2014 Dr. Mogeeb A. A. Mosleh .
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
INTERNET APPLICATION DEVELOPMENT For More visit:
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
LEARN THE QUICK AND EASY WAY! VISUAL QUICKPRO GUIDE Chapter 2: Programming with PHP Copyright © 2012 by Larry Ullman Dr. Mogeeb Mosleh Saturday ( pm)
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
PHP. PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
Radoslav Georgiev Telerik Corporation
PHP – Hypertext Preprocessor.
1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in PHP consists of a number of functions.
A pache M ySQL P hp Robert Mudge Reference:
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
Web Systems & Technologies
PHP Built-In Functions
CGS 3066: Web Programming and Design Spring 2017
Session 2 Basics of PHP.
PHP (Session 1) INFO 257 Supplement.
Introduction to Dynamic Web Programming
CHAPTER 5 SERVER SIDE SCRIPTING
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Database Programming Using PHP
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
PHP Introduction.
PHP FORM HANDLING Post Method
Introduction to Web programming
Ch. 3. PHP (이 강의 내용의 대부분 예들은 w3schools. com/php/default
Introduction to Web programming
PHP.
Web DB Programming: PHP
MCS/BCS.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
PHP PART 2.
PHP Forms and Databases.
PHP an introduction.
Database Access with PHP and MySQL
PHP-II.
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

PHP Functions 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 <?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. ©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.

PHP - Sort Functions For Arrays 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.

Sort Array in Ascending Order - sort() The following example sorts the elements of the $cars array in ascending alphabetical order: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Sort Array in Ascending Order - sort() The following example sorts the elements of the $numbers array in ascending numerical order: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Sort Array in Descending Order - rsort() The following example sorts the elements of the $cars array in descending alphabetical order: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Sort Array in Descending Order - rsort() The following example sorts the elements of the $numbers array in descending numerical order: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Sort Array (Ascending Order), According to Value - asort() ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Sort Array (Ascending Order), According to Key - ksort() The following example sorts an associative array in ascending order, according to the key: ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Sort Array (Descending Order), According to Value - arsort() ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Sort Array (Descending Order), According to Key - krsort() ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Formatting User Input/Output addslashes() stripslashes() strip_tags() htmlspecialchars() ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

addslashes() 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.

stripslashes() removes backslashes added by the addslashes() function. <!DOCTYPE html> <html> <body> <?php echo stripslashes("Who\'s Peter Griffin?"); ?>   </body> </html> Output: Who’s Peter Griffin? ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

strip_tags() Strips a string from HTML, XML, and PHP tags. (Demo) ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

htmlspecialchars() The htmlspecialchars() function converts some predefined characters to HTML entities. The predefined characters are: & (ampersand) becomes & " (double quote) becomes " ' (single quote) becomes ' < (less than) becomes < > (greater than) becomes > (Demo) ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

PHP 5 Include Files 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 Assume we have a standard footer file called "footer.php", that looks like this: <?php echo "<p>Copyright © 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.

Form Processing: How forms work 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

PHP for Forms 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.

19.8 Form Processing and Business Logic 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.

19.8.2 Using PHP to Process HTML5 Forms 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.

Reminder: Using GET 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.

19.4 Form Processing and Business Logic (Cont.) 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.

19.9 Reading from a Database 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.

19.9 Reading from a Database (Cont.) 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.