Document Details

Smt. Kashibai Navale College of Commerce, Pune - 04. MES Abasaheb Garware College

Prof. Gajanan A. Deshmukh and Mrs. Swati S. Jadhav

Tags

PHP programming programming language web development computer science

Summary

This is a PHP textbook for students in the S.Y. B.B.A. (C.A.) program at Smt. Kashibai Navale College of Commerce, Pune -04. It covers various aspects of PHP programming.

Full Transcript

A Book Of PH P For BBA (Computer Applications): Semester - III [Course Code 304 : Credit - 3] CBCS Pattern As Per New Syllabus, Effective from June 2020 Prof. Gajanan A. Deshmukh...

A Book Of PH P For BBA (Computer Applications): Semester - III [Course Code 304 : Credit - 3] CBCS Pattern As Per New Syllabus, Effective from June 2020 Prof. Gajanan A. Deshmukh B.Sc. (Comp. Sci), MCA HOD - BCA DEPT. Smt. Kashibai Navale College of Commerce, Pune -04. Mrs. Swati S. Jadhav M.C.S, NET, M.Phil, Assistant Professor, Department of Computer Science, MES Abasaheb Garware College Price ` 240 N4951 PHP ISBN 978-93-90225-83-5 First Edition : August 2020 © : Authors The text of this publication, or any part thereof, should not be reproduced or transmitted in any form or stored in any computer storage system or device for distribution including photocopy, recording, taping or information retrieval system or reproduced on any disc, tape, perforated media or other information storage device etc., without the written permission of Authors with whom the rights are reserved. Breach of this condition is liable for legal action. Every effort has been made to avoid errors or omissions in this publication. In spite of this, errors may have crept in. Any mistake, error or discrepancy so noted and shall be brought to our notice shall be taken care of in the next edition. It is notified that neither the publisher nor the authors or seller shall be responsible for any damage or loss of action to any one, of any kind, in any manner, therefrom. Published By : Polyplate Printed By : NIRALI PRAKASHAN YOGIRAJ PRINTERS AND BINDERS Abhyudaya Pragati, 1312, Shivaji Nagar, Survey No. 10/1A, Ghule Industrial Estate Off J.M. Road, Pune – 411005 Nanded Gaon Road Tel - (020) 25512336/37/39, Fax - (020) 25511379 Nanded, Pune - 411041 Email : [email protected] Mobile No. 9404233041/9850046517 DISTRIBUTION CENTRES PUNE Nirali Prakashan : 119, Budhwar Peth, Jogeshwari Mandir Lane, Pune 411002, Maharashtra (For orders within Pune) Tel : (020) 2445 2044; Mobile : 9657703145 Email : [email protected] Nirali Prakashan : S. No. 28/27, Dhayari, Near Asian College Pune 411041 (For orders outside Pune) Tel : (020) 24690204; Mobile : 9657703143 Email : [email protected] MUMBAI Nirali Prakashan : 385, S.V.P. Road, Rasdhara Co-op. Hsg. Society Ltd., Girgaum, Mumbai 400004, Maharashtra; Mobile : 9320129587 Tel : (022) 2385 6339 / 2386 9976, Fax : (022) 2386 9976 Email : [email protected] DISTRIBUTION BRANCHES JALGAON Nirali Prakashan : 34, V. V. Golani Market, Navi Peth, Jalgaon 425001, Maharashtra, Tel : (0257) 222 0395, Mob : 94234 91860; Email : [email protected] KOLHAPUR Nirali Prakashan : New Mahadvar Road, Kedar Plaza, 1st Floor Opp. IDBI Bank, Kolhapur 416 012 Maharashtra. Mob : 9850046155; Email : [email protected] NAGPUR Nirali Prakashan : Above Maratha Mandir, Shop No. 3, First Floor, Rani Jhanshi Square, Sitabuldi, Nagpur 440012, Maharashtra Tel : (0712) 254 7129; Email : [email protected] DELHI Nirali Prakashan : 4593/15, Basement, Agarwal Lane, Ansari Road, Daryaganj Near Times of India Building, New Delhi 110002 Mob : 08505972553 Email : [email protected] BENGALURU Nirali Prakashan : Maitri Ground Floor, Jaya Apartments, No. 99, 6th Cross, 6th Main, Malleswaram, Bengaluru 560003, Karnataka; Mob : 9449043034 Email: [email protected] Other Branches : Hyderabad, Chennai Note : Every possible effort has been made to avoid errors or omissions in this book. In spite this, errors may have crept in. Any type of error or mistake so noted, and shall be brought to our notice, shall be taken care of in the next edition. It is notified that neither the publisher, nor the author or book seller shall be responsible for any damage or loss of action to any one of any kind, in any manner, therefrom. The reader must cross check all the facts and contents with original Government notification or publications. [email protected] | www.pragationline.com Also find us on www.facebook.com/niralibooks Preface … We take this opportunity to present this book entitled as “PHP” to the students of B.B.A (CA): Third Semester. The object of this book is to present the subject matter in a most concise and simple manner. The book is written strictly according to the New Syllabus (CBCS Pattern). The book has its own unique features. It brings out the subject in a very simple and lucid manner for easy and comprehensive understanding of the basic concepts, its intricacies, procedures and practices. This book will help the readers to have a broader view on PHP. The language used in this book is easy and will help students to improve their vocabulary of Technical terms and understand the matter in a better and happier way. We sincerely thank Shri. Dineshbhai Furia and Shri. Jignesh Furia of Nirali Prakashan, for the confidence reposed in us and giving us this opportunity to reach out to the students as well as teachers. We also thank to Mrs. Prajakta Khatavkar, Mrs. Anita Panajkar, Mrs. Prachi Sawant and Mr. Ravindra Walodare of M/s Nirali Prakashan for their excellent co- operation. We have given our best inputs for this book. Any suggestions towards the improvement of this book and sincere comments are most welcome on [email protected]. Authors Syllabus … 1. PHP Basics (6 Hrs.) 1.1 Setting up a development environment 1.2 Variables, numbers and strings 1.3 Calculations with PHP 1.4 Using Arrays 2. Control Structures and Loops (7 Hrs.) 2.1 Conditional Statements 2.2 Using Loops for Repetitive tasks 2.3 Combing Loops and Arrays 3. Functions, Objects and Errors (7 Hrs.) 3.1 PHP's Built-in functions 3.2 Creating Custom functions 3.3 Passing Values by Reference 3.4 Understanding Objects 4. Working with Forms (7 Hrs.) 4.1 Building a Form 4.2 Processing a Form's Data 4.3 Differences between POST and GET 4.4 Preserving User Input 5. More with Forms (7 Hrs.) 5.1 Dealing with checkboxes and radiobuttons 5.2 Retrieving values from lists 5.3 Validating and restricting data 5.4 Sending Email 6. Storing and Protecting Data (7 Hrs.) 6.1 Setting and Reading Cookies 6.2 Protecting Online Files 6.3 Understanding Session Variables 7. MySQL Database Overview (7 Hrs.) 7.1 phpMyAdmin Overview 7.2 Using a MySQL Database 7.3 Reading and Writing Data Contents … 1. PHP Basics 1.1 – 1.46 2. Control Structures and Loops 2.1 – 2.24 3. Functions, Objects and Errors 3.1 – 3.52 4. Working with Forms 4.1 – 4.14 5. More with Forms 5.1 – 5.30 6. Storing and Protecting Data 6.1 – 6.13 7. MySQL Database Overview 7.1 – 7.30 * Practical Programs P.1 – P.22 1… PHP Basics Objectives… [ ▣ To learn how to set up environment of PHP. ▣ To learn how to declare and assign values to variable of PHP. ▣ To study PHP arithmetic using different types of operators. ▣ To study Arrays and different types of Arrays like indexed array, associative array and multidimensional array of PHP. 1.1 INTRODUCTION PHP is recursive acronym "Hypertext Pre-processor" it is a widely used open source general purpose scripting language that is for developing web applications and its code can be easily embedded into HTML. PHP is so popular because it's very simple to learn, code and deploy on server, hence it has been the popular language amongst the developers. Like any other scripting language it allows developers to build logic into the creation of web page content and handle data returned from a web browser. PHP also contains a number of extensions that make it easy to interact with databases, extracting data to be displayed on a web page and storing information entered by a web site visitor back into the database. PHP consists of a scripting language and an interpreter. Like other scripting languages, PHP enables web developers to define the behavior and logic they need in a web page. 1.1.1 Features of PHP Some unique features of PHP are as follows: Performance: Scripts written in PHP executes faster than other scripting languages like JSP, ASP.NET and Perl. PHP designed to improve performance through optimized memory, also third party applications are available which improves the response time. (1.1) PHP (BBA (CA) : Sem-III) PHP Basics Portability: PHP is available for Windows, Unix, MAC OS and OS/2. So PHP programs are portable between platforms. Portability means program written on windows can execute on Unix without any changes. Ease of use: It is extremely sophisticated programming language, its syntax is very clear and consistent. Thats why programmers can easily read the code and that why it is favored for web based applications. Open source: PHP is open source project it means this language is developed by worldwide team of volunteers who makes it source code freely available. So there is no licensing fees required. This is an important factor for reducing software development cost. Also it makes robust because any developer can suggest changes in the source code of language and it can be added as per guidelines. Third party application support: PHP supports for different wide range of databases, like MySQL, Postgres SQL, oracle, Microsoft Server. It supports more than fifteen databases. PHP also supports XML to read and write the documents. Widely used: PHP is used on more than millions of websites from personal home pages to giants like facebook, wikipidea, tumblr, yahoo etc. Also, there are so many peoples who are using PHP for web development. 1.1.2 Communication Between Apache Server and Web Browser Now we will see how communication takes place between web browser and web server. When you are accessing a page using browser such as safari or firefox, it cause a little conversation to happen over the internet between your computer and another computer. When PHP is involved the server does more work for its half over the conversation. Internet 3 2 7 6 Web browser Apache php 5 4 MySQL DiskDrive 1 www.example.com/my.php/test.php Fig. 1.1: Communication between Apache server and browser 1.2 PHP (BBA (CA) : Sem-III) PHP Basics 1. When you request a PHP page on browser. 2. Browser sends message over the internet to the computer asking for www.example.com/my.php/test.php. 3. Apache server running www.example.com gets this message and ask for PHP engine. 4. The PHP engine reads the test.php file from the disk drive. 5. The PHP engine runs the commands and access the data with database such as mysql. 6. PHP engine takes the program output and sends back to apache HTTP server in turn apache server sends back the output to your browser. 7. The browser displays the output on to screen. 1.2 SETTING UP A DEVELOPMENT ENVIRONMENT We can install all in one packages for PHP like XAMPP, LAMPP or we can download and install PHP manually also. Firstly we will see how we install XAMPP, XAMPP stands for cross platform (Apache, MariaDB, PHP and Perl). o Apache: It is open source web server. Apache is the most widely used server worldwide for delivery of web content. The server application is made available as a free software by the Apache Software Foundation. o MySQL/MariaDB: In MySQL, XAMPP contains one of the most popular relational database management systems in the world. In combination with the web server Apache and the scripting language PHP, MySQL offers data storage for web services. Current XAMPP versions have replaced MySQL with MariaDB. o PHP: It is server-side programming language. PHP enables users to create dynamic websites or applications. PHP can be installed on all platforms and supports a number of diverse database systems. o Perl: The scripting language Perl is used in system administration, web development, and network programming. Like PHP, Perl also enables users to program dynamic web applications. We can install XAMPP from www.apachefriends.org site where you have to download its recent version, currently it is 7.4.7. After downloading the file, run.exe file, you have to just click on next and agree to everything. If error messages are not displayed, go to c:xampp/PHP and open the PHP.ini file, and make sure that everything under error handling and logging. When successfully installed it will show a dashboard through which you can start, stop the services and you can configure them like changing the ports etc. 1.3 PHP (BBA (CA) : Sem-III) PHP Basics Fig. 1.2 Open your browser and enter the URL http://localhost, it will open up the XAMPP welcome page. To ensure that the Apache and MySQL servers have been installed on system, restart your operating system, run the browser, and enter the http://localhost URL again. You should see the below screen on browser. Fig. 1.3 1.4 PHP (BBA (CA) : Sem-III) PHP Basics Most oftenly you will be able to redirect to home page of XAMPP if you can’t see it then you have to stop services which are running on port 80 or you can change the port of apache to another port number. Most commonly service is installed on http port 80 or 8080. You can save your programs in htdocs folder located in c:/xampp from where they will be fetched on localhost. Manually installing PHP on windows: PHP 5.5 and higher version requires at least windows 8, 2010 or higher versions of windows with 32 bit or 64 bit. You have to check necessary hardware configurations also before installing PHP. PHP requires Visual C Runtime (CRT). Many applications requires it so it may be installed. Installing PHP on a Windows 10 machine is not at all difficult. First of all let’s understand the steps involved. 1. First, we will download the latest PHP package from the PHP website. 2. Once we have the zip file we will create a PHP7 folder in the C drive and extract the contents of the zip file in this folder. 3. Configure php.ini file : you need to make some changes in this file. 4. Define extension directory: extension_dir= "c:\php/ext" 5. Change the path environment variable. 6. Now open Environment Variables (MyComputer -> System Properties -> Environment Variables), under system variables, select Path and click Edit, in the Variable value, go to the end of the list and put a semicolon, then add C:\php;C:\php\ext assuming C:\php is the location of your PHP installation. Development environment on Linux One of the most common server configuration is LAMP stack (Linux, Apache, MySQL, PHP). We will be using this set up and execute our programs on it. As this is the most commonly used and easy to install set up of PHP. Also these are all in one packages. For Linux we will try to install, apache, MySQL separately and try not to install third party applications. You can still install XAMPP if you would prefer to use that instead. But remember, this is a development environment and you should not try use/configure these third- party applications to run as a live server. Firstly we will check system updates for Linux. sudo apt-get update sudo apt-get upgrade sudo apt-get dist- upgrade 1.5 PHP (BBA (CA) : Sem-III) PHP Basics sudo means super user do it allows user to run programs as superuser (admin). First command will check for new update packages for Linux. And upgrade will upgrade Linux from current version to new version. Once your system is up to date you can install apache on your system with following command. sudo apt-get install Apache2 Now we will require the database also which is most commonly MySQL, to install it run following command on terminal. sudo apt-get install mysql-server sudo my_sql_secure_installation. It will ask you for password enter secure password and make sure both apache and MySQL both are installed to start apache server type the following command. sudo service apache2 start When you type the command, you will be able to see following screen. Fig. 1.4: 1.6 PHP (BBA (CA) : Sem-III) PHP Basics To start MySQL through terminal type following command, sudo mysql -u root You will be able to login to MySQL database in our case we have installed Mariadb, Fig. 1.5 Once you installed apache and MySQL, then you can install PHP by running following command, sudo apt-get install PHP Now this will install PHP in its default settings, so you will need to locate the PHP.ini file which can be located in etc/PHP/7.4/apcache2 and also in etc/PHP/7.4/cli. If you want PhpMyAdmin for setting up and making or altering your database in much more flexible way, you can use following command, sudo apt-get install PHPmyadmin You need to change the configure apache to include PHPmyadmin with following command, sudo echo -e "\include etc/PHPmyadmin/apache.conf" >>/etc/apache2/apache2.conf This is how you are ready to execute and run the PHP programs on linux. 1.3 PHP OVERVIEW PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. 1.3.1 PHP Syntax A PHP script can be placed anywhere in the document. A PHP script starts with. 1.7 PHP (BBA (CA) : Sem-III) PHP Basics The default file extension for PHP files is ".php". A PHP file normally contains HTML tags, and some PHP scripting code. Syntax: Program 1.1: PHP program to display "Hello World". Output: Hello World! Above, program of a simple PHP file, that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page. 1.3.2 PHP Comments Comments are mostly used for documentation purpose to explain the functionality of code and to make your program self explanatory and readable. Comments are skipped by compiler while executing the script. There are two types of comments PHP provides us single line comments and multiple line comments. 1. Single Line Comments: PHP single line comments begins with //. The code of next line will be taken for execution by compiler. Program 1.2: PHP program for single line comment.

Use Quizgecko on...
Browser
Browser