PHP : Hypertext Preprocessor - PowerPoint PPT Presentation

About This Presentation
Title:

PHP : Hypertext Preprocessor

Description:

PHP : Hypertext Preprocessor – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 13
Provided by: Teren88
Category:

less

Transcript and Presenter's Notes

Title: PHP : Hypertext Preprocessor


1
PHP Hypertext Preprocessor
2
What is PHP for?
  • Unlike JavaScript which is a client-side script
    language, PHP is a server side script
  • PHP is processed on the server, therefore you can
    not use the View/Source menu command to view
    someone else's PHP code. Finally someone is
    protecting my code. ?
  • PHP programs/scripts perform several basic
    operations
  • Obtain data from a user
  • Perform computations
  • Access and manipulate data stored in files and
    databases
  • Display data so that a user can view it.

3
My first PHP script
  • Inside the regular html file
  • You include the following tag
  • lt?php
  • // code here
  • ?gt
  • Make sure you save it as .php

4
My first PHP script pex1.php
  • lt?php
  • echo 'Hello World'
  • echo 'ltbr /gt lth1gt Hello Worldlt/h1gt'
  • echo 'ltemgt Hello World lt/emgt '
  • ?gt
  • ! Before you upload to your server, make sure you
    make a directory call php so we know where to
    look for your code.

5
PHP Script
  • Note that inside the echo command, it is similar
    to writing code on regular HTML.
  • Remember that the PHP script will be
    pre-processed.
  • Therefore when you try and view the code, it does
    not have the php scripts but rather the final
    processed text.
  • Therefore, you have to upload it to the server to
    see how it looks like. If you preview, you will
    see garbage.

6
Variables
  • Similar to JavaScript, you can work with
    variables.
  • Variables must begin with a dollar sign ().
  • You can generally name a variable anything you
    like as long as it begins with a and a letter
    or a underscore _.
  • For example, a good variable name might be
    firstName or first_name.
  • DO NOT include blank spaces in a variable name.
  • If you choose to make a variable name out of two
    or more words, I recommend using an underscore (
    _ ) between the two words or capitalizing the
    second word so the variable name is easier to
    read.

7
Variables
  • A variable can store integers as in
  • num 5
  • or it can store a decimal number as in
  • price 19.99
  • or it can store a string (i.e. a word or phrase)
    as in
  • name 'John Doe'
  • Variables in php do not need to be declared with
    specific data types

8
Variables
  • You can use the dot operator ( . ) to concatenate
    two strings together into one string. Remember to
    add a blank space if necessary with ' ' or " ".
    For example the following code segment will
    result in the name "John Doe" being stored in the
    variable wholeName.
  • firstName 'John'
  • lastName 'Doe'
  • wholeName firstName . ' ' . lastName

9
Using comments
  • Especially when you are still new to the language
    it is good to put comments around your code
  • Three ways to include a PHP comment
  • Anything typed to the right of two forward
    slashes // is a comment as in // this is a
    comment
  • Anything enclosed within the symbols / and / is
    a comment. This kind of comment can extend to
    multiple lines of code
  • / this is a comment this is still a
    comment this is a comment /
  • Anything typed to the right of a pound symbol (
    ) is a comment
  • this is a comment

10
Process data from a form
  • PHP to access data submitted by a form
  • PHP creates an array using data sent using a POST
    action request.
  • To assess variables in a POST request, you can
    use the _POST array.
  • e.g. ltinput type"text" name"val1" value""gt
  • _POST'val1' will have the value of what you
    store in the textbox above.

11
Let us put it together using formspex2.html
pex2.php
  • pex2.html
  • lthtmlgt
  • ltheadgt lt/headgt
  • ltbodygt
  • ltform action "pex2.php" method"post"gt
  • Enter your message
  • ltinput type"text" name"msg" size"30"gt
  • ltinput type"submit" value"Send"gt
  • lt/formgtlt/bodygt
  • lt/htmlgt
  • pex2.php
  • lt?php
  • input_POST'msg'
  • echo "You said input"
  • ?gt

12
pex3.html, pex3.php
Error Message 1
Error Message 2
Correct Response
  • Create a form that allows you to enter your first
    and last name.
  • Check that both names are not empty using PHP
    using strlen().
  • The command trim(variable_name) trims any spaces
    in the beginning or end and returns the new
    string.
  • The command strlen(variable_name) counts the
    number of characters in the variable_name.
  • So you can combine strlen(trim(variable_name) to
    count the number of characters omitting any
    spaces in front or back.
  • The if (condition) works the same as JavaScript
    eg.
  • if (a b)
  • echo (ltbgtHELPlt/bgt)
Write a Comment
User Comments (0)
About PowerShow.com