Results 1 to 10 of 42

Thread: Day 5 - Array - Become PHP Expert in 30 days

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    http://php.flashwebhost.com/mini/array.php


    Code:
    <?php
    
    
    $animals = array('lion', 'dog', 'cat', 'horse', 'rabit', 'tiger', 'rabit', 'camel', 'deer', 'fox', 'squirrel');
    
    echo $animals[0];
    echo '<br>';
    echo $animals[2];
    echo '<br>';
    echo $animals[3];
    echo '<br>';
    echo $animals[5];
    echo '<br>';
    echo $animals[10];

  2. #2
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default Array add example

    http://php.flashwebhost.com/sibichan...y_add_eg_1.php

    PHP Code:

    <?php $hostingPackage = array (' Small '' Medium ' ' Large ') ;
    echo 
    '<pre>';

    print_r($hostingPackage);

    echo 
    '</pre>';

    $hostingPackage [] = 'Premium';$hostingPackage [] = 'Starandard';

    echo 
    '<pre>';

    print_r($hostingPackage);echo '</pre>';
    Another example

    http://php.flashwebhost.com/sibichan...y_add_eg_2.php


    PHP Code:
    <?php 
    $companyNames 
    = array('BizHat''FlashWebHost''HostOnNet');
    echo 
    '<pre>';

    print_r($companyNames);

    echo 
    '</pre>';
    echo 
    'Iam going to add 2 more Company Names';
    echo 
    '<br>';
    $companyNames[] = 'Buyscripts';
    $companyNames[]= 'FreeMarriage';
    echo 
    '<pre>';

    print_r($companyNames);

    echo 
    '</pre>';
    Last edited by Vahaa11; 05-14-2014 at 08:59 AM.

  3. #3
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/statesname.php

    Code:
    <?php
    
    $states = array('Kerala' , 'Tamil Nadu' , 'Karnataka' , 'Rajastan' , 'Orissa' );
    
    $numberOfStates = count($states);
    echo '<pre>';
    print_r($states);
    
    echo '<p> I am going to add 2 more states to $states array. </p>';
    
    $states[] = 'Kerala';
    $states[] = 'Tamil Nadu';
    
    echo '<pre>';
    print_r ($states);
    echo '</pre>';
    Last edited by sherlyk; 05-14-2014 at 09:33 AM.

  4. #4
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default

    Quote Originally Posted by sherlyk View Post
    http://php.flashwebhost.com/sherly/states1.php


    Code:
    <?php
    
    
    $states = array('Kerala' , 'Tamil Nadu' , 'Karnataka' , 'Rajastan' , 'Orissa' );
    
    
    $numberOfStates = count($states);
    echo '<pre>';
    print_r($states);
    
    
    echo '<p> I travelled  to add 2 more states to $states array. </p>';
    
    
    $states[] = 'Kerala';
    $states[] = 'Tamil Nadu';
    
    
    echo '<pre>';
    print_r ($states);
    echo '</pre>';

    Modified your code like below. Try to create meaningfull sentences.


    Your are not using the count(); function. Then no need to add in your script. Right ?


    PHP Code:

    <?php

    $states 
    = array('Kerala' 'Tamil Nadu' 'Karnataka' 'Rajastan' 'Orissa' );

    $numberOfStates count($states);

    echo 
    '<pre>';

    print_r($states);

    echo 
    '<pre>';

    echo 
    $numberOfStates ' States are listed here.I am going to add 2 more states ' ;

    $states[] = 'Gujarat';

    $states[] = 'Maharashtra';

    echo 
    '<pre>';

    print_r ($states);

    echo 
    '</pre>';

    echo 
    'So the total no of states  ' $numberOfStates;
    Last edited by Vahaa11; 05-14-2014 at 11:16 AM.

  5. #5
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/vegetables.php

    Code:
    <?php
    
    $vegetables = array('tomato', 'potato', 'cucumber', 'carrot', 'spinach', 'cabbage', 'chilli');
    
    
    echo '<pre>';
    print_r($vegetables );
    echo '</pre>';
    
    echo '<p>I am going to add 2 more vegetables  to $vegetables  array.</p>';
    
    $vegetables [] = 'beetroot';
    $vegetables [] = 'beans';
    
    echo '<pre>';
    print_r($vegetables );
    echo '</pre>';

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •