An array is a systemic arrangement of objects. Hum, what does this mean? Well in programming an array is a type of data structure. Each array can hold several pieces of information. It’s sort of like a variable in that it stores data, but not at all like a variable in that instead of storing one bit of information it can store many pieces of information.
Let’s start with an example. Let’s say that you are storing information about people.
You could have a variable that stored my name “Angela”. But in an array, you could store my name, my age, my height, my
In this sample code, we will look at storing two bits of information at a time, the first being somebody’s name and the second being their favorite color.
In this example code, you can see that the friend array is sorted by number, and contains a list of friends. In the second array, color, instead of using numbers it uses strings to identify the different bits of information.
The identifier used to retrieve data from the array is called it’s key.
In our first example, the keys were integers 0, 1, 2, and 3. In our second example, the keys were strings. In both cases, we are able to access the data held in the array by using both the array’s name, and the key.
Like variables, arrays always start with a dollar sign ($array) and they are case sensitive.
They can not start with an underscore or a number, you must start them with a letter.
So, to put it simply, an array is kind of like a variable with lots of little variables inside of it. But what exactly do you do with an array? And how is it useful to you as a PHP programmer?
In practice, you will probably never create an array like the one in the example above. The most useful thing you can do with an array in PHP is to use it to hold information you get form somewhere else.
Having your website's information stored in an MySQL database is not uncommon. When your website needs certain information it simply accesses your database, and wha-laa, on demand data.
Let’s say you have a database of people who live in your city. You now want to search that database and print out records for anyone named “Tom”. How would you go about doing this?
You would read through the database for people named Tom, and then pull their name and all the other information about them from the database, and place it in an array inside of your program. You are then able to cycle through this array, and print out the information or store it to use elsewhere in your program.
A good example of how to write data from a MySQL database to an array to be used in your program can be found here.
On the surface, an array might not look that interesting to you, but when you do more programming and start storing more complex data structures you will find you are often writing them to arrays when they need to be used.
Learn PHP
Take this step-by-step approach to learn PHP coding
PHP is a programming language used to enhance websites built with HTML. It is server-side code that can add a log-in screen, CAPTCHA code or survey to your website, redirect visitors to other pages or construct a calendar.
The Essentials for Learning PHP
Learning a new language—programming or otherwise—can be a bit overwhelming. Many people don't know where to start and give up before they begin. Learning PHP is not as overwhelming as it might seem.
Just take it one step at a time, and before you know it, you'll be off and running.
Basic Knowledge
Before you start learning PHP you need a basic understanding of HTML. If you already have it, great. If not there are plenty of HTML articles and tutorials to help you. When you know both languages, you can switch between PHP and HTML right in the same document. You can even run PHP from an HTML file.
Tools
When creating PHP pages, you can use the same software you use to create your HTML pages. Any plain text editor will do. You also need an FTP client to transfer files from your computer to your web host. If you already have an HTML website, you most likely already use an FTP program.
The Basics
The basic skills you need to master first include:
- How to begin and end PHP code using <?php and ?> respectively.
- How to leave comments that don't execute in the code; they just inform programmers who work on your code in the future (or remind you of your thinking).
- How to use the echo and print statements.
- How to set a variable.
- How to use an array.
- How to use operators and operands.
- How to use conditional statements and nested statements.
Start with this PHP Basics tutorial to learn about all these basic skills.
Learning Loops
After you master the basic skills, it is time to learn about loops.
A loop evaluates a statement as true or false. When it is true, it executes code and then alters the original statement and starts over again by re-evaluating it. It continues to loop through the code like this until the statement becomes false. There are several different types of loops including while and for loops. They are explained in this Learning Loops tutorial.
PHP Functions
A function performs a specific task. Programmers write functions when they plan to do the same task repeatedly. You only have to write the function once, which saves time and space. PHP comes with a set of predefined functions, but you can learn to write your own custom functions. From here, the sky is the limit. With a solid knowledge of the PHP basics, adding PHP functions to your arsenal when you need them is easy.
Now What?
Where can you go from here? Check out 10 Cool Things to Do With PHP for ideas you can use to enhance your website.
No comments:
Post a Comment