1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài giảng Phát triển ứng dụng nguồn mở: Bài 2.2 - Đoàn Thiện Ngân

98 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Bài 2.2 - Fundamentals of PHP. Các nội dung chính trong bài này gồm: Tổng quan về PHP, công cụ lập trình với PHP, ngôn ngữ lập trình PHP: Khái niệm cơ bản, hàm xuất dữ liệu, mảng, hàm. Mời các bạn cùng tham khảo.

Bài 2.2: Fundamentals of PHP GV: ĐỒN THIỆN NGÂN Đồn Thiện Ngân Bài 2.2 - 1/98 Nội dung • Tổng quan PHP • Cơng cụ lập trình với PHP • Ngơn ngữ lập trình PHP – Khái niệm – Hàm xuất liệu – Mãng – Hàm Đoàn Thiện Ngân Bài 2.2 - 2/98 Tài liệu tham khảo Bắt buộc: PHP Manual http://www.php.net/docs.php Beginning PHP and MySQL: From Novice to Professional, 4th Edition, W Jason Gilmore, 2010 PHP for the Web; 4th Edition, Larry Ullman; Peachpit Press; 2011 Đoàn Thiện Ngân Bài 2.2 - 3/98 General Concepts • PHP (Hypertext Preprocessor): ngơn ngữ scripting mã nguồn mở nhiều người sử dụng đặc biệt phù hợp với ứng dụng web dễ dàng nhúng vào HTML • PHP code sử dụng lệnh nằm cặp tag • PHP khác hẵn với client-side JavaScript: – Mã lệnh PHP thực server, – Khi PHP script thực xong, mã HTML phát sinh gởi client – PHP ngơn ngữ server−side scripting Đồn Thiện Ngân Bài 2.2 - 4/98 PHP scripts PHP scripts dùng lãnh vực: • Server-side scripting: lãnh vực phổ biến PHP (CGI or server module) • Command line scripting: PHP script chạy shell script khơng cần server hay browser Lý tưởng cho scripts dùng cron (on *nix or Linux) hay Task Scheduler (on Windows) ─ simple text processing tasks • Writing desktop applications: PHP khơng phải ngôn ngữ tốt tạo ứng dụng GUI desktop, dùng PHP-GTK để viết chương trình Đoàn Thiện Ngân Bài 2.2 - 5/98 PHP Characteristics • PHP dùng hầu hết OS: Linux, Unix (HP-UX, Solaris, OpenBSD, …), MS Windows, Mac OS X, RISC OS,… • PHP hỗ trợ hầu hết web servers: IIS, Apache, … PHP hoạt động module, hay CGI processor • Khơng có output HTML, PHPcó khả xuất liệu dạng images, PDF Flash movies (libswf & Ming), XHTML, XML, … • PHP tạo tập tin lưu trữ hệ thống tập tin PHP tạo liệu nhớ đệm server Đoàn Thiện Ngân Bài 2.2 - 6/98 PHP Characteristics • Một tính mạnh ấn tượng hỗ trợ hầu hết DBMS: MySQL, PostGreSQL, Sybase, MS SQL Server, … • Trong PHP, viết trang web truy cập DBMS dùng specific extensions (mysql, mysqli, pgsql, … ), hay dùng abstraction layer PDO (PHP Data Object), hay giao tiếp với DBMS hỗ trợ Open Database Connection chuẩn (ODBC extension) DBMS khác dùng sockets (CouchDB) Đồn Thiện Ngân Bài 2.2 - 7/98 PHP Download • • • • http://www.php.net/ http://www.php.net/downloads.php http://www.php.net/docs.php Môi trường cài đặt: – MS Windows – Linux – Unix – Mac OS –… Đoàn Thiện Ngân Bài 2.2 - 8/98 PHP – Apache – MS Windows • Trước hết cần web server: Apache, IIS, … • MS Windows: Cài phần riêng lẻ (đọc PHP manual) a) httpd b) php, cấu hình php với Apache, thử chạy trang php Cài gói cấu hình sẵn (đơn giản) – Wamp, AppServ, xampp, … (Windows, Apache, MySQL, PHP) http://www.wampserver.com/en/ http://www.appservnetwork.com/ http://www.apachefriends.org/en/xampp.html Đoàn Thiện Ngân Bài 2.2 - 9/98 PHP – Apache – Linux • Gói LAMP • Cài riêng lẻ – Cài httpd (Apache server) # yum install httpd httpd–manual # service httpd start # chkconfig ––levels 235 httpd on (thử Firefox –– localhost) – Cài php # yum install php php–mysqlnd php–pgsql php–pdo # service httpd restart – Thử nghiệm php Đoàn Thiện Ngân Bài 2.2 - 10/98 Splicing an Array • array_splice() function removes all elements of an array found within a specified range, returning those removed elements in the form of an array • array array_splice(array array, int offset [, int length [, array replacement]]) $states = array(”10", ”20", ”30", ”40", “50"); $subset = array_splice($states, 3, 1); print_r($states); print_r($subset); Đoàn Thiện Ngân Bài 2.2 - 84/98 Returning a Random Set of Keys • array_rand() function will return a random number of keys found in an array If we omit the optional num_entries parameter, only one random value will be returned We can tweak the number of returned random values by setting num_entries accordingly mixed array_rand(array array [, int num_entries]) $states = array(”10", ”20", ”30", ”40", “50"); $subset = array_rand($states); print_r($states); print_r($subset); Đoàn Thiện Ngân Bài 2.2 - 85/98 Shuffling Array Elements • shuffle() function randomly reorders an array void shuffle(array input_array) $cards = range(1,10); print_r($cards); print “”; shuffle($cards); print_r($cards); Đoàn Thiện Ngân Bài 2.2 - 86/98 Functions • The concept of embodying the repetitive processes within a named section of code and then invoking this name when necessary has long been a key feature of modern computer languages Such a section of code is known as a function • PHP functions, including how to create and invoke them, pass input to them, use a relatively new feature known as type hinting, return both single and multiple values to the caller, and create and include function libraries • Additionally, we’ll learn about both recursive and variable functions Đoàn Thiện Ngân Bài 2.2 - 87/98 Creating a Function • Define a function function functionName(parameters) { function-body } • Call the function functionName(parameters); Đồn Thiện Ngân Bài 2.2 - 88/98 Passing Arguments by Value function calcSalesTax($price, $tax) { $total = $price + ($price * $tax); echo "Total cost: $total"; } • calcSalesTax(15.00, 075); $pricetag = 15.00; $salestax = 075; calcSalesTax($pricetag, $salestax); Đoàn Thiện Ngân Bài 2.2 - 89/98 Passing Arguments by Reference • On occasion, you may want any changes made to an argument within a function to be reflected outside of the function’s scope Passing the argument by reference accomplishes this • Passing an argument by reference is done by appending an ampersand to the front of the argument function calculateCost(&$cost, $tax){…} • Note the value of $tax remains the same, although $cost has changed Đoàn Thiện Ngân Bài 2.2 - 90/98 Default Argument Values • Default values can be assigned to input arguments, which will be automatically assigned to the argument if no other value is provided To revise the sales tax example, suppose that the majority of your sales take place in Ohio You could then assign $tax the default value of 6.75 percent, function calcSalesTax($price, $tax=.0675) { $total = $price + ($price * $tax); echo "Total cost: $total"; } Đoàn Thiện Ngân Bài 2.2 - 91/98 Using Type Hinting • PHP introduced a new feature known as type hinting, which gives you the ability to force parameters to be objects of a certain class or to be arrays • Unfortunately, type hinting using scalar data types such as integers and strings is not supported • If the provided parameter is not of the desired type, a fatal error will occur As an ex., suppose you created a class named Customer and wanted to be certain that any parameter passed to a function named psPayPalPayment() was of type Customer function psPayPalPayment(Customer $customer) {) Đoàn Thiện Ngân Bài 2.2 - 92/98 Returning Values from a Function • return() statement returns any ensuing value back to the function caller, returning program control back to the caller’s scope in the process If return() is called from within the global scope, the script execution is terminated function calcSalesTax($price, $tax=.0675) { return $price + ($price * $tax); } … $total = calcSalesTax($price); Đoàn Thiện Ngân Bài 2.2 - 93/98 Returning Multiple Values • The list() construct offers a convenient means for retrieving values from an array, function retrieveUserProfile() { $user[] = "Jason Gilmore"; $user[] = "jason@example.com"; $user[] = "English"; return $user; } list($name, $email, $language) = retrieveUserProfile(); echo "Name: $name, email: $email, language: $language"; Đoàn Thiện Ngân Bài 2.2 - 94/98 Recursive Functions • Recursive functions, or functions that call themselves, offer considerable practical value to the programmer and are used to divide an otherwise complex problem into a simple case, reiterating that case until the problem is resolved • Practically every introductory recursion example involves factorial computation Đoàn Thiện Ngân Bài 2.2 - 95/98 Function Libraries • PHP libraries are created via the simple aggregation of function definitions in a single file • We can insert this file into scripts using include(), include_once(), require(), or require_once(), Đồn Thiện Ngân Bài 2.2 - 96/98 ??? • PHP Identifier, Variable, Data Types • Array – Create – Array Functions • Function – Create – Passing Arguments by Value – Passing Arguments by Reference – Default Argument Values – Function libraries Đoàn Thiện Ngân Bài 2.2 - 97/98 ??? • Handling File Uploads – Uploading Files via HTTP – Uploading Files with PHP – Taking Advantage of PEAR: HTTP_Upload Đoàn Thiện Ngân Bài 2.2 - 98/98 ... Peachpit Press; 2011 Đoàn Thiện Ngân Bài 2.2 - 3/98 General Concepts • PHP (Hypertext Preprocessor): ngôn ngữ scripting mã nguồn mở nhiều người sử dụng đặc biệt phù hợp với ứng dụng web dễ dàng nhúng... applications: PHP ngôn ngữ tốt tạo ứng dụng GUI desktop, dùng PHP-GTK để viết chương trình Đồn Thiện Ngân Bài 2.2 - 5/98 PHP Characteristics • PHP dùng hầu hết OS: Linux, Unix (HP-UX, Solaris, OpenBSD, …),... Bluefish, PsPAD, … Đồn Thiện Ngân Bài 2.2 - 12/98 PHP Basics • Embedding PHP Code in Your Web Pages • Commenting Your Code – Single-Line C++ Syntax: – Shell Syntax: – Multiple-Line C Syntax:

Ngày đăng: 09/05/2021, 18:28

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN