Chapter 4: Basics of PHP 42. Topics Introduction Creating and Executing PHP Programs Variables Operators Constant Arrays String Processing and Regular.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

1 PHP  Introduction to PHP  Basic Syntax of PHP Variables, Constant, Data Type, Type Casting  Getting Data From Client  PHP Database Manipulation.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Lecture 2 Introduction to C Programming
Introduction to C Programming
PHP Introduction.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic PHP.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(26) PHP (Personal Home Page)
Guide To UNIX Using Linux Third Edition
Introduction to PHP (Part-1) Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to C Programming
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
1 CS 482 Web Programming and Scripting Week 6 – Installing Apache, PHP, MySQL and Using the Software.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
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 .
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 26 - PHP Outline 26.1 Introduction 26.2 PHP 26.3 String Processing and Regular Expressions 26.4.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 26 - PHP Outline 26.1 Introduction 26.2 PHP 26.3 String Processing and Regular Expressions 26.4.
PHP Introduction Internet teknologi 2 (ITNET2). Ingeniørhøjskolen i Århus Slide 2 Agenda PHP Introduction –PHP Basic Facts –PHP History –PHP Platform.
Chap 3: Web Programming (CCSB144) - PHP 42 Chapter 4 - PHP Outline Introduction PHP String Processing and Regular Expressions Viewing Client/Server Environment.
 2008 Pearson Education, Inc. All rights reserved PHP.
Jozef Goetz contribution, Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
INTERNET APPLICATION DEVELOPMENT For More visit:
COP 3813 Intro to Internet Computing Prof. Roy Levow PHP.
Nael Alian Introduction to PHP
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
 2003 Prentice Hall, Inc. All rights reserved. 1 PHP Outline 26.1 Introduction 26.2 PHP 26.3 String Processing and Regular Expressions 26.4 Viewing Client/Server.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Input, Output, and Processing
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
 2008 Pearson Education, Inc. All rights reserved PHP.
Introduction to PHP.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
PHP Form Processing * referenced from
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Fundamentals of Programming I Overview of Programming
Chapter 19 PHP Part I Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Chapter 6 JavaScript: Introduction to Scripting
Chapter 26 - PHP Outline 26.1 Introduction 26.2 PHP
Chapter 2 Introduction to C++ Programming
23 PHP.
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 ©
Introduction to Scripting
Introduction to C++ Programming
WEB PROGRAMMING JavaScript.
PHP.
Introduction to C++ Programming
PHP PART 2.
23 PHP.
Presentation transcript:

Chapter 4: Basics of PHP 42

Topics Introduction Creating and Executing PHP Programs Variables Operators Constant Arrays String Processing and Regular Expressions 43

Introduction PHP –PHP: Hypertext Preprocessor –Originally called “Personal Home Page Tools” –Popular server-side scripting technology –Open-source Anyone may view, modify and redistribute source code Supported freely by community –Platform independent 44

Introduction Basic application –Scripting delimiters Must enclose all script code –Variables preceded by $ symbol Case-sensitive –End statements with semicolon –Comments // for single line /* */ for multiline –Filenames end with.php by convention 45

first.php (1 of 1) 46 Declare variable $name Scripting delimiters Single-line comment Function print outputs the value of variable $name

Introduction 47 Fig Simple PHP program.

Creating and executing PHP programs Install wamp Run the wampserver –Make sure the speedometer is fully in white color Sunday, September 06, 2015 created by Mr Azlan Yusof, edited by F.Hani 48

Creating and executing PHP programs Then, install editor. If you want to try other editor to replace Notepad or Dreamweaver, go to Crimson Editor site to download the editor. It’s free. Some of the Crimson Editor features: –Syntax highlighting –Undo/ redo –Spell checker –And more…. Sunday, September 06, 2015 created by Mr Azlan Yusof, edited by F.Hani 49

Creating and executing PHP programs Screen Capture of Crimson Editor.

Creating and executing PHP programs Testing your installation –Open up editor. –Enter the following code, exactly as it appears here, into a blank text file. Sunday, September 06, 2015 created by Mr Azlan Yusof, edited by F.Hani 51 <?php phpinfo(); ?>

Creating and executing PHP programs Save the file in your web server’s root directory as phpinfo.php. Where is your web server’s root directory ? In wamp/ www. You will have this folder in your PC after installing Wamp server. Double check that you have saved the file in the right place, with the right name. It should be in your web server’s root directory. Sunday, September 06, 2015 created by Mr Azlan Yusof, edited by F.Hani 52

Creating and executing PHP programs Then, Type in in address bar. If you see the following screen, congratulations! PHP is up and running on your web server. Sunday, September 06, 2015 created by Mr Azlan Yusof, edited by F.Hani 53

Creating and executing PHP programs Let us try another simple PHP codes. Type below codes. Save as try.php. Sunday, September 06, 2015 created by Mr Azlan Yusof, edited by F.Hani 54 PHP Test Hello World "; ?>

Creating and executing PHP programs View the output by following the steps explained before. Here is the output: Why do I create try.php, not try.html ? –Since HTML file cannot contain any PHP codes, our first file should be saved with.php extension. 55

Variables –Can have different types at different times –Type conversions settype function Type casting –Concatenation operator. (period) 56

Data types 57

data.php (1 of 3) 58 Assign a string to variable $testString Assign a double to variable $testDouble Assign an integer to variable $testInteger

data.php (2 of 3) 59 Print each variable’s value Call function settype to convert the data type of variable $testString to a double. Call function settype to convert the data type of variable $testString to an integer. Convert variable $testString back to a string

data.php (3 of 3) 60 Use type casting to cast variable $data to different types

Data types 61 Fig Type conversion.

Operators To help you perform calculations and computations, PHP includes an assortment of useful operators. Some of the familiar operators: 62 +Addition -Substraction *Multiplication /Division %Modulus

Operators Example: <?php //Calculate the area of a circle of given radius echo(“ Examples ”); $radius= 2.0; $pi= $area= $pi * $radius * $radius; echo(“radius = ”); echo($radius); echo(“ area = ”); echo($area); ?> 63

Operators Output: Examples radius = 2 area =

Constants –Named values –define function 65

operators.php (1 of 3) 66 Define constant VALUE.Add constant VALUE to variable $a.

operators.php (2 of 3) 67 Multiply variable $a by two using the multiplication assignment operator *=. Test whether variable $a is less than 50 Add 40 to variable $a using the addition assignment operator +=. Print if variable $a is less than 50.

operators.php (3 of 3) 68 Add constant VALUE to an uninitialized variable. Add a string to an integer. Print an uninitialized variable ( $nothing ).

69 Fig Using PHP’s arithmetic operators.

Arrays –Group of related data Elements –Name plus braces and index Indices start at zero –count function –array function 70

Arrays Arrays, cont. –Built-in iterators Maintain pointer to element currently referenced reset key Next foreach loops 71

PHP keywords 72

arrays.php (1 of 3) 73 Create the array $first by assigning a value to an array element. Assign a value to the array, omitting the index. Appends a new element to the end of the array. Use a for loop to print out each element’s index and value. Function count returns the total number of elements in the array.

arrays.php (2 of 3) 74 Call function array to create an array that contains the arguments passed to it. Store the array in variable $second. Assign values to non-numerical indices in array $third. Function reset sets the internal pointer to the first element of the array. Function key returns the index of the element which the internal pointer references. Function next moves the internal pointer to the next element.

arrays.php (3 of 3) 75 Operator => is used in function array to assign each element a string index. The value to the left of the operator is the array index, and the value to the right is the element’s value.

Arrays 76 Fig Array manipulation.

String Processing and Regular Expressions String processing –Equality and comparison two important operations –strcmp function Returns –1 if string 1 < string 2 Returns 0 if string 1 = string 2 Returns 1 if string 1 > string 2 –Relational operators 77

compare.php (1 of 2) 78 Use a for loop to iterate through each array element.Function strcmp compares two strings. If the first string alphabetically precedes the second, then –1 is returned. If the strings are equal, 0 is returned. If the first string alphabetically follows the second, then 1 is returned.

compare.php (2 of 2) 79 Use relational operators to compare each array element to string “apple”.

String Processing and Regular Expressions 80 Fig Using the string comparison operators.

String Processing Regular expressions –Pattern matching templates –ereg function <?php $search = if ( ereg $search) == true ) print ("string was found."); ?> 81

Verifying a Username and Password 82 Fig XHTML form for obtaining a username and password.

password.php (1 of 7) 83 Variable names, when preceded by the logical negation operator ( ! ), return true if they are empty or set to 0. This checks if a user has submitted a form without specifying a username or password. Function fieldsBlank is called if the user has submitted an incomplete form to notify the user that all form fields must be completed. Function isset tests whether the user has pressed the New User button, indicating that a new user must be added. To add a new user, we open the file password.txt in append mode and assign the file handle that is returned to variable $file.

password.php (2 of 7) 84 Print an error message and terminate script execution if the file cannot be opened. Function fputs writes the name and password to the text file.. Function userAdded is called to print a message to the user to indicate that the username and password were added to the file.

password.php (3 of 7) 85 Before entering the while loop, variable $userVerified is set to 0. The while loop executes as long as the there are more lines in the file to read and variable $userVerified is still 0 or empty. Function fgets reads a line from the text file. The result is assigned to variable $line. Function chop removes the newline character from the end of the line. Function split is called to separate the string at the specified delimiter (in this case, a comma). The resulting array is stored in array $field. The username entered by the user is tested against the one returned in the text file (stored in the first element of the array). If they match, variable $userVerified is set to 1. Function checkPassword is called to verify the user’s password. Variable $PASSWORD and array $field are passed to the function. If function checkPassword returns true, function accessGranted is called to notify the client that permission has been granted. Otherwise, function wrongPassword is called.

password.php (4 of 7) 86 After the while loop has executed, function fclose is called to close the file. If variable $userVerified has not been set to a value other than 0, function accessDenied is called to notify the client that access has been denied. Function checkPassword compares the user’s password to the password in the file. If they match, true is returned, whereas false is returned if they do not.

password.php (5 of 7) 87 Function userAdded prints a message to the client indicating that the user has been added. Function accessGranted prints a message to the client indicating that permission has been granted.

password.php (6 of 7) 88 Function wrongPassword prints a message to the client indicating that the password is invalid. Function accessDenied prints a message to the client indicating that access has been denied.

password.php (7 of 7) 89 Function fieldsBlank prints a message to the client indicating that all form fields have not been completed.

Verifying a Username and Password 90 Fig Verifying a username and password.

password.txt (1 of 1) 91