Skip to main content
Menu
CodeBabes
  • Create new account
  • Request new password

or

  • Sign in with Twitter
  • Code
  • Babes
  • Sign Up
  • Log In

Looks like you missed a quiz. Go back and pass that shit!


NO STRIP FOR YOU!

Share the Love

  • Tweet

PHP Arrays

To really go all the way with PHP you have to understand arrays. An array is basically a container that groups values together.

Let’s say we want to keep track of my measurements, my bust, waist, and hips. In this not-so-hypothetical example, we could create some variables, $bust = 32, $waist = 24, and $hips = $32. That’s not too bad, but what if we started adding more things, like my height, weight, eye color, hair color, and shoe size (creeps). That’s a lot of variables to keep track of. That’s where arrays come in.

Bust open your helloworld.php. Let’s make an array of my babe. My bust, waist, and hips.

$babe = array(32, 24, 32);

Setting $babe creates the array. Now we can access parts of the array, basically pulling out whatever we need, like this:

print(‘bust: ’ . $babe[0]);
print(waist: ’ . $babe[1]);
print(‘hips: ’ . $babe[2]);

If we wanted to add my height, in inches, we could do:

$babe[] = 70;

The empty brackets mean that 70 will be added to the end of the array.

We can access it the same way:

print(‘height: ’ . $babe[3]);

The array we defined previously is actually a shortcut for:

array(
    0 => 32,
    1 => 24,
    2 => 32,
)

If we leave out the keys, then PHP will automatically create numeric keys, starting at 0.

This is working well, but it’s going to get difficult to remember what value is in what position.
Another way we could have done it is to use strings as keys instead.

$babe = array(‘bust’ => 32, ‘waist’ => 24, ‘hips’ => 32);

This makes things easier to read, and you don’t have to remember if bust is the first item in the array, or if it was hips. The same code above would look like:

print(‘bust: ’ . $babe[‘bust’]);
print(waist: ’ . $babe[‘waist’]);
print(‘hips: ’ . $babe[‘hips’]);

We could add my height to the array like so:

$babe[‘height’] = 70;

Then access it just like the others:

print(‘height: ’ . $babe[‘height’]);

Arrays can use strings or numbers as keys, and anything as the value, even other arrays.

We could add my eye color:

$babe[‘eye color’] = ‘black’;

Or we could add the types of modeling I do:

$babe[‘modeling’] = array(‘fashion’, ‘lingerie’, ‘swimwear’);

Then we could access the first type of modeling I do like this:

print(‘models: ’ . $babe[‘modeling’][0]);

Now you know your way around arrays and we’re halfway through getting freaky with PHP.

Questions or Comments?

Lessons

  • PHP Variables
  • PHP Conditionals
  • PHP Arrays
  • PHP Loops
  • PHP Functions
PHP Virgin

Downloads

SignUp & Subscribe

What the What?

Lessons: 41

Much more to come!

50/50 Entertainment and Education. Learning doesn't have to be boring :)

Our Philosophy

Contact

  • support
  • Tweets!
  • Youtube
  • Google Plus
  • Facebook

Stuff

  • Blog
  • Sign Up
  • All Babes
  • All Courses
  • Become a CodeBabe!
  • MAKE A LESSON WITH CODEBABES!

Recent Courses

Internet Virgin
SEO Virgin
HTML Virgin
CSS Virgin
Programming Virgin with Javascript

Learn to Code with Babes

Copyright © 2025 CodeBabes.com • Made with

  • Privacy Policy
  • Terms of Use
Close Menu
  • Blog

  • All Courses
  • Android Virgin
    • Android Studio Tour
    • App Layout in Android Studio
    • Java Variables, Java Data Types & Android Views
    • Java Event Handling in Android
    • Android Intents, OOP This, Variable Scope
    • Passing the Data with Android Intents & Finish
    • Complete the Android App with Launcher Icons & APK
    • Android Happy Ending
  • Internet Intro
    • Backend Development
    • Front End Web Development
    • Programming Tools you Need
    • Hosting and Domain Names
  • HTML Virgin
    • HTML Text Tags
    • HTML Links and Images
    • HTML Layout with HTML 5
    • HTML Tables
  • CSS Virgin
    • Where to Stick your CSS
    • CSS Properties, Sizes and Colors
    • CSS Selectors
    • CSS Box Model
  • Programming Virgin
    • Variables in Programming
    • Programming Intro Conditional Statements
    • Programming Intro to Arrays
    • Programming with Loops
    • Programming with Functions
  • PHP Virgin
    • PHP Variables
    • PHP Conditionals
    • PHP Arrays
    • PHP Loops
    • PHP Functions
  • SEO Virgin
    • Make Google Love You
    • Understanding Keyword SEO
    • Optimizing for SEO
    • Technical SEO