PHP 1 by mamatazroy91

VIEWS: 144 PAGES: 29

									Server-side Scripting with PHP
   Client-side Scripting VS Server-side
• Client-side scripting
   – is about "programming" the behavior of the browser
   – If code is executed on your browser, it is considered client-
   – Use HTML, XHTML, DHTML, Java Applet, Flash File, Active
     X Control and Javascript technologies.
• Server-side scripting
   – is about "programming" the behavior of the server.
   – If code is executed on the Web server, it is considered
     server side code.
   – This can be a program written in a variety of languages.
     Some of the programming languages and technologies that
     can be used are Active Server Pages (ASP), PHP, C/C++, and
     ISAPI, Java Server Pages (JSP), and more.
      What is Server-side Scripting?
• Normally when a browser requests an HTML file, the
  server returns the file, but if the file contains a server-
  side script, the script inside the HTML file is executed
  by the server before the file is returned to the browser
  as plain HTML.

• At W3Schools the server-side scripting by using Active
  Server Pages (ASP) and Hypertext Preprocessor (PHP).

• You cannot view the ASP or PHP source code by
  selecting "View source" in a browser, you will only see
  the output from the server, which is plain HTML. This is
  because the scripts are executed on the server before
  the result is sent back to the browser as plain HTML.
      What can Server Scripts Do?
• Dynamically edit, change or add any content
  to a Web page
• Respond to user queries or data submitted
  from HTML forms
• Access any data or databases and return the
  results to a browser
• Customize a Web page to make it more useful
  for individual users
• Provide security since your server code cannot
  be viewed from a browser
Preparing environment for web
   Server Side Web Development
• Web server –Apache,IIS,
• Programming Language – PHP, ASP, Perl, Java
  Server Page(jsp)
• Database – MySQL, Sybase,Oracle
• Package-Wamp, Xampp
        Wamp Installation
             Download at:
License Agreement
Default Directory
Create icon
Installation in progress
Mail Parameters
Finish Installation
             Wampserver 2.1 Admin

•Right click on this icon and then click on “Start All Services”.

•This will start the apache web server along with everything we
need to run PHP pages on our machine
•Now open up your web browser and type in http://localhost.
•You should see the WAMP welcome page.
•When you type in http://localhost in your browser, it executes
the index.php file in your www folder.
•All our php files will go in the www folder
Introduction to PHP
                What is PHP?
• PHP stands for PHP: Hypertext Preprocessor
  – is a server-side scripting language, like ASP
  – scripts are executed on the server
  – supports many databases (MySQL, Informix,
    Oracle, Sybase, Solid, PostgreSQL, Generic ODBC,
  – is an open source software
  – is free to download and use
               How PHP Works?
• PHP sits between your browser and the web server.
1. When you type in the URL of a PHP website in your
   browser, your browser sends out a request to the web
2. The web server then calls the PHP script on that page.
3. The PHP module executes the script, which then
   sends out the result in the form of HTML back to your
   browser, which you see on the screen.


                 PHP – get started                   <html>
• Open up notepad.                                   <body>

• Write in:                                          <?php
                                                     echo "Hello World";
• Save the file in www folder, i.e:                  ?>
• The file must have a .php extension. If the file has a .html
  extension, the PHP code will not be executed.
• Each code line in PHP must end with a semicolon.
• There are two basic statements to output text with
  PHP: echo and print. In the example above we have
  used the echo statement to output the text "Hello
                   PHP Syntax
• A PHP file normally contains HTML tags, just like
  an HTML file, and some PHP scripting code
• A PHP scripting block always starts with <?php
  and ends with ?>.        <?php

• A PHP scripting block can be placed anywhere in
  the document.
• Each code line in PHP must end with a semicolon.
• The semicolon is a separator and is used to
  distinguish one set of instructions from another.
            Comments in PHP
• // to make a single-line comment or
• /* and */ to make a large comment block.

               //This is a comment

               This is
               a comment

                PHP Variables
• Variables are used for storing values, like text
  strings, numbers or arrays.
• When a variable is declared, it can be used
  over and over again in your script.
• All variables in PHP start with a $ sign symbol.
• The correct way of declaring a variable in PHP:
             $var_name = value;

• Example:      <?php
                $txt="Hello World!";
        Naming Rules for Variables
• A variable name must start with a letter or an
  underscore "_"
• A variable name can only contain alpha-numeric
  characters and underscores (a-z, A-Z, 0-9, and _ )
• A variable name should not contain spaces. If a
  variable name is more than one word, it should
  be separated with an underscore ($my_string), or
  with capitalization ($myString)
• PHP variables are case sensitive, that means they
  could be either lower case or upper case or
  combination of both.
• It's good practice to initialize a variable i.e. assign
  it a value. Uninitialized variables in PHP have a
  value of either false, empty string or empty array.
           Declaring a PHP Variable
• First, think of a logical name for a variable..
• Assign that variable a value.
• Remember to put the $ sign in front of the
  variable name.

1. PHP String Variable
 $logo_text = "PHP-Learn-It! Learn PHP by
 echo $logo_text; ?>
  Declaring a PHP Variable (Continue)
2. PHP Integer Variable
    $number = 100;
    echo $number;

3. PHP Float or Decimal Variable
    $decimal_number = 100.01;
    printf("%.2f", $decimal_number);
            PHP String Variables
• String variables are used for values that
  contain characters.
• A string variable is used to store and
  manipulate text.
• A string can be used directly in a function or it
  can be stored in a variable.
• Below, the PHP script assigns the text "Hello
  World" to a string variable called $txt:
          $txt="Hello World";
          echo $txt;
          PHP Strings Operators
• Concatenating Strings in PHP
  – In PHP, you can use '.' to concatenate two or more
    strings together to form a single string
• Example 1 - Concatenating PHP Strings
                 $str1 = "I Love PHP.";
                 $str2 = "PHP is fun to learn.";
                 echo $str1." ".$str2;

• Example 2 - Concatenating PHP Strings
                 $str1 = "I Love PHP.";
                 $str2 = $str1." PHP is fun to learn.";
                 echo $str2;

To top