Your SlideShare is downloading. ×
P H P  Part  I,  By  Kian
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

P H P Part I, By Kian

3,457
views

Published on


1 Comment
6 Likes
Statistics
Notes
  • sir plz send this ppt to email id nnn96325@gmail.com...........it's urgent plzzzzzzzzzzzzz
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
3,457
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
1
Likes
6
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Inscribed By: Kian  PHP & MySQL & Dreamweaver
  • 2. PART I
    • An Introductory to PHP’s world !
  • 3. Needed Installers
    • PHP Installer (phpdev423)
    • MySQL-front
    • Apache Server
    • PHP Editor (Crimson)
    • Macromedia Dreamweaver (V.8)
  • 4. PHP
    • PHP was first created by Rasmus Lerdorf as a Personal Home Page tool in 1994.
    • Later recursively renamed to P HP H ypertext P reprocessor
    • PHP 1.0, June 1995
    • PHP 2.0, April 1996
    • PHP 3.0, June 1998
    • PHP 4.3.4 (Dec 2003)
    • Current version of PHP is 5.1
  • 5. Important PHP Features
    • Designed from beginning as a server-side web scripting language.
    • Runs on all major operating systems.
    • Built-in support for several relational databases such as MySQL
    • Built-in support for session management
  • 6. Important PHP Features
    • Interpreted language
    • Loosely typed language
      • a variable can hold a value of any type at different stages in its lifetime
    • Hybrid language
      • can be used in a non object-oriented style or an object-oriented style or a mixture of both.
    • Parts of PHP are similar to Perl
  • 7. PHP-Apache Integration PHP Module PHP file Apache Web Server Web Browser Send page Request page HTML doc
  • 8.
    • PHP code can be standalone (like Perl) or embedded in HTML code (like ASP)
    • PHP script tags are used to distinguish PHP code from HTML code:
      • <?php
      • PHP code goes here
      • ?>
    • These tags are XML compliant and should always be used for fully portable PHP code
    Where does PHP code go? Start PHP mode End PHP mode
  • 9. Alternate Tags
    • If short tags is enabled in the php.ini file you can use
      • <? PHP code goes here ?>
    • If you want to print the result of executing the PHP code you can use an = sign as in
      • <?= PHP code goes here ?>
    • Example (both are equivalent)
      • <? echo 'Hello World' ?>
      • <?= 'Hello World' ?>
  • 10. Variable Types
    • There are 8 data types in PHP
    • 4 are scalar types:
      • integer, floating point (double), string, and boolean
    • 2 are structured types:
      • array and object
    • 2 are special types:
      • resource and the NULL type
  • 11. Declaring Variables
    • Variable store a value
    • Variable types are not declared
    • All variables begin with a dollar sign ( $ )
      • first character is a letter or underscore (_)
      • remaining are letters, underscore or digits
      • remember that all Statements in PHP end with ;
    $count = 0; $name = “David&quot; ; $price = 45.50; $big_num = 1.2345E23; $success = TRUE;
  • 12. Notes About Variables
    • The value of a variable is the value of its most recent assignment.
    • Variables are assigned with a ‘=‘ operator.
    • The order of assigning is from right to left.
    • Variables do not need to be declared before assignment.
    • Variables have no intrinsic type other than the type of their current values.
    • Variable that are used before they are assigned have default values.
  • 13. More On Variables
    • Declaring Variables:
    • Because in PHP types are associated with values rather than variables, no such declaration is needed. The first step to use a variable is to assign it a value.
    • Assigning Variables:
    • $pi = 3 + 0.14159 // approximately
    • Reassigning Variables:
    • $my_num_var = “This should be a number – hope it’s reassigned! ”;
    • $my_num_var = 5;
  • 14. Naming Conventions
    • The convention in PHP (coming from C) is that the underscore character is used to simulate a space in variable names:
    • Example:
      • $number_of_files = 3;
    • This is different from the uppercase convention that is used in Java
      • numberOfFiles = 3;
  • 15. Constants
    • Constants are defined using the define function and do not begin with a dollar sign
    • The convention is to use all uppercase letters for names of constants
    • Examples:
      • define ( “COURSE ” , &quot;Internet Tools&quot; );
      • define ( ‘ CM_TO_INCH’ , 2.54);
    • Now use COURSE and CM_TO_INCH to refer to these constants
  • 16. Examples
    • <?php
    • define (&quot;CONSTANT&quot;, &quot;Hello world.&quot;); echo CONSTANT; // outputs &quot;Hello world.&quot; echo Constant; // outputs &quot;Constant&quot; and issues a notice. define (&quot;GREETING&quot;, &quot;Hello you.&quot;, true); echo GREETING; // outputs &quot;Hello you.&quot; echo Greeting; // outputs &quot;Hello you.&quot; ?>
  • 17. Three kinds of comments
    • C, Java style multi-line comments
      • /* This is a multi-line
      • comment
      • */
    • C++, Java single line comments
      • // a single line comment
    • Unix shell script and Perl comments
      • # a single line comment
  • 18. Literals & Strings
    • Integer literals
      • -1 , 1 , 2 , 3
    • Floating point literals
      • 1.234 , -4.56 , 1.34E-12
    • There are two kinds of string literal
      • single quotes: 'hello'
      • double quotes: &quot;hello&quot;
    • There are two boolean literals
      • TRUE , FALSE
  • 19. Variable Interpolation
    • Interpolation takes place inside doubly quoted strings.
    • This means that variables are replaced by their values and special control sequences such as take effect.
    • Interpolation does not take place inside singly quoted strings so is just two characters
    • This is similar to interpolation in Perl
  • 20. Variable Interpolation
    • If $name has the value “Rizwan&quot; then
    • &quot;Hello $name&quot; has the value Hello Rizwan
    • 'Hello $name' has the value Hello $name
    • &quot;$name = $name&quot; has the value $name = Rizwan
    • In the last case, the escape character is used to specify that the first $ is a literal character.
  • 21. Displaying Strings
    • The print function displays text on the standard output.
      • print ( &quot;Hello $name &quot; );
    • The echo function is similar but parentheses are not needed and several arguments can be supplied
      • echo &quot;Hello &quot; , $name , &quot; &quot; ;
    • There is also a C-style printf function that produces formatted output.
  • 22. Notes
    • Line break in HTML is: <BR> and not (“ ”)
    • Echo & Print:
    • echo “This will print in the user’s browser window. ”;
    • echo (“This will print the same script! ”);
    • echo “This will print” , ”the same too! ”;
    • echo (“But this will produce” , “PARSE ERROR! ”);
    • Echo & Print differ in two important differences:
      • Unlike ‘echo’ , ‘print’ can accept only one argument.
      • Unlike ‘echo’ , ‘print’ returns a value, which represent whether the print statement succeeded.
    • print(“3.14159”); //will print a string
    • print(3.14159); //will print a number
  • 23. Examples
    • Strings:
      • $string_1 = “This is a string in double quotes! ”;
      • $string_2 = ‘This is a string in single quotes! ’;
      • $string_39 = “This string has thirty-nine characters.”;
      • $string_0 = “ ”; //A string with 0 characters
      • $literally = ‘My $variable will not print! ’;
      • print($ literally); //Output: My $variable will not print!
      • $singly_quoted = ‘This quote mark:’ ‘is no big deal’;
      • $singly_quoted = ‘This quote mark ‘s no big deal either’;
  • 24. Arithmetical Operators
    • The usual operators are + , - , * , and /
    • Note that / is always a floating point divide
    • For integer divide use (int)( $a / $b )
    • The remainder (mod) operator is % so if $a and $b have integer values then $a % $b is the remainder when $a is divided by $b .
    • The ++ and -- operators are available
    • String Concatenation .
  • 25. Relational Operators (Logical Operators)
    • PHP has the usual C and Java style operators
      • || logical or
      • && logical and
      • ! negation
    • There are also different precedence versions
      • Or logical or
      • and logical and
      • xor logical exclusive XOR
        • ( Will evaluate its two operands and return TRUE only if either one of the operands itself returns true but not both. )
  • 26. Comparison Operators
    • PHP has the usual C and Java style operators
      • < , <= , == , != , > , >=
    • These operators perform type conversions if necessary before the comparison
    • There are also the operators
      • === , and !==
    • These operators compare value and type so no type conversion is done
    • These operators are also used for strings
  • 27. Comparison of == and ===
    • Suppose $a has the value &quot;0&quot; as a string
    • Suppose $b has the value 0 as an integer
    • Then
      • $a == $b is true since both have different data types
      • $a === $b is false since $a and $b have different types
  • 28. Assignment Operators $a = $a % $b $a %= $b %= $a = $a / $b $a /= $b /= $a = $a * $b $a *= $b *= $a = $a - $b $a -= $b -= $a = $a . $b $a .= $b .= (for strings) $a = $a + $b $a += $b += (for numbers) $a = $b $a = $b = Equivalent Example Operator
  • 29. Expression & Associativity & Evaluation Order
    • $result = 2 * 2 + 3 * 3 + 5 //Is equal to 18
    • $result1 = 2 + 3 * 4 + 5 // Is equal to 19
    • $result2 = (2 + 3) * (4 + 5) // Is equal to 45
    • $result3 = 2 + 2 * “Non-sense” + True;
        • //The output would be 3
        • A mixture of Associativity+BooleanValues+String
        • Booleans are counted as 0 or 1
        • String is always counted as 0
  • 30. Functions For Type Testing
    • gettype(arg) : Returns a staring representing the type of arg (either integer or double or …).
    • is_int(arg) : Returns a true value if arg is an integer, and false if not
    • is_integer(arg) :
    • is_long(arg) : Returns a true value if arg is a long, and false if not
    • is_double(arg) : Returns a true value if arg is a double, and false if not
    • is_float(arg) : Returns a true value if arg is a float, and false if not
    • is_real(arg) : Returns a true value if arg is a real, and false if not
  • 31. Functions For Type Testing
    • is_string(arg) : Returns a true value if arg is a string, and false if not
    • is_array(arg) : Returns a true value if arg is an array, and false if not
    • is_object(arg) : Returns a true value if arg is an object, and false if not
    • is_bool(arg) : Returns a true value if arg is a boolean value (True & False), and false if not
  • 32. Example
    • <?php
    • $this->myid = 2;
    • if (is_string($this->myid))
    • print &quot;true&quot;;
    • else
    • print &quot;false&quot;;
    • ?>
  • 33. Conditional Statements
    • Conditional allow us to perform different statements depending on certain conditions.
    • if
    • if else
    • elseif
    • else
    if (boolean expression 1) { ... } elseif (boolean expression 2) { ... } else { ... }
  • 34.
    • <? php //Note the space; will generate error
    • $a=2;
    • $b=1;
    • if ($a==$b)
    • { echo ‘value in $a is equal to the value in $b’; }
    • elseif ($a>$b)
    • { echo ‘value in $a is greater than value in $b’; }
    • else { echo ‘value in $a is NOT greater than OR equal to the value in $b’; }
    • ?>
  • 35. For Loop
    • The for loop is probably the most frequently used type of loop in PHP scripting and has his syntax:
    • for (initializer, test, increment) {statement/s}
    <?php $a = 0; $b = 0; for ($i=0; $i < 5; $i++;) { $a += 10; $b += 5; } echo (“At the end of the loop a=$a and b=$b !”); ?>
  • 36. While
    • While is a function that allow us to perform actions WHILE a condition is true.
    <?php $i=0; $num=50; while ($i < 10) { $sum--; $i++; } echo (“Loop stopped at $i</br> $num is now $num !”); ?>
  • 37. Switch
    • The switch statement works in an unusual way. First it evaluates a given expression then seeks a lable to match the resulting value.
    <?php $num = 2; switch ($num) { case 1: echo (“This is case 1 code!”); break ; case 2: echo (“This is case 2 code!”); break ; case 3: echo (“This is case 3 code!”); break ; default : echo (“This is default code!”); } ?>
  • 38.
    • There are many math constants in PHP. For example is M_PI and is M_SQRT2
    Math Functions <?php $radius = 1.0; $circ = 2.0 * M_PI * $radius ; $area = M_PI * $radius * $radius ; echo &quot;Circumference is $circ &quot; ; echo &quot;Area is $area &quot; ; ?>
  • 39.
    • There are many math functions in PHP:
      • abs sqrt
      • sin cos tan
      • asin acos atan atan2
      • exp expm1 log logp1
      • rand round
      • min max
      • pow
    • and many more
    Math Functions
  • 40. String Operations
    • The dot operator is used to concatenate strings
      • &quot;Hello &quot; . &quot;World&quot; is the same as &quot;Hello World&quot;
    • echo , print , printf , print_r , functions display strings on standard output
    • The length of a string $s is strlen ( $s )
  • 41. String Operations
    • Converting $s to upper case or lower case
      • $upper = strtoupper ( $s );
      • $lower = strtolower ( $s );
    • Converting first character of $s to uppercase
      • $upper_first = ucfirst ( $s );
    • Converting first character of every word in $s to uppercase
      • $upper_word = ucwords ( $s );
  • 42. String Comparison
    • can use the operators
      • < , <= , > , >= , == , != , === , !==
      • Note: Don't use == and != , use === and !==
    • Unexpected results for mixed comparisons of numbers and strings
    <?php $str1=‘99’;$str2=99; if ( $str1 === $str2 ) { echo &quot;Strings are equal ! &quot;; } else echo “Strings are not equal !”; ?>
  • 43. Substrings & Replacement
    • Substr (string, start)
      • return substring of string beginning at index start and continuing to end of the string
    • Substr (string, start, length)
      • return substring of string beginning at index start and consisting of length characters
    • There are also functions strrev, substr_replace
  • 44. Examples of substr_replace
    • <?php $var = 'ABCDEFGH:/MNRPQR/'; echo &quot;Original: $var<hr /> &quot;; echo &quot;The length of the original is &quot;. strlen($var);
    • /* These two examples replace all of $var with 'bob'. */ echo substr_replace($var, 'bob', 0) . &quot;<br /> &quot;; echo substr_replace($var, 'bob', 0, strlen($var)) . &quot;<br /> &quot;; /* Insert 'bob' right at the beginning of $var. */ echo substr_replace($var, 'bob', 0, 0) . &quot;<br /> &quot;; /* These next two replace 'MNRPQR' in $var with 'bob'. */ echo substr_replace($var, 'bob', 10, -1) . &quot;<br /> &quot;; echo substr_replace($var, 'bob', -7, -1) . &quot;<br /> &quot;; /* Delete 'MNRPQR' from $var. */ echo substr_replace($var, '', 10, -1) . &quot;<br /> &quot;; ?>
  • 45. Exploding a String
    • explode (separator, string)
      • extracts an array of substrings using the characters in separator as field separators
    • Example
    <?php $data = &quot;foo:*:1023:1000::/home/foo:/bin/sh&quot;; list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(&quot;:&quot;, $data); echo $user; // foo echo $pass; // * ?>
  • 46. Imploding a String
    • implode (separator, array)
      • inverse of explode: makes a single string from the strings in an array using separator string between each string in the array.
      • Example
    <?php $array = array('lastname', 'email', 'phone'); $comma_separated = implode(&quot;,&quot;, $array); echo $comma_separated; // lastname,email,phone ?>
  • 47. Two types of arrays
    • Indexed arrays:
      • the indices are 0, 1, 2, ....
      • these are like arrays in C, Java, or Perl
    • Associative arrays:
      • indices are strings (keys)
      • these are similar to hashes in Perl
    • Any values can be stored in an array
    An array is a data structure that stores one or more values in a single value.
  • 48. Indexed Arrays
    • The general form for setting the key of an array equal to a value is: $array[ key ] = value ;
    • $employee_array[0] = &quot;Bob&quot;;
    • $employee_array[1] = &quot;Sally&quot;;
    • $employee_array[2] = &quot;Charlie&quot;;
    • $employee_array[3] = &quot;Clare&quot;;
    • In the above example we made use of the key / value structure of an array. The keys were the numbers we specified in the array and the values were the names of the employees. Each key of an array represents a value that we can manipulate and reference.
    • echo &quot;Two of my employees are $employee_array[0] and $employee_array[1]”;
  • 49. Indexed Arrays
    • Ranges:
    • $digits = range (0,9);
      • $digits [0] is 0,
      • $digits [1] is 1, ...
    • $letters = range ( 'a' , 'z' );
      • $letters [0] is 'a' ,
      • $letters [1] is 'b' , ...
  • 50. Examples
    • <?php // array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) foreach (range(0, 12) as $number) {    echo $number .&quot; &quot;; } // The step parameter was introduced in 5.0.0 // array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) foreach (range(0, 100, 10) as $number) {    echo $number .&quot; &quot;; } // Use of character sequences introduced in 4.1.0 // array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'); foreach (range('a', 'i') as $letter) {    echo $letter .&quot; &quot;; }
    • // array('c', 'b', 'a'); foreach (range('c', 'a') as $letter) {    echo $letter .&quot; &quot;; } ?>
  • 51. Associative Arrays
    • In an associative array a key is associated with a value.
    • array can be used to initialize arrays with string indices. We use operator => . The value left of the operator is the array index and the value to the right is the element’s value.
    $product  = array( 'id'  =>   123 ,  'desc'  =>   null ,  'price'  =>  549 .99 );
  • 52. Associative Arrays
    • An associative array is a table of key-value pairs. Here the names are the keys and the values are the ages.
    $age = array( 'Fred' => 37, 'Gord' => 23, 'Alice' => 17, 'Bob' => 23 ); echo &quot;Fred's age is &quot; , $age ['Fred']; $age [ 'Fred' ] = 65; // Fred is now a senior echo &quot;<br>Fred's age is {$age['Fred']}&quot; ; Note braces needed for interpolation
  • 53. Sorting Arrays
    • sort(array) & rsort(array)
      • sort array in ascending alphabetical or numerical order or reverse alphabetical or numerical order
    <?php $names = array ( 'Fred' , 'Ted' , 'Barney' , 'Gord' ); sort ($names); // 'Barney','Fred','Gord','Ted' rsort ($names); // 'Ted','Gord','Fred','Barney‘; ?>
  • 54. Example
    • <?php
    • $names = array('Fred', 'Ted', 'Barney', 'Gord');
    • sort($names); //or rsort($names);
    • foreach($names as $elements=>$names)
    • echo &quot; &quot;.$names;
    • ?>
    • *Also, try to change the $elements to $names, you will get the number of elements in the array.
  • 55. User-defined Functions
    • User defined functions have the form
    function name ($ argument, $ argument ) { statement1; statement2; }
  • 56. A max Function
    • Return the max of two numeric values
    function max2 ( $a , $b ) { $a=3; $b=1; if ( $a > $b ) return $a; return $b; } echo “Max of 2 and 3 is &quot; , max2 ( );
  • 57. Reserved Words in PHP for E_ALL elseif class FALSE endwhile else cfunction extends endswitch echo( ) case exit( ) endif die( ) break eval endforeach do $argc E_WARNING endfor default as E_ERROR enddeclare declare $argv E_PARSE empty( ) continue and
  • 58. Reserved Words in PHP print( ) not $HTTP_ENV_VARS PHP_VERSION new $HTTP_POST_FILES $PHP_SELF list( ) $HTTP_POST_VARS PHP_OS include_once( ) $HTTP_GET_VARS parent include( ) $HTTP_COOKIE_VARS or if global old_function $HTTP_SERVER_VARS function NULL $HTTP_COOKIE_VARS foreach
  • 59. Reserved Words in PHP _wakeup TRUE _sleep $this _LINE_ stdclass _FILE_ switch while static virtual( ) return( ) xor require_once( ) var require( )
  • 60.
    • THE END
    • SEE YOU NEXT SESSION! 