Accessing an array from inside a function

The following code doesn’t work. The function should return a random value from within the $testArray array, but it doesn’t.

Can anyone identify the issue?

<?php
    $testimonials = array(
    'Alex is an extremely personable, professional and detail-oriented designer and developer. He meets deadlines, maintains quality, and focuses on results. He is the type of gentleman any creative and/or development team can benefit from having around.&quot;</p>
              <p class="testimonialFooter"><span>-Peter Grosschedl</span><br />Account Executive<br /><a href="http://www.cisp.com/">CommunityISP</a></p>',

    'Alex is a higly motivated and passionate worker. He takes pride in his work and maintains incredible work ethic. Intelligent and goal oriented. I am glad to have had the privilege of working with him.&quot;</p>
              <p class="testimonialFooter"><span>-Verlicia Garland</span><br />Billing Coordinator<br /><a href="http://www.cisp.com/">CommunityISP</a></p>',

    'Alex is dedicated to his work and has a talent for designing sites with effective and aesthetic layouts. Also has no trouble understanding customer&#39;s needs and communicating with customer&#39;s to finalize designs.&quot;</p>
              <p class="testimonialFooter"><span>-Daniel Lewis</span><br />Teir 2 Technician<br /><a href="http://www.cisp.com/">CommunityISP</a></p>',

    'Alex is one of the most driven individuals I have ever worked with. He does whatever it takes to produce the best web page before it goes live. I have learned a great deal about writing and design in a short time. What impresses me the most is that Alex is always eager to learn more and never afraid to get another opinion.&quot;</p>
              <p class="testimonialFooter"><span>-Dave Hershberger</span><br />Copywriter<br /><a href="http://www.cisp.com/">CommunityISP</a></p>',

    'I have only worked with Alex a short time but it&#39;s clear that he is a hard-working self-starter that understands exactly what a project will require from the onset, and how to get it done quickly and effectively. He is a creative and solution-oriented person who frequently comes up with new and innovative approaches to his assignments and projects. He would definitely be a valuable addition to any company.&quot;</p>
              <p class="testimonialFooter"><span>-Toni J. Lynch</span><br />Teir 2 Technician<br /><a href="http://www.cisp.com/">CommunityISP</a></p>', 

    'Alex has been great to work with. He is knowledgeable, detail-oriented, and personable. Will definitely work him in the future.&quot;</p>
              <p class="testimonialFooter"><span>-Emma Walker</span><br />Web Designer<br /><a href="http://www.3howards.com/">3Howards Media</a></p>',
    );

  // Define the testimonials function
  function testimonial($testimonials) {

    // Define a random key for the $testimonials array
    $randomKey = rand(0, (count($testimonials)-1));

    // Display a random testimonial
    echo $testimonials[$randomKey];

  }; // testimonials()

  testimonial($testimonials);
?>

Try this:



<?php	
 $testArray = array( 
    'test1', 
    'test2', 
    'test3', 
  ); 

//==================================
function testFunction($testArray)
{ 
  $randomKey = rand(0, (count($testArray)-1)); 
  
  return $testArray[$randomKey]; 
}//endfunc

for($i2=0; $i2<10; $i2++)
{
	echo '<br />', testFunction($testArray);
}
die;
   
?>  


.

I noticed you changed the code in the original post :slight_smile:

Try this:



<?php
$testimonials = array
( 
'<p>
	Alex is an extremely personable, professional and detail-
	oriented designer and developer. He meets deadlines, 
	maintains quality, and focuses on results. He is the type of 
	gentleman any creative and/or development team can benefit 
	from having around.&quot;
</p>
<p class="testimonialFooter">
	<span>-Peter Grosschedl</span>
	<br />
	Account Executive
	<br />
	<a href="http://www.cisp.com/">CommunityISP</a>
</p>', 

'<p>
	Alex is a higly motivated and passionate worker. He takes 
	pride in his work and maintains incredible work ethic. 
	Intelligent and goal oriented. I am glad to have had the 
	privilege of working with him.&quot;
</p>
<p class="testimonialFooter">
	<span>-Verlicia Garland</span>
	<br />
	Billing Coordinator
	<br />
	<a href="http://www.cisp.com/">CommunityISP</a>
</p>', 

'<p>
	Alex is dedicated to his work and has a talent for 
	designing sites with effective and aesthetic layouts. Also 
	has no trouble understanding customer&rsquo;s needs and 
	communicating with customer&rsquo;s to finalize 
	designs.&quot;
</p>
<p class="testimonialFooter">
	<span>-Daniel Lewis</span>
	<br />Teir 2 Technician
	<br />
	<a href="http://www.cisp.com/">CommunityISP</a>
</p>', 


'<p>
	Alex is one of the most driven individuals I have ever 
	worked with. He does whatever it takes to produce the best 
	web page before it goes live. I have learned a great deal 
	about writing and design in a short time. What impresses me 
	the most is that Alex is always eager to learn more and 
	never afraid to get another opinion.&quot;
</p>
<p class="testimonialFooter">
	<span>-Dave Hershberger</span><br />
	Copywriter
	<br />
	<a href="http://www.cisp.com/">CommunityISP</a>
</p>', 

'<p>
	I have only worked with Alex a short time but it&rsquo;s 
	clear that he is a hard- working self-starter that 
	understands exactly what a project will require from the 
	onset, and how to get it done quickly and effectively. He is 
	a creative and solution-oriented person who frequently comes 
	up with new and innovative approaches to his assignments and 
	projects. He would definitely be a valuable addition to any 
	company.&quot;
</p>
<p class="testimonialFooter">
	<span>-Toni J. Lynch</span>
	<br />
	Teir 2 Technician
	<br />
	<a href="http://www.cisp.com/">CommunityISP</a>
</p>',

'<p>
	Alex has been great to work with. He is knowledgeable, 
	detail-oriented, and personable. Will definitely work him in 
	the future.&quot;
</p>
<p 
	class="testimonialFooter">
	<span>-Emma Walker</span>
	<br />
	Web Designer<br />
	<a href="http://www.3howards.com/">3Howards Media</a>
</p>' 

); 

// Define the testimonials function 
function testimonial($testimonials) { 

  // Define a random key for the $testimonials array 
  $randomKey = rand(0, (count($testimonials)-1)); 

  // Display a random testimonial 
  echo $testimonials[$randomKey]; 

}; // testimonials() 

for($i2=0; $i2<10; $i2++)
{
	echo '<br />', testimonial($testimonials); 
}
  
die;
?>


I had to replace your it’s with it’s

.

Why did you add that?

I don’t understand. I didn’t have any apostrophes in my code. This still isn’t working for me.

For apostrophes I use the following, but SitePoint converts it to an actual apostrophe.

&#39

I just copied your code from original post and corrected to escape (\') some single quotes and run it just works as expected:


$testimonials = array( 
    'Alex is an extremely personable, professional and detail-oriented designer and developer. He meets deadlines, maintains quality, and focuses on results. He is the type of gentleman any creative and/or development team can benefit from having around.&quot;</p> 
              <p class="testimonialFooter"><span>-Peter Grosschedl</span><br />Account Executive<br /><a href="http://www.cisp.com/">CommunityISP</a></p>', 

    'Alex is a higly motivated and passionate worker. He takes pride in his work and maintains incredible work ethic. Intelligent and goal oriented. I am glad to have had the privilege of working with him.&quot;</p> 
              <p class="testimonialFooter"><span>-Verlicia Garland</span><br />Billing Coordinator<br /><a href="http://www.cisp.com/">CommunityISP</a></p>', 

    'Alex is dedicated to his work and has a talent for designing sites with effective and aesthetic layouts. Also has no trouble understanding customer\\'s needs and communicating with customer\\'s to finalize designs.&quot;</p> 
              <p class="testimonialFooter"><span>-Daniel Lewis</span><br />Teir 2 Technician<br /><a href="http://www.cisp.com/">CommunityISP</a></p>', 

    'Alex is one of the most driven individuals I have ever worked with. He does whatever it takes to produce the best web page before it goes live. I have learned a great deal about writing and design in a short time. What impresses me the most is that Alex is always eager to learn more and never afraid to get another opinion.&quot;</p> 
              <p class="testimonialFooter"><span>-Dave Hershberger</span><br />Copywriter<br /><a href="http://www.cisp.com/">CommunityISP</a></p>', 

    'I have only worked with Alex a short time but it\\'s clear that he is a hard-working self-starter that understands exactly what a project will require from the onset, and how to get it done quickly and effectively. He is a creative and solution-oriented person who frequently comes up with new and innovative approaches to his assignments and projects. He would definitely be a valuable addition to any company.&quot;</p> 
              <p class="testimonialFooter"><span>-Toni J. Lynch</span><br />Teir 2 Technician<br /><a href="http://www.cisp.com/">CommunityISP</a></p>',

    'Alex has been great to work with. He is knowledgeable, detail-oriented, and personable. Will definitely work him in the future.&quot;</p> 
              <p class="testimonialFooter"><span>-Emma Walker</span><br />Web Designer<br /><a href="http://www.3howards.com/">3Howards Media</a></p>', 
    ); 

  // Define the testimonials function 
  function testimonial($testimonials) { 

    // Define a random key for the $testimonials array 
    $randomKey = rand(0, (count($testimonials)-1)); 

    // Display a random testimonial 
    echo $testimonials[$randomKey]; 

  }; // testimonials() 

  testimonial($testimonials);

Originally Posted by John_Betong
PHP Code:
for($i2=0; $i2<10; $i2++)
{
echo ‘<br />’, testimonial($testimonials);
}

die;
?>
Why did you add that?

I copied and pasted your code. eliminated the it’s errors and also the paragragraphs statements which were not formatted correctly. When the errors were eliminated I ran the function through a loop to ensure that the results were random, followed by a die(); to prevent my further code from being called.

.