Title: Introduction to PHP
1Introduction to PHP
2What Is PHP? Stands for Personal Home Page
orPHP Hypertext
- Advantages of Using PHP to enhance Web pages
- Easy to use.
- Open source.
- Multiple platform.
3How PHP Pages are Accessed and Interpreted
4Getting Started with PHP
- To develop and publish PHP scripts all you need
is - A Web server with PHP built into it
- A client machine with a basic text editor and
Internet connectionTextPad - FTP or Telnet softwareat Conestoga, we are
saving the files under C\Inetpub\wwwroot\name.p
hp
5Exploring 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.
6Creating 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
statement.
7Alternative 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.
8Proper 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
9If Use Improper Syntax
- Suppose you use the wrong syntax
- lt?php print ( A simple initial script)
- ?gt
-
10Php not giving errors
- If php is not giving errors
- Open the php.ini file under c\php5 in textpad,
approx line 357 - Change the
- Display_errorson
-
11PHP'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.
-
-
12Embedding PHP Statements Within HTML Documents
save as programName.php, to run
http//localhost/programName.php
- 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
Pagelt/fontgt - ltscript text phpgt
- print ("Using PHP is not hard")
- lt/scriptgt
- this is instead of Hello World
- lt/bodygtlt/htmlgt
13Would Output The Following ...
14Using Backslash (\) to Generate HTML Tags with
print()
- 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
beoutputprint ("ltfont color\"blue\"gt") - The above statement would output
- ltfont color"blue"gt
15Using 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.
16Using 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
line - ?gt
17Example 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
list - print ("lt/bodygtlt/htmlgt")
- ?gt
18Alternative Comment Syntax
- PHP allows a couple of additional ways to create
comments. - lt?php
- phpinfo() This is a built-in
function - ?gt
- Multiple line comments. lt?php
- /
- A script that gets information about the
- PHP version being used.
- /
- lt? phpinfo() ?gt
19Using 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.
20Assigning 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.
21Selecting 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. - Must follow Conestoga Standards camelNotation
22Combining 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
old. - age6
- print ("Bryant is age years old.")
23 A Full Example ...
- lthtmlgt
- ltheadgt lttitlegtVariable Example lt/titlegt lt/headgt
- ltbodygt
- lt?php
- firstNum 12
- secondNum 356
- temp firstNum
- firstNum secondNnum
- secondNum temp
- print (First Number firstNum ltbrgtSecond Number
secondNum") - ?gt lt/bodygt lt/htmlgt
24 Gives you ...
25 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
- totalFruit apples oranges
- print ("The total number of fruit is
totalFruit") - ?gt
- These PHP statements would output
- The total number of fruit is 26.
26Common PHP Numeric Operators
27Example
- lthtmlgt
- ltheadgt lttitlegtVariable Example lt/titlegt lt/headgt
- ltbodygt
- lt?php
- columns 20
- rows 12
- totalSeats rows columns
- ticketCost 3.75
- totalRevenue totalSeats ticketCost
-
- building_cost 300
- profit totalRevenue - buildingCost
- print ("Total Seats are totalSeats ltbrgt")
- print ("Total Revenue is totalRevenue
ltbrgt") - print ("Total Profit is profit")
- ?gt
- lt/bodygt lt/htmlgt
28Gives you ...
29WARNING 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
30Example
- 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
31 Gives you ...
32Working 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.
33WARNING Be Careful Not to Mix Variable Types
- Be careful not to mix string and numeric variable
types. - 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
34Using 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")
35TIP 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
36The 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
37The strlen() Function Example
- lt?php
- comments "Good Job"
- len strlen(comments)
- print ("Lengthlen")
- ?gt
This PHP script would output Length8.
38The trim() Function
- This function removes any blank characters from
the beginning and end of a string. For example,
consider the following script - lt?php
- inName " Easter Bunny"
- name trim(inName)
- print ("namenamename")
- ?gt
- Would Print nameEaster BunnnyEaster Bunny
39The 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.
40The substr() Function
- Substr has the following general format
41The 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/2009"
- month substr(date, 0, 2)
- day substr(date, 3, 2)
- print ("Monthmonth Dayday")
- ?gt
42The 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/2099"
- year substr(date, 6)
- print ("Yearyear")
- ?gt
- The above script segment would output Year2099.
43Example with Buttons
lthtmlgt ltheadgt lttitlegt A Simple Form lt/titlegt
lt/headgt ltbodygt ltform actionnameOfThePHP.php"meth
od"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
44Gives you ...
45Receiving Input into PHP
- 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
46To Receive Data from HTML
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
47Another Example.
- 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 Input.lt/fontgt
- lt?php
- print ("ltbrgtYour email address is email")
- print ("ltbrgt Contact preference is contact")
- ?gt
48 Gives you ...
49Create Using textpad or Dreamweaver this html,
Save in C\inetpub\wwwroot\exercise?
50Receiving File in the same location, same name
with .php as the extension