Introduction to PHP - PowerPoint PPT Presentation


PPT – Introduction to PHP PowerPoint presentation | free to download - id: 213562-YzhlN


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Introduction to PHP


To develop and publish PHP scripts all you need is: A Web ... br input type='submit' value='Click To Submit' input type='reset' value='Erase and Restart' ... – PowerPoint PPT presentation

Number of Views:9181
Avg rating:3.0/5.0
Slides: 54
Provided by: Addi93
Learn more at:


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Introduction to PHP

Introduction to PHP
What Is PHP? Stands for Personal Home Page
or PHP Hypertext
  • Advantages of Using PHP to enhance Web pages
  • Easy to use.
  • Open source.
  • Multiple platform.

How PHP Pages are Accessed and Interpreted
Getting Started with PHP
  • To develop and publish PHP scripts all you need
  • A Web server with PHP built into it
  • A client machine with a basic text editor and
    Internet connection……TextPad
  • FTP or Telnet software…at Conestoga, we are
    saving the files under C\Inetpub\wwwroot\phppro

Exploring the Basic PHP Development Process
  • The basic steps you can use to develop and
    publish PHP pages are
  • 1. Create a PHP script file and save it to a
    local disk using .php extension.
  • 2. Use copy to copy the file to the server.
  • 3. Access your file using a browser.

Creating a PHP Script File and Saving It to a
Local Disk
  • You can use a number of different editors to
    create your PHP script files.
  • The PHP script starts with a lt?php tag and ends
    with ?gt.
  • Between these tags is a singlePHP print

Alternative PHP Delimiters
  • You can alternatively start your PHP scripts with
    the ltscriptgt tag as follows
  • ltscript language"PHP"gt
  • print ("A simple initial script")
  • lt/scriptgt
  • If have short_open_tag enabled in its
    configuration file, you can use lt? and ?gt.
  • If asp_tags is enabled in the PHP configuration
    file, you can use lt and gt as delimiters.

Copying Files To A Web Server with FTP….we are
not doing this…copying to Drive C
  • 1. Connect to the Internet and start FTP.
  • 2. Connect to your Web server with FTP.
  • 3. Copy files
  • to the Web
  • server.

Accessing Your File Using a Browser
Proper Syntax
  • If you have a syntax error then you have written
    one or more PHP statements that are grammatically
    incorrect in the PHP language.
  • The print statement syntax

If Use Improper Syntax
  • Suppose you use the wrong syntax
  • 1. lt?php 2. print ( A simple initial script)
  • 3. ?gt

A Little About PHP's Syntax
  • Some PHP Syntax Issues
  • Be careful to use quotation marks, parentheses,
    and brackets in pairs.
  • Most PHP commands end with a semicolon ().
  • Be careful of case.
  • PHP ignores blank spaces.

Embedding PHP Statements Within HTML Documents
  • One way to use PHP is to embed PHP scripts
    within HTML tags in an HTML document.
  • lthtmlgt
  • ltheadgt
  • lttitlegtHTML With PHP Embeddedlt/titlegt lt/headgt
  • ltbodygt
  • ltfont size5 color"blue"gtWelcome To My
  • lt?php
  • print ("ltbrgt Using PHP is not hardltbrgt")
  • ?gt
  • this is instead of Hello World
  • lt/bodygtlt/htmlgt

Would Output The Following ...
Using Backslash (\) to Generate HTML Tags with
  • Sometimes you want to output an HTML tag that
    also requires double quotation marks.
  • Use the backslash (\) character to signal that
    the double quotation marks themselves should
    be output print ("ltfont color\"blue\"gt")
  • The above statement would output
  • ltfont color"blue"gt

Using Comments with PHP Scripts
  • Comments enable you to include descriptive text
    along with the PHP script.
  • Comment lines are ignored when the script runs
    they do not slow down the run-time.
  • Comments have two common uses.
  • Describe the overall script purpose.
  • Describe particularly tricky script lines.

Using Comments with PHP Scripts
  • Comment Syntax - Use //
  • lt?php
  • // This is a comment
  • ?gt
  • Can place on Same line as a statement
  • lt?php
  • print ("A simple initial script") //Output a
  • ?gt

Example Script with Comments
  • lthtmlgt ltheadgt
  • lttitlegt Generating HTML From PHPlt/titlegt lt/headgt
  • ltbodygt lth1gt Generating HTML From PHPlt/h1gt
  • lt?php
  • //
  • // Example script to output HTML tags
  • //
  • print ("Using PHP has ltigtsome advantageslt/igt")
  • print ("ltulgtltligtSpeedlt/ligtltligtEase of uselt/ligt
  • ltligtFunctionalitylt/ligtlt/ulgt") //Output bullet
  • print ("lt/bodygtlt/htmlgt")
  • ?gt

Alternative Comment Syntax
  • PHP allows a couple of additional ways to create
  • lt?php
  • phpinfo() This is a built-in
  • ?gt
  • Multiple line comments. lt?php
  • /
  • A script that gets information about the
  • PHP version being used.
  • /
  • lt? phpinfo() ?gt

Using PHP Variables
  • Variables are used to store and access data in
    computer memory.
  • A variable name is a label used within a script
    to refer to the data.

Assigning New Values to Variables
  • You can assign new values to variables
  • days 3
  • newdays 100
  • days newdays
  • At the end of these three lines, days and
    newdays both have values of 100.

Selecting Variable Names
  • You can select just about any set of characters
    for a variable name in PHP, but they must
  • Use a dollar sign () as the first character
  • Use a letter or an underscore character (_) as
    the second character.
  • NoteTry to select variable names that help
    describe their function. For example counter is
    more descriptive than c or ctr.

Combining Variables and the print Statement
  • That is, to print out the value of x, write the
    following PHP statement
  • print ("x")
  • The following code will output Bryant is 6 years
  • age6
  • print ("Bryant is age years old.")

A Full Example ...
  • lthtmlgt
  • ltheadgt lttitlegtVariable Example lt/titlegt lt/headgt
  • ltbodygt
  • lt?php
  • first_num 12
  • second_num 356
  • temp first_num
  • first_num second_num
  • second_num temp
  • print ("first_num first_num ltbrgtsecond_numsec
  • ?gt lt/bodygt lt/htmlgt

Gives you ...
Using Arithmetic Operators
  • You can use operators such as a plus sign () for
    addition and a minus sign () for subtraction to
    build mathematical expressions.
  • For example
  • lt?php
  • apples 12
  • oranges 14
  • total_fruit apples oranges
  • print ("The total number of fruit is
  • ?gt
  • These PHP statements would output
  • The total number of fruit is 26.

Common PHP Numeric Operators
A Full Example
  • lthtmlgt
  • ltheadgt lttitlegtVariable Example lt/titlegt lt/headgt
  • ltbodygt
  • lt?php
  • columns 20
  • rows 12
  • total_seats rows columns
  • ticket_cost 3.75
  • total_revenue total_seats ticket_cost
  • building_cost 300
  • profit total_revenue - building_cost
  • print ("Total Seats are total_seats ltbrgt")
  • print ("Total Revenue is total_revenue ltbrgt")
  • print ("Total Profit is profit")
  • ?gt lt/bodygt lt/htmlgt

Gives you ...
WARNING Using Variables with Undefined Values
If you accidentally use a variable that does not
have a value assigned to it will have no value
(called a null value). When a variable with a
null value is used in an expression PHP, PHP may
not generate an error and may complete the
expression evaluation. For example, the
following PHP script will output x y4. lt?php
y 3 yy x 1 // x has a null value
print ("xx yy") ?gt
A Full Example
  • lthtmlgt
  • ltheadgt lttitlegtExpression Example lt/titlegt lt/headgt
  • ltbodygt
  • lt?php
  • grade1 50
  • grade2 100
  • grade3 75
  • average (grade1 grade2 grade3) / 3
  • print ("The average is average")
  • ?gt lt/bodygt lt/htmlgt

Gives you ...
Working with PHP String Variables
  • Character strings are used in scripts to hold
    data such as customer names, addresses, product
    names, and descriptions.
  • Consider the following example.
  • name"Christopher"
  • preference"Milk Shake"
  • name is assigned Christopher and the variable
    preference is assigned Milk Shake.

WARNING Be Careful Not to Mix Variable Types
  • Be careful not to mix string and numeric variable
  • For example, you might expect the following
    statements to generate an error message, but they
    will not. Instead, they will output y1.
  • lt?php
  • x "banana"
  • sum 1 x
  • print ("ysum")
  • ?gt

Using the Concatenate Operator . Vs
  • The concatenate operator combines two separate
    string variables into one.
  • For example,
  • fullname firstname . lastname
  • fullname will receive the string values of
    firstname and lastname connected together.
  • For example,
  • firstname "John"
  • lastname "Smith"
  • fullname firstname . lastname
  • print ("Fullnamefullname")

TIP An Easier Way to Concatenate Strings
  • You can also use double quotation marks to create
  • concatenation directly,
  • For example,
  • Fullname "FirstName LastName"
  • //This statement has the same effect as
  • Fullname FirstName . " " . LastName

The strlen() Function
  • Most string functions require you to send them
    one or more arguments.
  • Arguments are input values that functions use in
    the processing they do.
  • Often functions return a value to the script
    based on the input arguments. For example

The strlen() Function Example
  • lt?php
  • comments "Good Job"
  • len strlen(comments)
  • print ("Lengthlen")
  • ?gt

This PHP script would output Length8.
The trim() Function
  • This function removes any blank characters from
    the beginning and end of a string. For example,
    consider the following script
  • lt?php
  • in_name " Santa Claus "
  • name trim(in_name)
  • print ("namenamename")
  • ?gt
  • Would Print nameSanta ClausSanta Claus

The strtolower() and strtoupper() Functions
  • These functions return the input string in all
    uppercase or all lowercase letters, respectively.
  • For example,
  • lt?php
  • inquote "Now Is The Time"
  • lower strtolower(inquote)
  • upper strtoupper(inquote)
  • print ("upperupper lowerlower")
  • ?gt
  • The above would output upperNOW IS THE TIME
    lowernow is the time.

The substr() Function
  • Substr has the following general format

The substr() Function
  • The substr() function enumerates character
    positions starting with 0 (not 1),
  • For example, in the string Homer, the H would
    be position 0, the o would be position 1, the
    m position 2, and so on.
  • For example, the following would output
  • Month12 Day25.
  • lt?php
  • date "12/25/2004"
  • month substr(date, 0, 2)
  • day substr(date, 3, 2)
  • print ("Monthmonth Dayday")
  • ?gt

The substr() Function
  • As another example, consider the following use of
    the substr() function
  • It does not include the third argument (and thus
    returns a substring from the starting position to
    the end of the search string).
  • lt?php
  • date "12/25/2004"
  • year substr(date, 6)
  • print ("Yearyear")
  • ?gt
  • The above script segment would output Year2004.

Another Full Script Example
lthtmlgt ltheadgt lttitlegt A Simple Form lt/titlegt
lt/headgt ltbodygt ltform action""method"post"
gt Click submit to start our initial PHP
program. ltbrgt ltinput type"submit" value"Click
To Submit"gt ltinput type"reset" value"Erase and
Restart"gt lt/formgt lt/bodygt lt/htmlgt
Gives you ...
Receiving Form Input into PHP Scripts
  • To receive HTML form input into a PHP script
  • Use a PHP var name that matches the variable
    defined in the form elements name argument.
  • For example, if form uses the following
  • ltinput type"radio" name"contact" value"Yes"gt
  • Then form-handling PHP script could use a
    variable called contact.
  • If the user clicks the radio button, then
    contact would Yes

Full Example (2 Files)
  • Suppose your HTML form uses the following
  • Enter email address ltinput type"text" size"16"
    maxlength"20" name"email"gt
  • ltform action"receiving.php" method"post"gt
  • Then can receive input as follows
  • lthtmlgt
  • ltheadgtlttitlegt Receiving Input lt/titlegt lt/headgt
  • ltbodygt
  • ltfont size5gtThank You Got Your
  • lt?php
  • print ("ltbrgtYour email address is email")
  • print ("ltbrgt Contact preference is
  • ?gt

Gives you ...
  • Since PHP 4.2.1, the default PHP configuration is
    require a different mechanism to receive input
    for security reasons (than the one just shown).
  • Technical details it is a PHP configuration
    option to turn REGISTER_GLOBALS OFF (new default)
    or ON in the php.ini configuration file.
  • If your site has REGISTER_GLOBALS OFF you must
    use a different mechanism to receive HTML Form

How can you tell if Register_Globals is OFF?
  • Enter the following PHP script and run it.
  • lt?PHP phpinfo() ?gt
  • Search through the output for REGISTER_GLOBALS
    and see if it is set to OFF or ON.
  • If it is off you must use the following way to
    receive input data.

Getting input data with Register_Globals OFF?
  • To receive data with REGISTER_GLOBALS OFF you use
    a special variable called POST.
  • _POST'name' ?gt.

Enclose in square bracket and then quotes
Name of HTML form variable (note do not use )
Special PHP Global variable. Technically it is an
associative array
Full Example, when REGISTER_GLOBALS is OFF.
  • Suppose your HTML form uses the following
  • Enter email address ltinput type"text" size"16"
    maxlength"20" name"email"gt
  • Then can receive input as follows
  • lthtmlgt
  • ltheadgtlttitlegt Receiving Input lt/titlegt lt/headgt
  • ltbodygt
  • lt?php
  • email _POSTemail
  • contact _POSTcontact
  • ?gt
  • ltfont size5gtThank You Got Your
  • lt?php
  • print ("ltbrgtYour email address is email")
  • print ("ltbrgt Contact preference is contact")
  • ?gt

Gives you ...