Skip to content

Instantly share code, notes, and snippets.

@loraxx753
Created October 17, 2012 20:49
Show Gist options
  • Save loraxx753/3908087 to your computer and use it in GitHub Desktop.
Save loraxx753/3908087 to your computer and use it in GitHub Desktop.
PHP Practice 1
<?php
//Starting values (one way to do an array)
$peopleWhoHateChris = array('Jacob');
//Adding to the array
$peopleWhoHateChris[] = 'John';
$peopleWhoHateChris[] = 'Mike';
//Another way to add
array_push($peopleWhoHateChris, 'Luke');
//Step 1: print out the number of people who hate Chris by saying "X people think Chris is a fucking asshole."
//Step 2: Print out the 3rd person who hates Chris (should be Mike)
//Step 3: Remove the last person from the array, then print out each person in a list (with a loop)
//You're gonna have to look stuff up.
//Try googling "remove last item from array php"
@OldMnWither
Copy link

"; print $peopleWhoHateChris[2]; unset($peopleWhoHateChris[2]); foreach($peopleWhoHateChris as $name){ print $name; } ?>

Ok this should be all you wanted for real this time. Only problem is - how do I get a space between the unset and the foreach to print out the names. It all jams into a line and I cant figure out how to insert a nbsp or /n correctly.

@loraxx753
Copy link
Author

Almost right, you're unsetting "Mike" instead of the last item "Luke"

As for how to break it up, remember you can echo html right into this, so you could do print $name."
"; to add a line break between each or $name." "; for a space.

@loraxx753
Copy link
Author

Damnit... lemme try that again

As for how to break it up, remember you can echo html right into this, so you could do print $name."<br/>"; to add a line break between each or $name." "; for a space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment