SlideShare a Scribd company logo
TRIVUz Academy

                             PF03
                 Class Id:




Programming Fundamentals
                              MS Alam TRIVUz
                                 Founder, TRIVUz Network
TRIVUz Academy
TRIVUz Academy




Recap PF01 & PF02



             TRIVUz Academy
Recap
                           We Define Computer & Programming

                                        Programming Process

                                     Programming Language

                                             Inputs & Outputs

                                    Define Logical Processing

                                                   Variables

                                                  Data Types

                                 Define Conditional Statement
Programming Fundamentals
                                            If… Then… Else…
 TRIVUz Academy                                PHP Operators

                                        www.trivuzacademy.com
We are going to learn
                                                Variable+

                                                    Array

                                          Array Functions

                                             Global Array

                                                     Loop

                                   for, do…while, for each

                                                Functions


Programming Fundamentals


 TRIVUz Academy
www.trivuzacademy.com
Variable+
                   Array




TRIVUz Academy
Array
What is an Array?
A variable is a storage area holding a number or
text. The problem is, a variable will hold only one
value.




                                      TRIVUz Academy
Array
What is an Array?
A variable is a storage area holding a number or
text. The problem is, a variable will hold only one
value.
An array is a special variable, which can store
multiple values in one single variable.




                                      TRIVUz Academy
Array
What is an Array?
• An array in PHP is a structure which maps keys
  (array element names) to values
• The keys can specified explicitly or they can be
  omitted
• If keys are omited, integers starting with 0 are
  keys
• The value mapped to a key can, itself, be an
  array, so we can have nested arrays


                                      TRIVUz Academy
Array
What is an Array?
Variable VS Array




TRIVUz Academy
Array
What is an Array?
Variable VS Array?
In Variable:
<?php

 $student = “Farah”;

 $student = “Tawhid”;

 $student = “Jewel”;

?>

                        TRIVUz Academy
Array
What is an Array?
Variable VS Array?
In Array:
<?php

 $student = array(“Farah”,”Tawhid”,”Jewel”);

?>




                                               TRIVUz Academy
Array
What is an Array?
Variable VS Array?
Array will create Variable index like
$student[0] = “Farah”;

$student[1] = “Tawhid”;

$student[2] = “Jewel”;




                                        TRIVUz Academy
Array
Very simple array and output

Code
<?php

  $student = array(“Farah”,”Tawhid”,”Jewel”);

  print_r($student);

?>



Output
Array ( [0] => Trivuz [1] => Tawhid [2] => Jewel )



                                                     TRIVUz Academy
Array
Echo value from array

Code
<?php

 echo $student[0];
 echo $student[1];
?>




Output
Farah
Tawhid


                        TRIVUz Academy
Array
Change Value in Array

Code
<?php
 $student[0] = ”Trivuz”;
 $student[2] = "Asif Islam”;
 print_r($student);
?>




Output
Array ( [0] => Trivuz[1] => Tawhid [2] => Asif Islam )



                                                         TRIVUz Academy
Array
Specifying an Array
• A special function is used to specify arrays
  array()

• Format of Usage
  array([key=>]value, …)

• A key is either a string or a non-negative integer

• A value can be anything

• Format of associative array specification
  $ages = array(“Huzaifa”=>22, “Tuhin”=>23)

                                              TRIVUz Academy
Array
Specifying an Array
• Here is another associative (hash) array:
  $ages[„Riaydh‟]=“24”;
  $ages[„Piash‟]=“21”;

• Implicit indices are integers, starting at 0
  $student = array(“Koushik”,”Tafsir”,”Eunus”);

• Here is the same array written differently
  $student[0] = “Koushik”;
  $student[0] = “Tafsir”;
  $student[0] = “Eunus”;

                                                  TRIVUz Academy
Array
Specifying an Array
• If and explicit integer index is followed by
  implicit indices, they follow on from the highest
  previous index
   • Here is an array indexed by integers 1,2,3
       $student = array(1=>“Avishek”,”Mehedi”,”Masud”);

   • Here is an array indexed by integers 1,5,6
       $student = array(5=>“Avishek”,1=>”Mehedi”,”Masud”);

• Show output
  <?php
   print_r($student);
  ?>

                                                          TRIVUz Academy
Array
Specifying an Array
• A two-dimensional hash array
  $result = array(“Farah“=>array(“Bangla”=>85,”English”=>78));

  echo $result[„Farah‟][„Bangla];
  echo $result[„Farah‟][„English‟];


• A two-dimensional ordinary array
  $heights = array(10,20,30,40,50),array(100,200));

  echo $heights[0][1]; // output : 20
  echo $heights[1][1]; // output : 200


• Change a two-dimensional array value
  $heights[0][0] = 130;
  $heights[0][1] = 140;
  print_r($heights); // output : Array ( [0] => Array ( [0] => 130 [1] => 140 ) )

                                                                    TRIVUz Academy
Array Functions
PHP provides a huge set of array-manipulation functions
      array – Create an array
      array_change_key_case – Returns an array with all string keys
       lowercased or uppercased
      array_chunk – Split an array into chunks
      array_count_values – Counts all the values of an array
      array_diff – Computes the difference of arrays
      array_filter – Filters elements of an array using a callback funciton
      array_flip – Flip all the values of an array
      array_fill – Fill and array with values
      array_intersect – Computes the intersection of arrays
      array_key_exists – Checks if the given key or index exists in the
       array
      array_keys – Return all the keys of an array


                                                        TRIVUz Academy
Array Functions
PHP provides a huge set of array-manipulation functions
      array_map – Applies the callback to the elements of the given
       arrays
      array_merge – Merge two or more arrays
      array_merge_recursive – Merge two or more arrays recursively
      array_multisort – Sort multiple or multi-dimensional arrays
      array_pad – Pad array to the specified length with a value
      array_pop – Pop the element off the end of array
      array_push – Push one or more elements onto the end of array
      array_rand – Pick one or more random entries out of an array
      array_reverse – Return an array with elements in reverse order
      array_reduce – Iteratively reduce the array to a single value using
       a callback function
      array_shift – Shift an element off the beginning of array
      array_slice – Extract a slice of the array
                                                        TRIVUz Academy
Array Functions
PHP provides a huge set of array-manipulation functions
      array_splice – Remove a portion of the array and replace it with
       something else
      array_sum – Calculate the sum of values in an array
      array_unique – Remove duplicate values from an array
      array_unshift – Prepend one or more elements to the beginning of
       array
      array_values – Return all the values of an array
      array_walk – Apply a user function to every member of an array
      arsort – Sort an array in reverse order and maintain index
       association
      asort – Sort an array and maintain index association
      compact – Create array containing variables and their values
      count – Count element in a variable
      current – Return the current element in an array
                                                          TRIVUz Academy
Array Functions
PHP provides a huge set of array-manipulation functions
      each – Return the current key and value pair from an array and
       advance the array cursor
      end – Set the internal pointer of an array to its last element
      extract – Import variables into the current symbol table from an
       array
      in_array – Return TRUE if a value exists in an array
      Array_search – Searches the array for a given value and returns
       the corresponding key if successful
      key – Fetch a key from an associative array
      krsort – Sort an array by key
      list – Assign variables as if they were an array
      natsort– Sort an array using a “natural order” algorithm
      pos – Get the current element from an array
      prev – Rewind the internal array pointer
                                                          TRIVUz Academy
Array Functions
PHP provides a huge set of array-manipulation functions
      range – Create an array containing a range of elements
      reset – Set the internal pointer of an array to its first element
      rsort – Sort an array in reverse order
      shuffle – Shuffle an array
      sizeof – Get the number of elements in variable
      sort – Sort an array
      uasort – Sort an array with a user-defined comparison function
      uksort – Soft an array by keys using a user-defined comparison
       function
      usoft– Soft an array by values using a user-defined comparison
       function




                                                          TRIVUz Academy
Global Array
Global Arrays
      PHP Creates 6 global arrays that contain EGPCS (ENVIRONMENT,
       GET, POST, COOKIES and SERVER) information
      PHP also creates a variable called $_REQUEST that contains all the
       information in the 6 global arrays
      PHP also creates a variable called $PHP_SELF that contains the
       name of the current script (relative to the doc root)
      $_ENV – Contains the values of any environment variables, such as
       the browser version
           Eg: $_ENV[HTTP_USER_AGENT]



      $_FILES – Contains information about any files submitted
      $_COOKIES – Contain any cookies submitted as name value pairs
      $_SERVER – Contains useful information about the webserver

                                                       TRIVUz Academy
Global Array
$_SERVER Keys
      [DOCUMENT_ROOT]        [SERVER_SOFTWARE]
      [HTTP_*]               [COMSPEC]
      [PHP_SELF]             [GATEWAY_INTERFACE]
      [QUERY_STRING]         [PATHEXT]
      [REMOTE_ADDR]          [PATH]
      [REQUEST_METHOD]       [REMOTE_PORT]
      [REQUEST_URI]          [SERVER_ADDR]
      [SCRIPT_FILENAME]      [SERVER_ADMIN]
      [SCRIPT_NAME]          [SERVER_SIGNATURE]
      [SERVER_PORT]          [SystemRoot]
      [SERVER_PROTOCOL]      [WINDIR]

                                          TRIVUz Academy
Control Structure
                      Looping




TRIVUz Academy
Control Structure
                                 Looping

 Loops execute a block of code a specified number of
 times, or while a specified condition is true.

 PHP loops are control structures and you can use
 them the execute a code block more times. It means
 you don't have to copy and paste your code many
 times in the file just use a right loop statement.


                                     TRIVUz Academy
Control Structure
                               Looping
A basic example:
<?php
        echo " 1 ”;
        echo " 2 ”;
        echo " 3 ”;
        echo " 4 ”;
        echo " 5 ”;
?>




                                 TRIVUz Academy
Control Structure
                                      Looping
A basic example:
<?php
        echo " 1 ”;
        echo " 2 ”;
        echo " 3 ”;
        echo " 4 ”;
        echo " 5 ”;
?>

With a for loop it looks like this:
<?php
        for ($i=1; $i <= 5; $i++) {
        echo “ $i “;
}
?>
                                       TRIVUz Academy
Control Structure
                               Looping

 In PHP, we have the following looping statements:
     While
     Do…while
     For
     foreach




                                  TRIVUz Academy
Control Structure
                while loop
Syntax

while




                    TRIVUz Academy
Control Structure
                           while loop
Syntax

while (condition)




                               TRIVUz Academy
Control Structure
                                  while loop
Syntax

while (condition)
{
        // code to be executed;
}




                                      TRIVUz Academy
Control Structure
                                         while loop
Example

<?php

$i = 1;
while ($i < 5)
 {
         echo “ I = “ . $i . “<br />”;
         echo $i++;
 }

?>



                                             TRIVUz Academy
Control Structure
                                                    while loop
Execution
Code:
<?php
$i = 1;
while ($i < 5)
 {
           echo “ Value of I is now “ . $i . “<br />”;
           echo $i++;
 }
?>




                                                         TRIVUz Academy
Control Structure
                                                    while loop
Execution (Loop - 1)
Code:
<?php
$i = 1;
while ($i < 5) // $i is now 1
 {
           echo “ Value of I is now “ . $i . “<br />”;
           echo $i++; // $i is now 2
 }
?>

Output:
  Value of I is now 1




                                                         TRIVUz Academy
Control Structure
                                                    while loop
Execution (Loop - 2)
Code:
<?php
$i = 1;
while ($i < 5) // $i is now 2
 {
           echo “ Value of I is now “ . $i . “<br />”;
           echo $i++; // $i is now 3
 }
?>

Output:
  Value of I is now 1
  Value of I is now 2



                                                         TRIVUz Academy
Control Structure
                                                  While Loop
Execution (Loop - 3)
Code:
<?php
$i = 1;
while ($i < 5) // $i is now 3
 {
           echo “ Value of I is now “ . $i . “<br />”;
           echo $i++; // $i is now 4
 }
?>

Output:
  Value of I is now 1
  Value of I is now 2
  Value of I is now 3


                                                         TRIVUz Academy
Control Structure
                                                    while loop
Execution (Loop - 4)
Code:
<?php
$i = 1;
while ($i < 5) // $i is now 4
 {
           echo “ Value of I is now “ . $i . “<br />”;
           echo $i++; // $i is now 5
 }
?>

Output:
  Value of I is now 1
  Value of I is now 2
  Value of I is now 3
  Value of I is now 4

                                                         TRIVUz Academy
Control Structure
                                                    while loop
Execution (Loop - 4)
Code:
<?php
$i = 1;
while ($i < 5) // $i is now 5
 {
           echo “ Value of I is now “ . $i . “<br />”;
           echo $i++; // not executed
 }
 // next code to execute
?>
Output:
  Value of I is now 1
  Value of I is now 2
  Value of I is now 3
  Value of I is now 4

                                                         TRIVUz Academy
Control Structure
                          do…while loop
Syntax

do {
         // code to be executed;
 }
while (condition);




                                   TRIVUz Academy
Control Structure
                           do…while loop
Example

do {
         $i++;
         echo “The number is “ . $i . “<br />”;
 }
while ($i < 5);




                                                  TRIVUz Academy
Control Structure
                                   for loop
Syntax

for (init; condition; increment)

{
         // code to be executed;

}




                                    TRIVUz Academy
Control Structure
                                               for loop
Example

for ($i = 1; $i < 5; $++)

{
         echo “Current value of I is “ . $i . “<br />”;

}




                                                   TRIVUz Academy
Control Structure
                                        for each

FOREACH is used in PHP to loop over all elements of an array. The basic
syntax of FOREACH is as follows:
FOREACH ($array_variable as $value)
{
  //code to execute
}

or

FOREACH ($array_variable as $key => $value)
{
  //code to execute
}

                                              TRIVUz Academy
Control Structure
                                          for each
Simple Syntax

foreach (array_expression as $value)

  // statement

foreach (array_expression as $key => $value)

  // statement




                                               TRIVUz Academy
Control Structure
                                                for each
Example

<?php

    $arr = array(1, 2, 3, 4);

    foreach ($arr as &$value) {

     $value = $value * 2;

}

    // $arr is now array(2, 4, 6, 8)

    unset($value); // break the reference with the last element

?>

                                                      TRIVUz Academy
Control Structure
                                         for each
Example

<?php
        $arr = array("one", "two", "three");
        reset($arr);
        while (list(, $value) = each($arr)) {
                  echo "Value: $value<br />n”;
        }
        foreach ($arr as $value) {
                  echo "Value: $value<br />n”;
        }
?>



                                                  TRIVUz Academy
Function

           What is function?

           How function help programmers?

           Types of function




TRIVUz Academy
Function

           System Defined Function

           User Defined Function




TRIVUz Academy
Function
                                      User-Defined Function

In PHP, functions are defined in the following fashion:



function function_name ([variable [= constant][,…])
  {

         // any valid PHP code

 }




                                                      TRIVUz Academy
Function
                                         User-Defined Function

Example: User-Defined Function to determine a Leap Year

<?php

function is_leapyear ($year = 2011)
  {

        $is_leap = (!($year % 4) && (($year % 100) || !($year % 400)));

        return $is_leap;

 }

?>



                                                      TRIVUz Academy
Function
                                 User-Defined Function

Function with user defined argument

function function_name ($arg)
  {

        // any valid PHP code

 }




                                          TRIVUz Academy
Function
                                          User-Defined Function

Example: User-Defined Function to determine a Leap Year

<?php
function is_leapyear ($year)
  {

         $is_leap = (!($year % 4) && (($year % 100) || !($year % 400)));

         return $is_leap;

 }

?>




                                                       TRIVUz Academy
Function
                                          User-Defined Function

Example: User-Defined Function to determine a Leap Year

<?php
function is_leapyear ($year)
  {

         $is_leap = (!($year % 4) && (($year % 100) || !($year % 400)));

         return $is_leap;

 }

?>
Calling the user defined function is_leapyear

 Is_leapyer(2008);
 Is_leapyer(2011);
                                                       TRIVUz Academy
Thank You



            MS Alam TRIVUz
            Founder,
            TRIVUz Academy
            trivuz@gmail.com

More Related Content

What's hot (20)

Introducing Akka
Introducing AkkaIntroducing Akka
Introducing Akka
Meetu Maltiar
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Ranel Padon
 
Cse java
Cse javaCse java
Cse java
MohammedAbdulNaseer5
 
javaarray
javaarrayjavaarray
javaarray
Arjun Shanka
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
OXUS 20
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
Rafael Magana
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
camel-scala.pdf
camel-scala.pdfcamel-scala.pdf
camel-scala.pdf
Hiroshi Ono
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
Princess Sam
 
Scala
ScalaScala
Scala
Sven Efftinge
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmer
Girish Kumar A L
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
Arzath Areeff
 
Java q ref 2018
Java q ref 2018Java q ref 2018
Java q ref 2018
Christopher Akinlade
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
guest5c8bd1
 
Collections and generic class
Collections and generic classCollections and generic class
Collections and generic class
ifis
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
Jussi Pohjolainen
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
Gousalya Ramachandran
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
Arslan Arshad
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Ranel Padon
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
OXUS 20
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
Rafael Magana
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
Princess Sam
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmer
Girish Kumar A L
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Collections and generic class
Collections and generic classCollections and generic class
Collections and generic class
ifis
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
Arslan Arshad
 

Viewers also liked (10)

THINK LIKE A CHILD
THINK LIKE A CHILDTHINK LIKE A CHILD
THINK LIKE A CHILD
Varun Garg
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
M.Zalmai Rahmani
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
mohamedsaad24
 
ds and algorithm session
ds and algorithm sessionds and algorithm session
ds and algorithm session
Varun Garg
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
SHC
 
Ruby
RubyRuby
Ruby
Aizat Faiz
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
Trivuz ত্রিভুজ
 
Ruby on Rails for beginners
Ruby on Rails for beginnersRuby on Rails for beginners
Ruby on Rails for beginners
Vysakh Sreenivasan
 
Programming languages
Programming languagesProgramming languages
Programming languages
Akash Varaiya
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
Varun Garg
 
THINK LIKE A CHILD
THINK LIKE A CHILDTHINK LIKE A CHILD
THINK LIKE A CHILD
Varun Garg
 
ds and algorithm session
ds and algorithm sessionds and algorithm session
ds and algorithm session
Varun Garg
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
SHC
 
Programming languages
Programming languagesProgramming languages
Programming languages
Akash Varaiya
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
Varun Garg
 
Ad

Similar to Programming Basics - array, loops, funcitons (20)

Php my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.netPhp my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
PHP Underground Session 1: The Basics
PHP Underground Session 1: The BasicsPHP Underground Session 1: The Basics
PHP Underground Session 1: The Basics
Robin Hawkes
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
Amirul Azhar
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
ayushmishraaa09
 
PHP array 1
PHP array 1PHP array 1
PHP array 1
Mudasir Syed
 
PHP Arrays_Introduction
PHP Arrays_IntroductionPHP Arrays_Introduction
PHP Arrays_Introduction
To Sum It Up
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
Norhisyam Dasuki
 
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptxPHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
kamalsmail1
 
Intoduction to php arrays
Intoduction to php arraysIntoduction to php arrays
Intoduction to php arrays
baabtra.com - No. 1 supplier of quality freshers
 
PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptx
ShaliniPrabakaran
 
PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
Yuriy Krapivko
 
PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
Yuriy Krapivko
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
40NehaPagariya
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
Vineet Kumar Saini
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical Hacking
BCET
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
mythili213835
 
Php
PhpPhp
Php
shakubar sathik
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
richambra
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.netPhp my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
PHP Underground Session 1: The Basics
PHP Underground Session 1: The BasicsPHP Underground Session 1: The Basics
PHP Underground Session 1: The Basics
Robin Hawkes
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
Amirul Azhar
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
ayushmishraaa09
 
PHP Arrays_Introduction
PHP Arrays_IntroductionPHP Arrays_Introduction
PHP Arrays_Introduction
To Sum It Up
 
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptxPHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
kamalsmail1
 
PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptx
ShaliniPrabakaran
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical Hacking
BCET
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
richambra
 
Ad

Recently uploaded (20)

How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 

Programming Basics - array, loops, funcitons

  • 1. TRIVUz Academy PF03 Class Id: Programming Fundamentals MS Alam TRIVUz Founder, TRIVUz Network TRIVUz Academy
  • 2. TRIVUz Academy Recap PF01 & PF02 TRIVUz Academy
  • 3. Recap We Define Computer & Programming Programming Process Programming Language Inputs & Outputs Define Logical Processing Variables Data Types Define Conditional Statement Programming Fundamentals If… Then… Else… TRIVUz Academy PHP Operators www.trivuzacademy.com
  • 4. We are going to learn Variable+ Array Array Functions Global Array Loop for, do…while, for each Functions Programming Fundamentals TRIVUz Academy www.trivuzacademy.com
  • 5. Variable+ Array TRIVUz Academy
  • 6. Array What is an Array? A variable is a storage area holding a number or text. The problem is, a variable will hold only one value. TRIVUz Academy
  • 7. Array What is an Array? A variable is a storage area holding a number or text. The problem is, a variable will hold only one value. An array is a special variable, which can store multiple values in one single variable. TRIVUz Academy
  • 8. Array What is an Array? • An array in PHP is a structure which maps keys (array element names) to values • The keys can specified explicitly or they can be omitted • If keys are omited, integers starting with 0 are keys • The value mapped to a key can, itself, be an array, so we can have nested arrays TRIVUz Academy
  • 9. Array What is an Array? Variable VS Array TRIVUz Academy
  • 10. Array What is an Array? Variable VS Array? In Variable: <?php $student = “Farah”; $student = “Tawhid”; $student = “Jewel”; ?> TRIVUz Academy
  • 11. Array What is an Array? Variable VS Array? In Array: <?php $student = array(“Farah”,”Tawhid”,”Jewel”); ?> TRIVUz Academy
  • 12. Array What is an Array? Variable VS Array? Array will create Variable index like $student[0] = “Farah”; $student[1] = “Tawhid”; $student[2] = “Jewel”; TRIVUz Academy
  • 13. Array Very simple array and output Code <?php $student = array(“Farah”,”Tawhid”,”Jewel”); print_r($student); ?> Output Array ( [0] => Trivuz [1] => Tawhid [2] => Jewel ) TRIVUz Academy
  • 14. Array Echo value from array Code <?php echo $student[0]; echo $student[1]; ?> Output Farah Tawhid TRIVUz Academy
  • 15. Array Change Value in Array Code <?php $student[0] = ”Trivuz”; $student[2] = "Asif Islam”; print_r($student); ?> Output Array ( [0] => Trivuz[1] => Tawhid [2] => Asif Islam ) TRIVUz Academy
  • 16. Array Specifying an Array • A special function is used to specify arrays array() • Format of Usage array([key=>]value, …) • A key is either a string or a non-negative integer • A value can be anything • Format of associative array specification $ages = array(“Huzaifa”=>22, “Tuhin”=>23) TRIVUz Academy
  • 17. Array Specifying an Array • Here is another associative (hash) array: $ages[„Riaydh‟]=“24”; $ages[„Piash‟]=“21”; • Implicit indices are integers, starting at 0 $student = array(“Koushik”,”Tafsir”,”Eunus”); • Here is the same array written differently $student[0] = “Koushik”; $student[0] = “Tafsir”; $student[0] = “Eunus”; TRIVUz Academy
  • 18. Array Specifying an Array • If and explicit integer index is followed by implicit indices, they follow on from the highest previous index • Here is an array indexed by integers 1,2,3 $student = array(1=>“Avishek”,”Mehedi”,”Masud”); • Here is an array indexed by integers 1,5,6 $student = array(5=>“Avishek”,1=>”Mehedi”,”Masud”); • Show output <?php print_r($student); ?> TRIVUz Academy
  • 19. Array Specifying an Array • A two-dimensional hash array $result = array(“Farah“=>array(“Bangla”=>85,”English”=>78)); echo $result[„Farah‟][„Bangla]; echo $result[„Farah‟][„English‟]; • A two-dimensional ordinary array $heights = array(10,20,30,40,50),array(100,200)); echo $heights[0][1]; // output : 20 echo $heights[1][1]; // output : 200 • Change a two-dimensional array value $heights[0][0] = 130; $heights[0][1] = 140; print_r($heights); // output : Array ( [0] => Array ( [0] => 130 [1] => 140 ) ) TRIVUz Academy
  • 20. Array Functions PHP provides a huge set of array-manipulation functions  array – Create an array  array_change_key_case – Returns an array with all string keys lowercased or uppercased  array_chunk – Split an array into chunks  array_count_values – Counts all the values of an array  array_diff – Computes the difference of arrays  array_filter – Filters elements of an array using a callback funciton  array_flip – Flip all the values of an array  array_fill – Fill and array with values  array_intersect – Computes the intersection of arrays  array_key_exists – Checks if the given key or index exists in the array  array_keys – Return all the keys of an array TRIVUz Academy
  • 21. Array Functions PHP provides a huge set of array-manipulation functions  array_map – Applies the callback to the elements of the given arrays  array_merge – Merge two or more arrays  array_merge_recursive – Merge two or more arrays recursively  array_multisort – Sort multiple or multi-dimensional arrays  array_pad – Pad array to the specified length with a value  array_pop – Pop the element off the end of array  array_push – Push one or more elements onto the end of array  array_rand – Pick one or more random entries out of an array  array_reverse – Return an array with elements in reverse order  array_reduce – Iteratively reduce the array to a single value using a callback function  array_shift – Shift an element off the beginning of array  array_slice – Extract a slice of the array TRIVUz Academy
  • 22. Array Functions PHP provides a huge set of array-manipulation functions  array_splice – Remove a portion of the array and replace it with something else  array_sum – Calculate the sum of values in an array  array_unique – Remove duplicate values from an array  array_unshift – Prepend one or more elements to the beginning of array  array_values – Return all the values of an array  array_walk – Apply a user function to every member of an array  arsort – Sort an array in reverse order and maintain index association  asort – Sort an array and maintain index association  compact – Create array containing variables and their values  count – Count element in a variable  current – Return the current element in an array TRIVUz Academy
  • 23. Array Functions PHP provides a huge set of array-manipulation functions  each – Return the current key and value pair from an array and advance the array cursor  end – Set the internal pointer of an array to its last element  extract – Import variables into the current symbol table from an array  in_array – Return TRUE if a value exists in an array  Array_search – Searches the array for a given value and returns the corresponding key if successful  key – Fetch a key from an associative array  krsort – Sort an array by key  list – Assign variables as if they were an array  natsort– Sort an array using a “natural order” algorithm  pos – Get the current element from an array  prev – Rewind the internal array pointer TRIVUz Academy
  • 24. Array Functions PHP provides a huge set of array-manipulation functions  range – Create an array containing a range of elements  reset – Set the internal pointer of an array to its first element  rsort – Sort an array in reverse order  shuffle – Shuffle an array  sizeof – Get the number of elements in variable  sort – Sort an array  uasort – Sort an array with a user-defined comparison function  uksort – Soft an array by keys using a user-defined comparison function  usoft– Soft an array by values using a user-defined comparison function TRIVUz Academy
  • 25. Global Array Global Arrays  PHP Creates 6 global arrays that contain EGPCS (ENVIRONMENT, GET, POST, COOKIES and SERVER) information  PHP also creates a variable called $_REQUEST that contains all the information in the 6 global arrays  PHP also creates a variable called $PHP_SELF that contains the name of the current script (relative to the doc root)  $_ENV – Contains the values of any environment variables, such as the browser version  Eg: $_ENV[HTTP_USER_AGENT]  $_FILES – Contains information about any files submitted  $_COOKIES – Contain any cookies submitted as name value pairs  $_SERVER – Contains useful information about the webserver TRIVUz Academy
  • 26. Global Array $_SERVER Keys  [DOCUMENT_ROOT]  [SERVER_SOFTWARE]  [HTTP_*]  [COMSPEC]  [PHP_SELF]  [GATEWAY_INTERFACE]  [QUERY_STRING]  [PATHEXT]  [REMOTE_ADDR]  [PATH]  [REQUEST_METHOD]  [REMOTE_PORT]  [REQUEST_URI]  [SERVER_ADDR]  [SCRIPT_FILENAME]  [SERVER_ADMIN]  [SCRIPT_NAME]  [SERVER_SIGNATURE]  [SERVER_PORT]  [SystemRoot]  [SERVER_PROTOCOL]  [WINDIR] TRIVUz Academy
  • 27. Control Structure Looping TRIVUz Academy
  • 28. Control Structure Looping  Loops execute a block of code a specified number of times, or while a specified condition is true.  PHP loops are control structures and you can use them the execute a code block more times. It means you don't have to copy and paste your code many times in the file just use a right loop statement. TRIVUz Academy
  • 29. Control Structure Looping A basic example: <?php echo " 1 ”; echo " 2 ”; echo " 3 ”; echo " 4 ”; echo " 5 ”; ?> TRIVUz Academy
  • 30. Control Structure Looping A basic example: <?php echo " 1 ”; echo " 2 ”; echo " 3 ”; echo " 4 ”; echo " 5 ”; ?> With a for loop it looks like this: <?php for ($i=1; $i <= 5; $i++) { echo “ $i “; } ?> TRIVUz Academy
  • 31. Control Structure Looping  In PHP, we have the following looping statements:  While  Do…while  For  foreach TRIVUz Academy
  • 32. Control Structure while loop Syntax while TRIVUz Academy
  • 33. Control Structure while loop Syntax while (condition) TRIVUz Academy
  • 34. Control Structure while loop Syntax while (condition) { // code to be executed; } TRIVUz Academy
  • 35. Control Structure while loop Example <?php $i = 1; while ($i < 5) { echo “ I = “ . $i . “<br />”; echo $i++; } ?> TRIVUz Academy
  • 36. Control Structure while loop Execution Code: <?php $i = 1; while ($i < 5) { echo “ Value of I is now “ . $i . “<br />”; echo $i++; } ?> TRIVUz Academy
  • 37. Control Structure while loop Execution (Loop - 1) Code: <?php $i = 1; while ($i < 5) // $i is now 1 { echo “ Value of I is now “ . $i . “<br />”; echo $i++; // $i is now 2 } ?> Output: Value of I is now 1 TRIVUz Academy
  • 38. Control Structure while loop Execution (Loop - 2) Code: <?php $i = 1; while ($i < 5) // $i is now 2 { echo “ Value of I is now “ . $i . “<br />”; echo $i++; // $i is now 3 } ?> Output: Value of I is now 1 Value of I is now 2 TRIVUz Academy
  • 39. Control Structure While Loop Execution (Loop - 3) Code: <?php $i = 1; while ($i < 5) // $i is now 3 { echo “ Value of I is now “ . $i . “<br />”; echo $i++; // $i is now 4 } ?> Output: Value of I is now 1 Value of I is now 2 Value of I is now 3 TRIVUz Academy
  • 40. Control Structure while loop Execution (Loop - 4) Code: <?php $i = 1; while ($i < 5) // $i is now 4 { echo “ Value of I is now “ . $i . “<br />”; echo $i++; // $i is now 5 } ?> Output: Value of I is now 1 Value of I is now 2 Value of I is now 3 Value of I is now 4 TRIVUz Academy
  • 41. Control Structure while loop Execution (Loop - 4) Code: <?php $i = 1; while ($i < 5) // $i is now 5 { echo “ Value of I is now “ . $i . “<br />”; echo $i++; // not executed } // next code to execute ?> Output: Value of I is now 1 Value of I is now 2 Value of I is now 3 Value of I is now 4 TRIVUz Academy
  • 42. Control Structure do…while loop Syntax do { // code to be executed; } while (condition); TRIVUz Academy
  • 43. Control Structure do…while loop Example do { $i++; echo “The number is “ . $i . “<br />”; } while ($i < 5); TRIVUz Academy
  • 44. Control Structure for loop Syntax for (init; condition; increment) { // code to be executed; } TRIVUz Academy
  • 45. Control Structure for loop Example for ($i = 1; $i < 5; $++) { echo “Current value of I is “ . $i . “<br />”; } TRIVUz Academy
  • 46. Control Structure for each FOREACH is used in PHP to loop over all elements of an array. The basic syntax of FOREACH is as follows: FOREACH ($array_variable as $value) { //code to execute } or FOREACH ($array_variable as $key => $value) { //code to execute } TRIVUz Academy
  • 47. Control Structure for each Simple Syntax foreach (array_expression as $value) // statement foreach (array_expression as $key => $value) // statement TRIVUz Academy
  • 48. Control Structure for each Example <?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } // $arr is now array(2, 4, 6, 8) unset($value); // break the reference with the last element ?> TRIVUz Academy
  • 49. Control Structure for each Example <?php $arr = array("one", "two", "three"); reset($arr); while (list(, $value) = each($arr)) { echo "Value: $value<br />n”; } foreach ($arr as $value) { echo "Value: $value<br />n”; } ?> TRIVUz Academy
  • 50. Function What is function? How function help programmers? Types of function TRIVUz Academy
  • 51. Function System Defined Function User Defined Function TRIVUz Academy
  • 52. Function User-Defined Function In PHP, functions are defined in the following fashion: function function_name ([variable [= constant][,…]) { // any valid PHP code } TRIVUz Academy
  • 53. Function User-Defined Function Example: User-Defined Function to determine a Leap Year <?php function is_leapyear ($year = 2011) { $is_leap = (!($year % 4) && (($year % 100) || !($year % 400))); return $is_leap; } ?> TRIVUz Academy
  • 54. Function User-Defined Function Function with user defined argument function function_name ($arg) { // any valid PHP code } TRIVUz Academy
  • 55. Function User-Defined Function Example: User-Defined Function to determine a Leap Year <?php function is_leapyear ($year) { $is_leap = (!($year % 4) && (($year % 100) || !($year % 400))); return $is_leap; } ?> TRIVUz Academy
  • 56. Function User-Defined Function Example: User-Defined Function to determine a Leap Year <?php function is_leapyear ($year) { $is_leap = (!($year % 4) && (($year % 100) || !($year % 400))); return $is_leap; } ?> Calling the user defined function is_leapyear Is_leapyer(2008); Is_leapyer(2011); TRIVUz Academy
  • 57. Thank You MS Alam TRIVUz Founder, TRIVUz Academy [email protected]

Editor's Notes

  • #3: ----- Meeting Notes (12/28/11 19:20) -----