Theme Graphic
Theme Graphic

My experience learning PHP

In this blog I will share my experience learning PHP from the ground up. I know HTML and have wanted to learn PHP for a long time, so here...
Posted on Tuesday, November 17, 2009 at 10:47 AM

Looping through associative arrays

In my last post I discussed associative arrays. Because the keys are not numeric it can be tricky to loop through them unless you use the foreach command. The foreach command will return each key value pair of an associative array like so:

foreach($myArray as $key => $value) { echo "This is the key: $key. This is the value: $value<br>"; }

Now you can really harness the power of associative arrays in php!
Comments: 0 Tags: None

Posted on Tuesday, November 17, 2009 at 10:45 AM

Associative arrays

Associative arrays in php are very useful. You can store something in an array as a key value pair so that you can lookup a piece of data quickly based on its key. For instance if i have an array

$groceries = array();

I can set a key called "fruit" and set its value to "banana" like so:

$groceries['fruit'] = 'banana';

Now when you print out the value of $groceries['fruit'] (via echo) you will get the output banana.

I have used associative arrays to store the anchor text of links. For instance, on a site you might do

$aLinkKey = 'tropical fish'; $links[$aLinkKey] = 'http://www.tropicalfishkeeping.com'

and when you echo <a href="{$links[$aLinkKey]}">$aLinkKey</a> you get

tropical fish

Associative arrays are simple to use. As you keep going you will find that they are really one of the backbones of php. Next time we will discuss how to loop through them.
Comments: 0 Tags: None

Posted on Tuesday, November 17, 2009 at 10:37 AM

The php include - don't duplicate header and other code

The first thing I taught myself to use in PHP is the PHP include statement. The PHP include statement is powerful because you can include one file, for instance a header file, in other files. Using PHP include statements, you can create a header file for your site and simply include it in other files rather than copying and pasting the code that comprises your header in each file. You can use the PHP include statement like this:

include 'myHeader.php';

or

include("myHeader.php");

either one will work. you can also use include_once if you have any defines (more on that later) in your include file, but if it is simply a HTML header you shouldn't need to worry about it.

I hope this helped!
Comments: 0 Tags: None

 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.