Web Design Forum: TUTORIAL: What are arrays and how to use them in php - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: What are arrays and how to use them in php Rate Topic: -----

#1 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 18 November 2009 - 05:05 PM

Ok so welcome to this tutorial im gonna explain some about what an array is and how you can use them in php to your advantage. Im sure everyone here has heard the word array since there used in almost ever programming language and at the end of this tutorial you will know why.An array in simple terms is like a variable but can hold unlimited values so basicly you could have 1000 different values in an array lol though you prob would not need that much. But anyways imma show you a variable below that holds the value Cat then imma show you an array which holds a few different values.

$test = "Cat";


Ok so as you can see the variable test holds the value of cat but say you want it to hold a value of dog to you can do this but you would have to right it out like this.

$test = "Cat";
$test .="Dog";
$test .="Rabbit";



Above we have our main variable $test now in lame terms you cant use the same variable more then once on a page
but if you were to put the Dot in front of the Equal sign then this would not be using it more then once cause it would connect the variables together and basicly make it just once varable that would hold
Cat,Dog,Rabbit.This is where an array would be handy lets create one now to hold those same values.


$test = array('Cat','Dog','Rabbit');

see thats alot easier huh well you can also give each one of those animals a name like this


$test['my_array'] = array(
'FRED' => 'Cat',
'BILLY' => 'Dog',
'CARROT' => 'Rabbit'
);

And to echo out a certain value like cat from the array do this


echo "".$test['my_array']['FRED']."";



Ok so now that we got a few basics down pat lets get into the heavy lifting so to speak lol ok so we are gonna test if a certain value is in an array if it is it will output Value is in array other wise it will output Value not in array

<?php
$input = $_POST['test'];
$test = array('cat','dog',rabbit);
if(isset($_POST['submit'])){
if(in_array($input,$test)){
echo "Value is in array";
}else{
echo "Value not in array";
}

}
echo"
<form method=\"POST\" action=\"test.php\">
<input type=\"text\" name=\"test\">
 <input type=\"submit\" name=\"submit\" value=\"Test\">
</form>
";
?>
So this is a short tutorial on arrays hope 
you enjoy and if you enjoy this post 
feel free to give me a +1 



1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users