Introduction to PHP
UNIT II
                              PHP
 PHP is a powerful tool for making dynamic and interactive
  Web pages.
 PHP stands for PHP: Hypertext Preprocessor
 It is server side scripting Language.
 It runs on apache server.
 PHP supports many databases (MySQL, Informix, Oracle,
  Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
 PHP is an open source software
 Contains a vast library of functionality that programmers
  can harness
 Executes entirely on the server, requiring no specific
  features from the client
 Static resources such as regular HTML are simply output to
  the client from the server
 Dynamic resources such as PHP scripts are processed on
  the server prior to being output to the client
       PHP - What is it / does it do?
         Web Page Request           Load PHP File
                                                    PHP Engine –
                                                     Run Script
            HTML Response
                       Web Server    PHP Results
User
                 Scripting Languages
 A scripting language, script language or extension
  language is a programming language that allows control of
  one or more applications.
 "Scripts" are distinct from the core code of the application,
  as they are usually written in a different language and are
  often created or at least modified by the end-user.
There are two types of scripting Languages:
1. Client Side Scripting Languages
2. Server Side Scripting Languages
     Client Side Vs Server Side Scripting Languages
Client Side Scripting          Server Side Scripting
1. Client Side scripting       1. Server Side Scripting
   are the programming            are the programming
   language which runs on         language which runs on
   client computer. It will       Web Servers to generate
   run by the Web Browser.        dynamic web pages.
   Any user in the world can      Nobody can view the
   view the full script by        programming code of
   simple going to page           server side scripting.
   source.                     2. Better Security and
2. Security and integrity is      integrity is there in case of
   not there in the Client        server side scripting
   side Scripting Languages.      Languages.
3. Client Side Scripting   3. Server Side Scripting
   Languages have less        languages have more
   functionality.             functionality .
4. Example: JavaScript ,   4. Example : PHP , ASP ,
   Vbscript etc.              Perl etc.
                                PHP
 PHP is similar to C
 All scripts start with <?php and with with ?>
 Line separator: ; (semi-colon)
 Code block: { //code here } (brace brackets)
 White space is generally ignored (not in strings)
 Comments are created using:
   // single line quote
   /* Multiple line block quote */
 Precedence
   Enforced using parentheses
   E.g. $sum = 5 + 3 * 6; // would equal 23
   $sum = (5 + 3) * 6; // would equal 48
             Small Program in PHP
<html>
<body>
<?php
echo “hello World”;
?>
</body>
</html>
                   Variables in PHP
 A variable is defined with a $ Sign in PHP.
 $var_name = value;
 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 - Example Script
 <?php
   $author = “Trevor Adams”;
   $msg = “Hello world!”;
   echo $author . “ says ” . $msg;
 ?>
                  Global and Local Scope
 A variable declared outside a function has a GLOBAL SCOPE and can
  only be accessed outside a function:
 Example
 Variable with global scope:
 <?php
  $x = 5; // global scope
  function myTest() {
    // using x inside this function will generate an error
    echo "<p>Variable x inside function is:,$x</p>";
  }
  myTest();
  echo "<p>Variable x outside function is: $x</p>";
  ?>
 A variable declared within a function has a LOCAL SCOPE and can
  only be accessed within that function:
 Example
 Variable with local scope:
 <?php
  function myTest() {
    $x = 5; // local scope
    echo "<p>Variable x inside function is: $x</p>";
  }
  myTest();
  // using x outside the function will generate an error
  echo "<p>Variable x outside function is: $x</p>";
  ?>
                PHP The global Keyword
 The global keyword is used to access a global variable from
  within a function.
Example
 <?php
  $x = 5;
  $y = 10;
  function myTest() {
    global $x, $y;
    $y=$x + $y;
  }
  myTest();
  echo $y; // outputs 15
  ?>
                          Cont..
 Example
 <?php
 $x = 5;
 $y = 10;
 function myTest() {
   $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
 }
 myTest();
 echo $y; // outputs 15
 ?>
                       PHP - Constants
 Constants are special variables that cannot be
  changed
 Use them for named items that will not change
 Created using a define function
    define(„milestone‟, 1.6);
    Used without $
    $km = 5 * milestone;
 Example
 Create a constant with a case-sensitive name:
 <?php
  define("GREETING", "Welcome to W3Schools.com!");
  echo GREETING;
  ?>
 Create a constant with a case-insensitive name:
 <?php
  define("GREETING", "Welcome to W3Schools.com!", true);
  echo greeting;
  ?>
              PHP Constant Arrays
 Create an Array constant:
 <?php
 define("cars", ["Alfa Romeo","BMW","Toyota"]);
 echo cars[0];
 ?>
               Datatypes in PHP
 PHP supports the following data types:
 String
 Integer
 Float (floating point numbers)
 Boolean
 Array
 Object
 NULL
                   PHP String
 Example
 <?php
 $x = "Hello world!";
 $y = 'Hello world!';
 echo $x;
 echo "<br>";
 echo $y;
 ?>
                     PHP Integer
 An integer data type is a non-decimal number between
 -2,147,483,648 and 2,147,483,647.
 <?php
 $x = 5985;
 var_dump($x);
 ?>
                     PHP Float
 In the following example $x is a float. The PHP
  var_dump() function returns the data type and
  value:
 Example
 <?php
  $x = 10.365;
  var_dump($x);
  ?>
                  PHP Boolean
 A Boolean represents two possible states: TRUE or
  FALSE.
 $x = true;
  $y = false;
                        PHP Array
 An array stores multiple values in one single variable.
 In the following example $cars is an array. The PHP
  var_dump() function returns the data type and value:
 Example
 <?php
  $cars = array("Volvo","BMW","Toyota");
  var_dump($cars);
  ?>
                                PHP Object
 An object is a data type which stores data and information on how to process
  that data.
 First we must declare a class of object. For this, we use the class keyword. A
  class is a structure that can contain properties and methods:
 Example
 <?php
  class Car {
    function Car() {
      $this->model = "VW";
    }
  }
    // create an object
    $herbie = new Car();
    // show object properties
    echo $herbie->model;
    ?>
                   PHP NULL Value
 Null is a special data type which can have only one value:
  NULL.
 A variable of data type NULL is a variable that has no value
  assigned to it.
 Example
 <?php
  $x = "Hello world!";
  $x = null;
  var_dump($x);
  ?>
 We can execute html tags in PHP by giving them in PHP
  tags.
<html>
<body>
<?PHP
$a=5;
$b=6;
$c=$a+$b;
Echo “<ul><li> First Number :”.$a.”</li>”;
Echo “<li> Second Number :”.$b.”</li>”;
Echo “<li> Sum of Two Number”.$c.”</li>”;
?>
                        QUIZ
 Ques: What does PHP stand for?
 i) Personal Home Page
 ii) Hypertext Preprocessor
 iii) Pretext Hypertext Processor
 iv) Preprocessor Home Page
 a) Both i) and iii)
  b) Both ii) and iv)
  c) Only ii)
  d) Both i) and ii)
                       QUIZ
 PHP files have a default file extension of_______
 a) .html
 b) .xml
 c) .php
 d) .ph
                       QUIZ
 What should be the correct syntax to write a PHP
 code?
 a) < php >
 b) < ? php ?>
 c) <? ?>
 d) <?php ?>
                         QUIZ
 Which of the following PHP statement/statements
  will store 111 in variable num?
 i) int $num = 111; ii) int mum = 111; iii) $num = 111;
  iv) 111 = $num;
 a) Both i) and ii)
  b) i), ii), iii) and iv)
  c) Only iii)
  d) Only i)
                       QUIZ
 What will be the output of the following PHP code?
 <?php
 $num = 1;
 $num1 = 2;
 print $num . "+". $num1;
 ?>
 a) 3
 b) 1+2
 c) 1.+.2
 d) Error
                       QUIZ
 Which of the below symbols is a newline character?
 A. \r
 B. \n
 C. /n
 D. /r
                         QUIZ
 Which of following variables can be assigned a value
    to it?(i) $3hello
   (ii) $_hello
   (iii) $this
   (iv) $This
   A. All of the mentioned
   B. Only (ii)
   C. (ii), (iii) and (iv)
   D. (ii) and (iv)
 What will be the output of the following code?
 < ?php
 $foo = 'Bob';
 $bar = &$foo;
 $bar = "My name is $bar";
 echo $bar; echo $foo; ?>
 A. Error
 B. My name is BobBob
 C. My name is BobMy name is Bob
 D. My name is Bob Bob
                        QUIZ
 If $a = 12 what will be returned when ($a == 12) ? 5 :
    1 is executed?
   A. 12
   B. 1
   C. Error
   D. 5
 What will be the output of the following PHP code?
 < ?php
 $color = "maroon";
 $var = $color[2];
 echo "$var" ;
 ?>
 A. a
 B. Error
 C. $var
 D. r
 What will be the output of the following PHP code?
 < ?php
 $total = "25 students";
 $more = 10;
 $total = $total + $more;
 echo "$total" ;
 ?>
 A. Error
 B. 35 students
 C. 35
 D. 25 students
                      Questions
 Write a simple script in PHP that declares an
  interger variable having value 45 and string variable
  having value today‟s temperature concate the both
  the values and display in red color and italics font
  style.
 Write a script in PHP that declares a three variable
  ,two variables stores string values 1) $467 and 2)
  price‟s of product and third variable stores integer
  value. Each variable value is printed in ordered list of
  roman numbers.
 Write a script in PHP which declares float value and
  integer value and concate the two values and show
  them in line with a tab space between them and also
  each value in separate line.
 Write a script in PHP that declares a five names of
  your friend and display them in table as follows with
  a border 5 and color of border red and background
  color of table green and text color white
                         Friends Database
           First Name            value
           Second Name           value