While not used very often in PHP, continue and break deserve much more credit. Continue and break provide extra control over your loops by manipulating the flow of the iterations. Continue is the nicer one of the two as it prevents the current iteration, or flow through one pass of the loop, and just tells PHP to go to the next round of the loop. Break provides more aggressive control by completing skipping out of the loop and onto the code following the loop. If you have ever played the card game UNO, continue is basically the skip card, and break is someone slamming their cards down screaming I am the winner and leaves the room. Let’s go through some examples to see how to use them.
Once the var $i reaches 2 in the for loop, it meets the conditional statement $i == 2, which is then executed. The statement executed is break;. When break is executed it actually breaks us out of the for loop, which is why when $i equals 2 we exit the for loop and execute echo “End of for loop”; instead of ending the for loop when $i equals 5. Remember break escapes the entire logic of the other iterations.
It seems pretty obvious that continue is different than break. Notice how the only result not printed is 2. The continue basically says let’s skip out of this particular instance of the loop and move onto the next one. When we are running through the instance where $i becomes equal to 2, we meet the conditional statement if( $i == 2) where it is true. Then, we execute the continue, which stops us from executing the following statements in the loop. We then run through the next iteration of the loop where $i equals 3. Since we don’t meet the conditional statement this time, we write the following statements and will never enter into conditional if($i == 2).
Taking a quiz or a test an excellent way to test your own knowledge of a subject. We believe that our exams are completely fair and require significant knowledge to achieve a perfect score.
PHP Quiz
PHP is one of the most dominant scripting languages on the web because it is essentially free. PHP also has one of the highest rates of new coders compared to other languages because young coders prefer to start in a language that won’t cost them any money. PHP performs well in terms of speed and flexibility, which helps the new coders stick with the language. The PHP quiz is designed to determine if you have understand the fundamentals of PHP. Once you have mastered the basics of the quiz, you should explore using PHP on your own for a little while. When you have finished tinkering with PHP, you might want to consider moving onto database programming languages like SQL, and taking the SQL quiz.
We currently only provide a standard PHP quiz that isn’t exactly a professional testing mechanism. This means that if you are trying to test someone’s PHP knowledge this exam is not for you. It does provide an excellent overview of PHP, but it is intended for self-development. However in the future, we might integrate a testing system into After Hours Programming, but we are not currently working towards that goal. For now, we have created a simple practice test to check your understanding of PHP.
Somebody bring in the math squad. Relax, how complicated can standard arithmetic operators be? In PHP there are 6 groups of operators: arithmetic, assignment, increment/decrement, comparison, logical, and array operators. We will just deal with the first 3 for now. Quick note: We are going to assume from now on that the code is inside the <?php ?> tags.
Arithmetic Operators
Nothing special here, they are pretty much standard across most languages.
Assignment Operators
You have already used one of these! See, you’re a genius and you didn’t even know it. The PHP assignment operators always have an = somewhere in the statement. The rest of the assignment operators are more like shortcuts. You can use them by throwing the assignment operators in front of the =. Let’s take a look.
You’re smart, and you can figure out how to use the rest of them. As you can see with the variable $a, we basically add it to itself, which gives us 6. With $b, we multiple it by itself, which gives us 49. It’s literally just as simple as that. If you don’t want to use them, you don’t have too. Just have fun writing out the full $a = $a + $a; or $b = $b * $b;.
Increment and Decrement Operators
Four possible outcomes occur from increments and decrements. Notice the placement of the increment or decrement operator around the variable.
If you have ever messed with these guys before, you know what is going on here. If not, just understand that the position of the increment or decrement operator tells us when the action on the variable happens. In the first example, echo ++$a; increments the variable before we echo it out. However, in the second example, echo $b++;, we don’t increment the variable until after we echo it to the screen.
PHP Date and Times are just as frustrating as any other languages. The main issue of Date and Times is that they are entirely different strings that both have strong importance on position and number of characters. For example, a PHP date is usually in the format of “2013-02-01” and times always seem to vary. This is because, we as the great thinkers of the world, created a ridiculous time system with random values instead of some metric like construction. Of course, there is entirely a purpose for time’s current format, but we can both agree that it is not programmer friendly.
What we both know is that time and date is a rather useless distinction. In certain definitions of time, it also implies the date. So, from now on I want you to think of time also containing dates. It is much more useful to place these together and you probably will not give me a great reason to separate the two after this tutorial. First, let’s get into explaining timestamps and some PHP date and time functions.
PHP Timestamps
The PHP timestamp is a Unix timestamp that tracks the number of seconds since January 1, 1970 because we apparently believe milliseconds are useless. Nevertheless, let’s create a timestamp.
So, we just use PHP’s time function with default parameters to create a timestamp of this very moment (well, the moment I created this tutorial). Why would we want something like that when users cannot understand its format? First, it is a very consistent and easy to manipulate number. Second, I will show you how to format the timestamp into PHP dates and times in just a second. Also, note if you just want the current time, you should use time, but you should use mktime for custom datetimes.
PHP Dates
Using our newly created timestamp, we can format it into a date that the user can understand.
Boom! Now, we have a formatted date string that users can understand. Now I will go on a small tangent and emphasize my extreme approval of this particular format. Sure, “2013/02/01” is equally valid, but the hyphens seem to be more universal and easier to work with. I know I do not particularly provide a strong case for the date format, but if you have no preference, take mine!
PHP Times
Do you remember my tangent of times also include dates? Well, the guys that created PHP must have agreed with me. The PHP date function also can format timestamps into “Times”. Let’s see how to create a readable time.
Well done. Now, we have the current military time that I created this tutorial. I know you might be thinking, “How do I create a timestamp that is not the present?”. Relax, we will get to it in just a moment.
PHP Date and Time
Now, let’s mix it up a little bit and create our own time and not the current time. We want to format this into a full date and time string.
Now, that is a lot to digest. We have created a full date and time string using special formatting with the PHP date function. mktime actually has parameters this time, which is how we create a custom date and time that is not the present. In order, the mktime parameters are hour, minute, second, month, day, and year. See how much easier it is to work with timestamps instead of creating dates and times separately? The PHP date function has a lot of different formatting values. I have included the references below. Feel free to tinker around with this in the first parameter of the date function to see how the characters actually format the string.
Onto the most flexible variable in PHP, the string variable. You might notice how I keep referring to strings as variables. However, strings are much more like objects with methods and properties. Only the string object’s properties is what you might think of as the string. However, I won’t discuss objects much in the early tutorials because they are a bit more complicated. You can manipulate a string in almost every way imaginable in PHP. Previously you already learned how to create a string variable by putting the actual string in quotes following the variable name.
Nothing special here. We are just echoing a string variable just like before. What if we want to join strings with other strings or strings with variables?
Joining Strings
The concatenation operator in PHP is a little bit odd in comparison. The . joins everything together. Weird choice isn’t it? I thought so.
Yes, I was rocking out to that song while writing this. You should too! We have the string “We are the “ that we join to $myVar and again to ” of the world”. That is truly all we have for the simple side of strings. Check out the PHP Strings tutorial to get into more advanced string functions.