Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
714
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
16
Comments
0
Likes
0

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. PHP
    Loops - Statements that enable you to achieve repetitive tasks.
  • 2. The while Statement
    The while statement looks similar in structure to a basic if statement:
    while ( expression ) {
    // do something
    }
    As long as a while statement's expression evaluates to true, the code block is executed repeatedly. Each execution of the code block in a loop is often called an iteration.
  • 3. Sample 1
    <?php
    $counter = 1;
    while ( $counter <= 12 ) {
    print "Counter value is $counter"."<br />";
    $counter++;
    }
    ?>
  • 4. The do...while Statement
    A do...while statement looks a little like a while statement turned on its head. The essential difference between the two is that the code block is executed before the truth test and not after it.
    The test expression of a do...while statement should always end with a semicolon.
    do-while loop first do's and secondly checks the while condition!
    do {
    // code to be executed
    } while (expression);
  • 5. Sample 2
    <?php
    $num = 1;
    do {
    print "Execution number: $num<br /> ";
    $num++;
    } while ( $num > 200 && $num < 400 );
    ?>
  • 6. The for Statement
    is simply a while loop with a bit more code added to it.
     The common tasks that are covered by a for loop are:
    Set a counter variable to some initial value.
    Check to see if the conditional statement is true.
    Execute the code within the loop.
    Increment a counter at the end of each iteration through the loop.
    for ( initialize a counter; conditional statement; increment a counter){
    do this code;
    }
  • 7. Sample 3
    <?php
    for ( $counter=1; $counter<=12; $counter++ ) {
    print "$counter times 2 is".($counter*2)."<br />";
    }
    ?>
  • 8. Array
    An array is a data structure that stores one or more values in a single value (bucket). 
    The array() construct is useful when you want to assign multiple values to an array at one time
    are indexed from zero by default, so the index of any element in a sequentially indexed array always is one less than the element's place in the list
    $users = array ("Bert", "Sharon", "Betty", "Harry");
  • 9. Sample 4
    <?php
    //arrary
    $users = array ("Bert", "Sharon", "Betty", "Harry");
    print $users[2];
    ?>
  • 10. Associative Arrays
    In an associative array a key is associated with a value.
    $salaries["Bob"] = 2000;
    $salaries["Sally"] = 4000;
    $salaries["Charlie"] = 600;
    $salaries["Clare"] = 0;
  • 11. Sample 5
    <?php
    //associative arrary
    $salaries["Bob"] = 2000;
    $salaries["Sally"] = 4000;
    $salaries["Charlie"] = 600;
    $salaries["Clare"] = 0;
    echo "Bob is being paid - $" . $salaries["Bob"] . "<br />";
    echo "Sally is being paid - $" . $salaries["Sally"] . "<br />";
    echo "Charlie is being paid - $" . $salaries["Charlie"] . "<br />";
    echo "Clare is being paid - $" . $salaries["Clare"];
    ?>
  • 12. Php Functions
    is a self-contained block of code that can be called by your scripts. When called, the function's code is executed. You can pass values to functions, which they then work with. When finished, a function can pass a value back to the calling code.
    function myCompanyMotto(){
    //some codes
    } ?>
  • 13. Sample 6
    <?php
    function myname(){
    $name = "your-name";
    echo "$name <br />";
    }
    echo "My Name is <br />";
    myname();
    ?>
  • 14. A Function That Returns a Value
    A function can return a value using the return statement in conjunction with a value. return stops the execution of the function and sends the value back to the calling code.
    function addNums( $firstnum, $secondnum ) {$result = $firstnum + $secondnum;
    return $result;
    }
  • 15. Sample 7
    <?php
    function addNums( $firstnum, $secondnum ) {
    $result = $firstnum + $secondnum;
    return $result;
    }
    print addNums(3,5);
    // will print "8"
    ?>