1stwebdesigner |
PHP for Beginners: Part 2 – Making Decisions and Loops Posted: 16 Nov 2010 01:00 PM PST Over the past part I have shown you the basics of text in PHP and how to store it as variables. In this part of the tutorial I will show you firstly how to use if statements to make decisions in your scripts. Next, I am going to show you how to use another important part of PHP, loops. The Basics Of ifIf statements are used to compare two values and carry out different actions based on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed. if StructureThe structure of an if statement is as follows: if (something == something else) { THEN statements } else { ELSE statements } VariablesThe most common use of an if statement is to compare a variable to another piece of text, a number, or another variable. For example: if ($username == "webmaster") which would compare the contents of the variable to the text string. The THEN section of code will only be executed if the variable is exactly the same as the contents of the quotation marks so if the variable contained ‘Webmaster’ or ‘WEBMASTER’ it will be false. Constructing The THEN StatementTo add to your script, you can now add a THEN statement: if ($username == "webmaster") { echo "Please enter your password below"; } This will only display this text if the username is webmaster. If not, nothing will be displayed. You can actually leave an IF statement like this, as there is no actual requirement to have an ELSE part. This is especially useful if you are using multiple IF statements. Constructing The ELSE StatementAdding The ELSE statement is as easy as the THEN statement. Just add some extra code: if ($username == "webmaster") { echo "Please enter your password below"; } else { echo "We are sorry but you are not a recognized user"; } Of course, you are not limited to just one line of code. You can add any PHP commands in between the curly brackets. You can even include other IF statements (nested statements). Other ComparisonsThere are other ways you can use your IF statement to compare values. Firstly, you can compare two different variables to see if their values match e.g. if ($enteredpass == $password) You can also use the standard comparison operators to check to see if one variable is greater than or less than another: if ($age < "13") Or: if ($date > $finished) You can also check for multiple tests in one IF statement. For instance, if you have a form and you want to check if any of the fields were left blank you could use the following code: // || - OR logical operator if ($name == "" || $email == "" || $password == "") { echo "Please fill in all the fields!"; } Now it’s time to show you some other ways of using your PHP script to do other types of checks and loops. So, let’s start. The while LoopThe while loop is one of the most useful commands in PHP. It is also quite easy to set up and use. A while loop will, as the name suggests, execute a piece of code until a certain condition is met. Repeating A Set Number Of TimesIf you have a piece of code which you want to repeat several times without retyping it, you can use a while loop. For instance if you wanted to print out the words “Hello World” 5 times you could use the following code: $times = 5; $x = 0; while ($x < $times) { echo "Hello World"; ++$x; } The first two lines are just setting the variables. The $times variable holds the number of times you want to repeat the code. The $x variable is the one which will count the number of times the code has been executed. After these is the while line. This tells the php interpreter to repeat the code while $i is less than $times (or to repeat it until $i is equal to $times). This is followed by the code to be executed which is enclosed in { }. After the echo line which prints out the text, there is another very important line: ++$x; What this does is exactly the same as writing: $x = $x + 1; It adds one to the value of $x. This code is then repeated (as $x now equals 1). It continues being repeated until $x equals 5 (the value of times) when the computer will then move on to the next part of the code. Using $xThe variable counting the number of repeats ($x in the above example) can be used for much more than just counting. For example if you wanted to create a web page with all the numbers from 1 to 1000 on it, you could either type out every single one or you could use the following code: $number = 1000; $current = 0; while ($current < $number) { ++$current; echo "$current"; } There are a few things to notice about this code. Firstly, you will notice that I have placed the ++$current; before the echo statement. This is because, if I didn’t do this it would start printing numbers from 0, which is not what we want. The ++$current; line can be placed anywhere in your WHILE loop, it does not matter. It can, of course, add, subtract, multiply, divide or do anything else to the number as well. The other reason for this is that, if the ++$current; line was after the echo line, the loop would also stop when the number showed 999 because it would check $current which would equal 1000 (set in the last loop) and would stop, even though 1000 had not yet been printed. ArraysArrays are common to many programing languages. They are special variables which can hold more than one value, each stored in its own numbered ’space’ in the array. Arrays are extremely useful, especially when using while loops. Setting Up An ArraySetting up an array is slightly different to setting up a normal variable. In this example I will set up an array with 5 names in it: $names[0] = 'John'; $names[1] = 'Paul'; $names[2] = 'Steven'; $names[3] = 'George'; $names[4] = 'David'; or just: $names[] = 'John'; $names[] = 'Paul'; $names[] = 'Steven'; $names[] = 'George'; $names[] = 'David'; or simply: $names = array('John', 'Paul', 'Steven', 'George', 'David'); As you can see, the parts of an array are all numbered, starting from 0. To add a value to an array you must specify the location in the array by putting a number in [ ]. Reading From An ArrayReading from an array is just the same as putting information in. All you have to do is to refer to the array and the number of the piece of data in the array. So if I wanted to print out the third name I could use the code: echo "The third name is $names[2]"; // Steven Which would output: The third name is Steven Using Arrays And LoopsOne of the best uses of a loop is to output the information in an array. For instance if you wanted to print out the following list of names: You could use the following code: $x = 0; while ($x < sizeof($names)) { $namenumber = $x + 1; echo "Name $namenumber is $names[$x]"; ++$x; } As you can see, you can use the variable $x from the loop to print out the names in the array. You may have noticed I am also using the variable $namenumber which is always greater than $x. This is because the array numbering starts from 0, so to number the names correctly in the final output I must add one to the actual value. ConclusionIn this part of the tutorial I have showed you how you can use if statements to compare variables and text information and to make decisions. We’ve also looked at using another important part of PHP, loops. In the next part I will show you how you can send e-mails using PHP. Stay tuned. |
Managing Balance Between Inspiration and Individuality Posted: 16 Nov 2010 02:00 AM PST Inspiration is a very broad word to begin with, since it can be a lot of things. It can be things that make you happy, sad, scared, angry, excited – anything you can feel that works up your creativity and tickles your imagination. It can also be someone who gives you motivation in life – idols, loved ones or simply people you look up to. Being inspired is always a good thing especially to those in the field of art and design. When people search for inspiration they wander around and look for that something that gives impact. Something mind-blowing and takes your breath away. They take that inspiration with them and from that they try to unfold their own constructiveness. That they may come up to something unique. But sometimes holding on too much from that inspiration that the product is another mere mimic of what was done. Individuality, is more than being creative in a way. It’s taking your art up another level and making your piece stand out among others. It is something very deep and personal that you can make your very own style which will distinct you from the rest. In this article, I’m hoping to make a point that not everything can be found on the internet. You need to step back once in a while, to see the bigger picture. There are plenty of sources for inspiration and individuality. Think out of the box and explore horizons. To Begin WithBefore anything else, compare and question yourself -“what are the similarities”. By identifying what went wrong in the picture that makes it too similar and being comfortable with it, makes you accept that something needs to be changed and move on. Next, is to step out of the circle. Turn off your TV, computer, ipad, cellphones, anything that hinders you from thinking naturally on your own effort. Studies show that human beings only use %10 of the brain. So, you need your brain to keep working and not stay in the corner to rust. This time, go back the basics with a clean sheet of paper and good pencil. It doesn’t matter if you can’t draw, everyone’s an artist in their own way. What’s important is your own interpretation. Take your precious time“Time is gold” – that’s right. And I also like to think that in time, you can make gold. Time makes everything and everyone wiser, better, and stronger. Billionaires didn’t earn their fortune in a snap, they worked hard and long. No masterpiece was ever made in a rush. Some even took a lifetime to realize perfection. But if you’re under time pressure, then it is best to spend time very wisely. It is always better, if not best to take as much time and effort to something that needs to be achieved. Getting things in the fastest or easiest ways don’t always work nor last. Avant-Garde ArtistsGet inspired by the best of the best. The artists who have lived the highest of all standards and the masters of the art. Be influenced to be original and not simply to imitate. Each and everyone of us is unique from head to toe. You can be as originative as you can be, never give up. 1. Leonardo da VinciThe ever famous Leonardo da Vinci and his controversial Masterpiece “The Monalisa” (shown below). Also famous for his other works of art “The Last Supper” and “Vitruvian Man” to name some. 2. Pablo PicassoWho wouldn’t recognize the works of Pablo Picasso? He is a true example of being individualistic. His style is distinct and signature. He’s behind the outstanding abstract pieces like “Seated Woman with Wristwatch” (shown below), “Friendship” & “Weeping Woman”. 3. Vincent Van GoghRemarkable strokes and paintings full of enthusiasm, that’s Vincent Willem Van Gogh. Masterpieces inspired by Impressionism, Van Gogh experimented and developed his unique techniques. To name some of his famous works are “Sunflowers” (shown below), “The Starry Night” & “Irises”. Innovators of Modern SubjectFor modern form of arts like digital arts, originality is the most critical quality an artist should have. Here are some of the many artists to look up to. 1. Mark VerhaagenThis colorful vector-based illustrations with cool fictional creatures are the byproduct of this artist’s imagination. His unique taste for subjects are very catchy. 2. Paul DaveyHis impressive intensity for detail makes his pieces stand out like telling a story. And gives his design such radiance that they almost look real. 3. Tiago HoiselHis ability to bring out the inner cartoon of people as well as bringing out the person in the cartoon is simply marvelous. He’s attention to detail is taken up to another degree. His craft is categorical but diverse. Inspiration is essential but, individuality takes you far. So, find that boiling core of creativity in you and expand. Never hesitate to innovate and explore new things. It is the key to masterpiece. |
You are subscribed to email updates from 1stwebdesigner - Graphic and Web Design Blog To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
Comments (0)
Post a Comment