Web Programming TutorialsImplementing variables and arrays in php

Implementing variables and arrays in php

In this session today we will learn to implement variable and arrays in php.

  • To study the implementation of arrays and variables in php follow the steps given below :

  1. Install the xampp server , if it is installed already then go to the Xampp folder c:Xampp
  2. v1

  3. Inside this Xampp folder you will find an htdocs folder
  4. In this htdocs folder create a new folder say s3.
  5. v2

  6. In s3 folder create an index.php file and open it with notepad++.
  7. v4

  8. So now let’s learn to declare a variable , write the following code in index.php file
  9. <?php
     
    $var='Welcome';
    echo $var;
    
    ?>
    

  10. You will have the following look of your notepad++ window :
  11. v5

  12. To run the program , open up the browser and enter the following url in the address bar : http://localhost/s3/index.php

    Note: Here s3 is the folder name in the htdoc ,
    index.php is the file name inside the s3 folder.
  13. Hence you will have the following output :
  14. v6

  15. Let’s have an example for concatenation ,so create a new index1.php file and write the following code in it:
  16. <?php
     
    $var='Welcome';
    $var1='Eduonix';
    echo $var.' '.$var1;
    echo "<br>";
    echo 'I want to add '.$var.' '.$var1;
    
    
    ?>
    

  17. Now to have the output enter the url in the address bar : http://localhost/s3/index1.php
  18. Hence you will have the output which will have concatenation implemented in it
  19. v7

  20. Now lets take an example which will include the implementation of variable and array in php.
  21. So in s3 folder create a new file say complete.php and write the following code in it :
  22. <!DOCTYPE html>
    <html>
    <head>
    	<title>PHP Variables and Arrays</title>
    </head>
    <body>
    <h1>This is the string concatenation</h1>
    <?php
    	$word1 = 'Hello';
    	$word2 = 'World';
    	echo $word1.' '.$word2;
    ?>
    <br>
    <h1>Addition of two number in the php </h1>
    
    <?php
    	$num1 = 2;
    	$num2 = 3;
    	echo $num1 + $num2;
    ?>
    <br>
    <h1>Accessing the array element</h1>
    <?php
    $cars = array('Honda','Chevy','Ford');
    echo $cars[1];
    ?>
    <br>
    <h1>This is an index array in the php</h1>
    
    <?php
    $car = array('make' => 'Honda','model' => 'Accord','color' => 'Black');
    echo $car['model'];
    ?>
    
    </body>
    </html>
    

  23. You will have the following view of your notepad++ window :
  24. 1
    2

  25. Now to run the php program , open up the browser and enter the following url :
    http://localhost/s3/complete.php
  26. Hence you will have the output as shown below :
  27. 3

  28. Lets code to get the array element using foreach loop.
  29. So create a new file say foreach.php in s3 folder and write the given code in it :
  30. <html>
    <head>
    </head>
    <body>
    <?php
    
    $fruit=array('Mango','Apple','Orange');
        foreach($fruit as $myfruit)
    	{
    	      echo $myfruit;
    		  echo '<br />';
    	}
    ?>
    </body>
    </html>
    

  31. Now run the program by entering the address in the browser as http://localhost/s3/foreach.php
  32. So you will have the following output :
  33. v11

  34. Let’s have an example of associative array , so create a new file in s3 folder say associative.php and write the following code in it:-
  35. <html>
    <head>
    </head>
    <body>
    <h1>Associative array</h1>
    <?php
    
    $fruit=array('grapes'=>'green','apple'=>'red','banana'=>'yellow');
        foreach($fruit as $key=>$value )
    	{
    	     echo ucwords($key). ':' .$value.'<br />';
    	}
    ?>
    </body>
    </html>
    

  36. Now run the program by entering the address in the browser as http://localhost/s3/associative.php
  37. Hence you will have the following output :
  38. v12

  39. Thus we had a brief discussion on implementation of variables and arrays in php.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -