ad

Saturday, April 13, 2024

php array types

 

PHP ARRAY TYPES

PHP Array Type


PHP ARRAY

  1]Array is a special type of variable that we use to store        or store multiple values in a single variable without creating     multiple variables to store these values.

  2]  We
 use the array function ( ) to create an array in PHP.


PHP ARRAY TYPES :-

1] Indexed  Array 

 1]  PHP index array is an array represented by an index number. 
 2] All elements of an array are represented by index numbers starting from 0. 
 3] PHP index arrays can store numbers, strings, or single objects. 
 4] PHP indexed arrays are also called numeric arrays.

SYNTAX ::- 

   foreach($array as $value)
       {
            statement;
            statement;
            statement;
           }


PHP Array Type

Example :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>array</title>
</head>
<body>
    <?php
     $student=array("vitthal","seeta","radha","ram");
     rsort($student);
     foreach($student as $a)
     {
        echo $a."<br>";
     }
    ?>
</body>
</html>

PHP Array Type

Output

vitthal
seeta
ram
radha

2] Associative Array

 1] PHP allows you to associate a name/tag with any string in PHP using the => symbol.
 2] This way you can easily remember because each element is represented by a label instead of an additional number.

SYNTAX ::-

foreach ($array as $key => $value)
      {
            
         statement;
         statement;
      }

 Associative Array 

Example 


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>array</title>
</head>
<body>
    <?php
    $student=array(3=>"vitthal",1=>"seeta",2=>"radha");
    foreach($student as $x=>$y)
    {

    echo $y."your Roll no is ".$x."<br> ";
   }
    ?>
</body>
</html>

 Associative Array

Output ::-

vitthalyour Roll no is 3
seetayour Roll no is 1
radhayour Roll no is 2

 3] Multidimension Array

1] PHP multidimensional arrays are also called arrays. 
2] It allows you to store table data in an array.
3] In PHP, multidimensional arrays can be represented as a row*column matrix.

 Multidimension Array ::-

Example


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>array</title>
</head>
<body>
<?php
$COMPANY = array (
  array("TCS",22,18),
  array("IBM",15,13),
  array("INFOSYS",5,2),
  );
 
echo $COMPANY[0][0].": In stock: ".$COMPANY[0][1].", sold: ".$COMPANY[0][2].".<br>";
echo $COMPANY[1][0].": In stock: ".$COMPANY[1][1].", sold: ".$COMPANY[1][2].".<br>";
echo $COMPANY[2][0].": In stock: ".$COMPANY[2][1].", sold: ".$COMPANY[2][2].".<br>";

?>

</body>
</html>

      Multidimension Array::-

Output :-

TCS: In stock: 22, sold: 18.
IBM: In stock: 15, sold: 13.
INFOSYS: In stock: 5, sold: 2.



Related Post :- 

php program to print heart star pattern

php program to print alphabet pattern E

css id selector

Thursday, April 11, 2024

php Array

 PHP Array 

PHP Array


1] PHP Array is variable can store only one values

2] And array is use to store multiple value of a same data type.

3] To create an array we used [ ] square bracket.

4] By Default first element in and array store at Index No "0".

5] To access value in an array pass index No in square bracket.


 PHP ARRAY TYPES :-

1] Indexed  Array 

2] Associative Array

 3] Multidimension Array

PHP Array

Example :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> array </title>
</head>
<body>
    <?php
       $student= array("vitthal","seeta", "ram","shyam","radha","rukmini","krishna");

       var_dump($student);
    ?>
   
</body>
</html>

PHP Array

Output :-

PHP Array  output

PHP Array

Example :-

Q calculate length of array that is number array element.


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>array</title>
</head>
<body>
    <?php
      $student=["vitthal","radha","seeta","ram"];
      $length=count($student);
      echo "No of array element=".$length;
 
    ?>
</body>
</html>


PHP Array

Output :-

No of array element=4

Related Post :-


php data and time function

about us page design template

php program print diamond number pattern

Wednesday, April 10, 2024

PHP for loop

 PHP for loop

PHP for loop


Information :-

1]  The loop is the most complex loop in PHP and is used when the user knows             how often the block should be executed.

 2]  A for loop consists of an initialization statement, a test statement, and an     update statement (an increment or decrement statement).

3]  Loops
 for PHP can be used to iterate over a series of scheduled code.

4] If
 the number of iterations is known you should use it, otherwise use a loop.

5]  This means that you will use a loop when you already know how many     times you want to run a piece of code.


Syntax :-

for (initialization; condition; increment/decrement)

{

// enter code and executed 

syntax  explanation :-

  • initialization :-
  • This part is executed once before the loop starts. It often used to initialize variables.

  • condition:-
  • The condition that is evaluated before each iteration of the loop. If the condition evaluates to true, the loop continues; If it evaluates to false, the loop ends.

  • increment/decrement :- This is done after each iteration of the loop. It is usually used to increase or decrease a number.


 Flowchart     

PHP FOR loop /output


 PHP for loop

Example :-

Q 1. show all number 20 to 50.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<?php
for ($no=20;$no<50;$no++)
{
   echo $no.", ";
}
?>

</body>
</html>

 PHP for loop

Output :-


PHP  for loop output

Q.2 Show all number Between 100 to 50 descending order.

 PHP for loop

Example :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<?php
for($no=100; $no>50;$no--)
{
   echo$no.", ";
}
?>

</body>
</html>


 PHP for loop

Output :-

PHP program no descending order output


Related Post :-

javascript bitwise operators


php program tp print alphabet pattern C