Php Crash Course
Upcoming SlideShare
Loading in...5
×
 

Php Crash Course

on

  • 9,492 views

 

Statistics

Views

Total Views
9,492
Views on SlideShare
9,127
Embed Views
365

Actions

Likes
0
Downloads
97
Comments
1

4 Embeds 365

http://46h-wiki.wikispaces.com 177
http://46h-wiki.wikispaces.com 177
https://46h-wiki.wikispaces.com 8
http://www.slideshare.net 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • IT Pathshala, Noida-Sector-63, offers Guaranteed JOB with PHP Training Programs.

    PAY US ONLY IF YOU GET A JOB!

    CALL US - +91-9871350999 or +91-8800394970

    Technology Centre Address: H-110, Sector-63, Noida (UP)

    Program Takeaways:

    - GUARANTEED JOB in your technology domain
    - Live Major Project development
    - Experience Letter on Training Completion
    - Certification on Training Completion
    - Project Source Code available in CD

    Program Highlights:

    - 100% Guaranteed Job – Don’t pay us if you don’t get a JOB!
    - Industry Ready Courses
    - LIVE International Project Experience
    - Exposure to corporate culture at our IT services facility
    - Networking & Interaction with Industry Experts
    - Training Provided by a reputed IT Company
    - Live Project Source Code made available in CD
    - Technology certification and experience letter at program completion
    - Online Study Content

    CALL US - +91-9871350999 or +91-8800394970

    Our website: www.itpathshala.com
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Php Crash Course Php Crash Course Presentation Transcript

    • PHP (HYPERTEXT PREPROCESSOR)
    • Why PHP
      • Uses in server-side scripting. It is platform independent
      • Advantages of using PHP are:
        • Easy to learn, use, and implement
        • Free for access
        • Executes on any platform
      • Executes scripts from the command line and develops client-side GUI applications that can operate on any platform
      • Uses of PHP are application control, database access, file access, and graphics
    • Start Me Up
      • These embedded PHP commands are enclosed within special start and end tags.
      • <?php ... PHP code ... ?>
    • PHP and HTML can be combined
      • <html> <head></head> <body> Agent: So who do you think you are, anyhow? <br /> <?php // print output echo 'Neo: I am Neo, but my people call me The One.' ; ?> </body> </html>
    • Output
    • Comment in PHP code
      • PHP supports both single-line and multi-line comment blocks:
      • Example:
      • <?php // this is a single-line comment /* and this is a multi-line comment */ ?>
    • A Case of Identity
      • A variable used to store both numeric and non-numeric data, it can be altered, and can be compared with each other.
      • PHP supports a number of different variable types: integers, floating point numbers, strings and arrays.
      • Every variable has a name. In PHP, a variable name is preceded by a dollar ($)
      • Example
      • valid variable $popeye, $one and $INCOME
      • invalid $123 and $48hrs
      • Variable names in PHP are case sensitive, $me is different from $Me or $ME.
    • example using PHP's variables
      • <html> <head></head> <body> Agent: So who do you think you are, anyhow? <br /> <?php // define variables $name = 'Neo' ; $rank = 'Anomaly' ; $serialNumber = 1 ; // print output echo &quot;Neo: I am <b>$name</b>, the <b>$rank</b>. You can call me by my serial number, <b>$serialNumber</b>.&quot; ; ?> </body> </html>
    • Output
      • To assign a value to a variable, We use the assignment operator: the = symbol. This is used to assign a value (the right side of the equation) to a variable (the left side). The value being assigned need not always be fixed; it could also be another variable, an expression, or even an expression involving other variables, as below:
      • <?php $age = $dob + 15; ?>
      • We can also perform more than one assignment at a time. Consider the following example, which assigns three variables the same value simultaneously:
      • <?php $angle1 = $angle2 = $angle3 = 60; ?>
      • Every language has different types of variable. The language supports a wide variety of data types, including simple numeric, character, string and Boolean types, and more complex arrays and objects.
      • 1.Boolean : The simplest variable type in PHP, a Boolean variable, simply specifies a true or false value.
      • <?php $auth = true; ?>
      • 2. Integer : Store number without decimal like 75, -95, 2000 or 1.
      • <?php $age = 99; ?>
      Variable-data types
      • 3.Floating-point : A floating-point number is typically a fractional number such as 12.5 or 3.141592653589. Floating point numbers may be specified using either decimal or scientific notation.
      • <?php $temperature = 56.89; ?>
      • 4. String : A string is a sequence of characters, like &quot;hello&quot; or “world&quot;.
      • String values may be enclosed in either double quotes (&quot;&quot;) or single quotes('').
      • (Quotation marks within the string itself can be &quot;escaped&quot; with a backslash () character.)
      • String values enclosed in double quotes are automatically parsed for special characters and variable names; if these are found, they are replaced with the appropriate value.
    • example:
      • <?php $identity = 'James Bond' ; $car = 'BMW' ; // this would contain the string &quot;James Bond drives a BMW&quot; $sentence = &quot;$identity drives a $car&quot; ; echo $sentence ; ?>
      • //output
      • James Bond drives a BMW
    • Market Value
      • PHP also comes with operators for arithmetic, string, comparison and logical operations.
      • A good way to get familiar with operators is to use them to perform arithmetic operations on variables
      • Below is the example.
      • <html> <head> </head> <body> <?php // set quantity $quantity = 1000 ; // set original and current unit price $origPrice = 100 ; $currPrice = 25 ; // calculate difference in price $diffPrice = $currPrice - $origPrice ; // calculate percentage change in price $diffPricePercent = (( $currPrice - $origPrice ) * 100 )/ $origPrice ?>
      • <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot;> <tr> <td>Quantity</td> <td>Cost price</td> <td>Current price</td> <td>Absolute change in price</td> <td>Percent change in price</td> </tr> <tr> <td> <?php echo $quantity ?> </td> <td> <?php echo $origPrice ?> </td> <td> <?php echo $currPrice ?> </td> <td> <?php echo $diffPrice ?> </td> <td> <?php echo $diffPricePercent ?> %</td> </tr> </table> </body> </html>
    • Output -75% -75 25 100 1000 Percent change in price Absolute change in price Current price Cost price Quantity
      • Here is calculations using PHP's various mathematical operators, and stored the results of those calculations in different variables. The rest of the script is related to the display of the resulting calculations in a neat table.
      • We can even perform an arithmetic operation by using the two operators together.
      mathematical operators
    • Example
      • <?php // this... $a = 5 ; $a = $a + 10 ; // ... is the same as this $a = 5 ; $a += 10 ; ?>
    • Stringing Things Along
      • PHP also allows us to add strings with the string concatenation operator, represented by a period (.).
    • Example
      • <?php // set up some string variables $a = 'the' ; $b = 'games' ; $c = 'begin' ; $d = 'now' ; // combine them using the concatenation operator // this returns 'the games begin now<br />' $statement = $a . ' ' . $b . ' ' . $c . ' ' . $d . '<br />' ; print $statement ; // and this returns 'begin the games now!' $command = $c . ' ' . $a . ' ' . $b . ' ' . $d . '!' ; print $command ; ?>
      • //Output
      • the games begin now begin the games now!
    • example
      • <?php // define string $str = 'the' ; // add and assign $str .= 'n' ; // str now contains &quot;then&quot; echo $str ; ?>
      • //Output
      • then
    • Calling function
      • A function is a code that executes when you call it from your program without leaving current program.
      • Example: Calling printf fuction
      • <?php
      • $name = 'Musavir' ;
      • printf( &quot;welcome Mr. %s&quot; ,$name);
      • ?>
    • FORM
      • A form provides a medium of interface for the client and the server to interact with each other.
      • A form allow the user to actually input raw data into the application.
      • A form consists of the following attributes:
        • Action- specifies the Uniform Resource Locator(URL) that will process the form data and send the feedback.
        • Method- specifies the way the information is to be sent to the URL
      User (web browser) Web Server Enter data and the data are sent to Php script engine Passes data Process the data Give response or output
      • There are two common methods for passing data from one script to another: GET and POST.
      • The GET method specifies the web browser to send all the user information as part of the URL.
      • In this method, a ? is added at the end of the URL. This ? Indicates the ending of the URL and the beginning of the form information.
      • The POST method specifies the web browser that all the user information is sent through the body of the HTTP request.
      • This example contains two scripts, one containing an HTML form (named form.htm) and the other containing the form processing logic (message.php).
      • Here's form.htm:
      <html> <head> </head> <body> <form action=&quot;message.php&quot; method=&quot;post&quot;> Enter your message: <input type=&quot;text&quot; name=&quot;msg&quot; size=&quot;30&quot;> <input type=&quot;submit&quot; value=&quot;Send&quot;> </form> </body> </html>
      • Here the message.php:
      <html> <head></head> <body> <?php // retrieve form data $input = $_POST [ 'msg' ]; // use it echo &quot;You said: <i>$input</i>&quot; ; ?> </body> </html>
    • operator
      • An operator is a symbol that specifies a particular action in an expression.
    •  
    • Comparison operator <?php /* define some variables */ $mean = 9 ; $median = 10 ; $mode = 9 ; // less-than operator // returns true if left side is less than right // returns true here $result = ( $mean < $median ); print &quot;result is $result<br />&quot; ;
    • // greater-than operator // returns true if left side is greater than right // returns false here $result = ( $mean > $median ); print &quot;result is $result<br />&quot; ; // less-than-or-equal-to operator // returns true if left side is less than or equal to right // returns false here $result = ( $median <= $mode ); print &quot;result is $result<br />&quot; ;
    • // greater-than-or-equal-to operator // returns true if left side is greater than or equal to right // returns true here $result = ( $median >= $mode ); print &quot;result is $result<br />&quot; ; // equality operator // returns true if left side is equal to right // returns true here $result = ( $mean == $mode ); print &quot;result is $result<br />&quot; ;
      • The result of a comparison test is always Boolean: either true (1) or false (0 does not print anything)
      // not-equal-to operator // returns true if left side is not equal to right // returns false here $result = ( $mean != $mode ); print &quot;result is $result<br />&quot; ; // inequality operator // returns true if left side is not equal to right // returns false here $result = ( $mean <> $mode ); print &quot;result is $result&quot; ; ?>
    • === operator <?php /* define two variables */ $str = '10' ; $int = 10 ; /* returns true, since both variables contain the same value */ $result = ( $str == $int ); print &quot;result is $result<br />&quot; ; /* returns false, since the variables are not of the same type even though they have the same value */ $result = ( $str === $int ); print &quot;result is $result<br />&quot; ; /* returns true, since the variables are the same type and value */ $anotherInt = 10 ; $result = ( $anotherInt === $int ); print &quot;result is $result&quot; ; ?>
    • Logical operator
      • Four logical operator: AND, OR, XOR, NOT
      <?php /* define some variables */ $auth = 1 ; $status = 1 ; $role = 4 ; /* logical AND returns true if all conditions are true */ // returns true $result = (( $auth == 1 ) && ( $status != 0 )); print &quot;result is $result<br />&quot; ;
    • /* logical OR returns true if any condition is true */ // returns true $result = (( $status == 1 ) || ( $role <= 2 )); print &quot;result is $result<br />&quot; ; /* logical NOT returns true if the condition is false and vice-versa */ // returns false $result = !( $status == 1 ); print &quot;result is $result<br />&quot; ; /* logical XOR returns true if either of two conditions are true, or returns false if both conditions are true */ // returns false $result = (( $status == 1 ) xor ( $auth == 1 )); print &quot;result is $result<br />&quot; ; ?>
    • Conditional statement
      • a conditional statement allows you to test whether a specific condition is true or false, and perform different actions on the basis of the result.
      • if
      • The if conditional is one of the most commonplace constructs of any mainstream programming language, offering a convenient means for conditional code execution. The syntax is:
      if ( expression ) { statement }
      • Example:
      <html> <head></head> <body> <form action=&quot;ageist.php&quot; method=&quot;post&quot;> Enter your age: <input name=&quot;age&quot; size=&quot;2&quot;> </form> </body> </html>
    • ageist.php <html> <head></head> <body> <?php // retrieve form data $age = $_POST [ 'age' ]; // check entered value and branch if ( $age >= 21 ) {      echo 'Come on in, we have alcohol and music awaiting you!' ; } if ( $age < 21 ) {      echo &quot;You're too young for this club, come back when you're a little older&quot; ; } ?> </body> </html>
      • if-else construct, used to define a block of code that gets executed when the conditional expression in the if() statement evaluates as false.
      • The if-else construct looks like this:
      if (condition) {     do this!     } else {     do this! }
      • Example:
      <html> <head></head> <body> <?php // retrieve form data $age = $_POST [ 'age' ]; // check entered value and branch if ( $age >= 21 ) {     echo 'Come on in, we have alcohol and music awaiting you!' ;     } else {     echo &quot;You're too young for this club, come back when you're a little older&quot; ; } ?> </body> </html>
    • Ternary operator
      • represented by a question mark (?). This operator, which lets you make your conditional statements almost unintelligible, provides shortcut syntax for creating a single-statement if-else block.
      <?php if ( $numTries > 10 ) {       $msg = 'Blocking your account...' ;     } else {      $msg = 'Welcome!' ; } ?>
      • You could also do this, which is equivalent
      <?php $msg = $numTries > 10 ? 'Blocking your account...' : 'Welcome!' ; ?>
    • Nested if
      • or
      <?php if ( $day == 'Thursday' ) {     if ( $time == '0800' ) {         if ( $country == 'UK' ) {              $meal = 'bacon and eggs' ;         }     } } ?> <?php if ( $day == 'Thursday' && $time == '0800' && $country == 'UK' ) {      $meal = 'bacon and eggs' ; } ?>
    • <html> <head></head> <body> <h2>Today's Special</h2> <p> <form method=&quot;get&quot; action=&quot;cooking.php&quot;> <select name=&quot;day&quot;> <option value=&quot;1&quot;>Monday/Wednesday <option value=&quot;2&quot;>Tuesday/Thursday <option value=&quot;3&quot;>Friday/Sunday <option value=&quot;4&quot;>Saturday </select> <input type=&quot;submit&quot; value=&quot;Send&quot;> </form> </body> </html>
    • <html> <head></head> <body> <?php // get form selection $day = $_GET [ 'day' ]; // check value and select appropriate item if ( $day == 1 ) {      $special = 'Chicken in oyster sauce' ;     } elseif ( $day == 2 ) {      $special = 'French onion soup' ;     } elseif ( $day == 3 ) {      $special = 'Pork chops with mashed potatoes and green salad' ;     } else {      $special = 'Fish and chips' ; } ?> <h2>Today's special is:</h2> <?php echo $special ; ?> </body> </html>
    • While() loop
      • While (condition is true)
      • {
      • do this!
      • }
      • Execution of php statements within curly braces
        • As long as the specified condition is true
      • Condition becomes false
        • The loop will be broken and the following statement will be executed.
    • Examples
      • <?php
      • $number = 5;
      • while ($number >= 2 )
      • {
      • echo $number. &quot;<br/>&quot; ;
      • $number - = 1;
      • }
      • ?>
    • Examples
      • Variable is initialized to 5
      • The while loop executes as long as the condition, ($number>=2) is true
      • At the end of the loop block, the value of $number is decreased by 1.
    • Output of example
      • The while() loop executes a set of statements while a specified condition is true. But what happens if the condition is true on the first iteration of the loop itself? If you're in a situation where you need to execute a set of statements *at least* once, PHP offers you the do-while() loop.
    • Do-while() statement
      • do {     do this! } while (condition is true)
    • While() vs do-while()
      • Let's take a quick example to better understand the difference between while() and do-while():
    • Examples
      • <?php $x = 100 ; // while loop while ( $x == 700 ) {     echo &quot;Running...&quot; ;     break; } ?>
      • In this case, no matter how many times you run this PHP script, you will get no output at all, since the value of $x is not equal to 700. But, if you ran this version of the script:
    • Example
      • <?php $x = 100 ; // do-while loop do {     echo &quot;Running...&quot; ;     break; } while ( $x == 700 ); ?>
      • you would see one line of output, as the code within the do() block would run once.
      • <html> <head></head> <body> <?php // set variables from form input $upperLimit = $_POST [ 'limit' ]; $lowerLimit = 1 ; // keep printing squares until lower limit = upper limit do {     echo ( $lowerLimit * $lowerLimit ). '&nbsp;' ;      $lowerLimit ++; } while ( $lowerLimit <= $upperLimit ); // print end marker echo ' END' ; ?> </body> </html>
      • Thus, the construction of the do-while() loop is such that the statements within the loop are executed first, and the condition to be tested is checked afterwards. This implies that the statements within the curly braces would be executed at least once.
    • For() loops
      • Both the while() and do-while() loops continue to iterate for as long as the specified conditional expression remains true. But what if you need to execute a certain set of statements a specific number of times - for example, printing a series of thirteen sequential numbers, or repeating a particular set of <td> cells five times? In such cases, clever programmers reach for the for() loop...
      • The for() loop typically looks like this:
      • for (initial value of counter; condition; new value of counter) {     do this! }
    • Examples
      • <html> <head> <basefont face=&quot;Arial&quot;> </head> <body> <?php // define the number $number = 13 ; // use a for loop to calculate tables for that number for ( $x = 1 ; $x <= 10 ; $x ++) {     echo &quot;$number x $x = &quot; .( $number * $x ). &quot;<br />&quot; ; } ?> </body> </html>
      • define the number to be used for the multiplication table.
      • constructed a for() loop with $x as the counter variable, initialized it to 1.
      • specified that the loop should run no more than 10 times.
      • The auto-increment operator automatically increments the counter by 1 every time the loop is executed. Within the loop, the counter is multiplied by the number,
      • to create the multiplication table, and echo() is used to display the result on the page.
    • Switch-case() statement
      • Switch (decision-variable)
      • {
      • case first condition is true:
      • do this!
      • case second condition is true:
      • do this!
      • .
      • .
      • .
      • … and so on …
      • }
      • Appropriate case() block execution
        • Depend on the value of decision variable
      • Default block
        • Decision variable not match any of the listed case() conditions
      • <html> <head></head> <body> <?php // get form selection $day = $_GET [ 'day' ]; // check value and select appropriate item switch ( $day ) {     case 1 :          $special = 'Chicken in oyster sauce' ;         break;     case 2 :          $special = 'French onion soup' ;         break;     case 3 :          $special = 'Pork chops with mashed potatoes and green salad' ;         break;     default:          $special = 'Fish and chips' ;         break; } ?> <h2>Today's special is:</h2> <?php echo $special ?> </body> </html>
    • Summary
      • The do...while statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true.
      • The do...while loop is similar in nature to the While loop. The key difference is that the do...while loop body is guaranteed to run at least once. This is possible because the condition statement is evaluated at the end of the loop statement after the loop body is performed.
    • Summary
      • The for statement loop is used when you know how many times you want to execute a statement or a list of statements. For this reason, the for loop is known as a definite loop.
    • Summary
      • In programming it is often necessary to repeat the same block of code a number of times. This can be accomplished using looping statements. PHP includes several types of looping statements.
      • The while statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true.
    • Summary
      • It is often desirable when writing code to perform different actions based on different decision.
      • In addition to the if Statements, PHP includes a fourth type of conditional statement called the switch statement. The switch Statement is very similar or an alternative to the if...else if...else commands. The switch statement will test a condition. The outcome of that test will decide which case to execute. Switch is normally used when you are finding an exact (equal) result instead of a greater or less than result. When testing a range of values, the if statement should be used.