This Might take a While If you’ve never been introduced to the concept of a loop, think about it this way. Let’s say that you’re looking at an excel sheet. In that excel sheet you have rows that go down the sheet. Each row has some data inside of it. To read the contents of the next row, you move down. You can keep reading the contents of each row up until you reach the last row of that excel sheet. With a loop, you can read the information from a particular row, add 1 to the current row number,
Category: Programming
Why Not “Switch” from If/Else? Are you tired of writing if/elseif/elseif/…/else statements? You’re not alone. There is a more elegant way to write such large statements: the PHP switch statement. Let’s take a look at the anatomy of the switch statement. switch ( $arg ) { case “value_1”: expression if matched value_1 break; // terminate case “value_2”: expression if matched value_2 break; // terminate … default: execute if no cases matched } The switch statement accepts an argument. It has a series of “cases” that could potentially match the value of the argument. If it matches, the statements inside of that particular case are executed.
tired of if/else? How about ternary? The PHP Ternary Operator is a way to quickly express if/else statements. The ternary operator follows the following syntax: ( boolean expression ) ? if_true : if_false; If the boolean expression evaluates to true, the “if_true” result is displayed, otherwise the “if_false” result is displayed. Just as a quick recap, the normal if/else syntax looks like the following: <?php $expression = true; if ( $expression ) { // do statement if true } else { // do statement if false } ?> This gets redundant especially when you’re just echoing
Elseif: Between if and Else When you have more than 2 outcomes in your decision making, you’re going to need to use the PHP elseif statement. The elseif keyword goes in between the if and the else keywords. Your primary condition is handled by the if statement. If the condition is met inside of the if statement, there’s no point in testing any other conditions. The code that’s inside of the if statement body is executed and the remainder of the elseif/else statements are skipped. When the condition is not met, PHP will check the elseif statements next. If a condition matches, the code inside of that particular elseif statement body will execute and PHP will skip the remainder
What else is there? The PHP Else statements execute when the if statement’s condition evaluates to false. Remember, the expressions inside of the if statement can only execute when the conditional expression evaluates to true. The else keyword is appended right after the closing curly brace of the if statement. <?php $exercised = false; if ( $exercised ) { echo “You’re being healthy.”; } else { echo “Crickets”; ?> When the programmer chooses to omit the curly braces from the if statement because of a single line expression, the else keyword is appended immediately below the expression that’s
If You Only Knew The PHP if statement is probably the first piece of code that you wrote. “If” statements are conditional statements, meaning that if the expression that they’re evaluating is true, the if statement will allow for the execution of the expressions inside of the if statement body. The anatomy of the if statement looks like this: <?php if ( boolean expression ) { expressions in body } ?> The if statement starts with the word if, followed by an expression enclosed by parentheses. The expression will evaluate to either true or false. If the expression evaluates to true, the expressions inside the
Which Operators Come First? PHP Operator precedence just looks at which operation will be performed first. You’ve seen operator precedence in mathematics. The multiplication and division operators are executed before the addition and subtraction operators. With all of the different operators that we have in PHP, the interpreter needs to know which operation to perform in what order. Let’s look at a basic example in math to get started: 2 + 3 * 5 If you were not aware of operator precedence in mathematics, you may start working from left to right. The answer that you would get would be
My Hands Aren’t Short! PHP shorthand operators are amazing! There, I said it. The shorthand operator combines the expression on the right with the assignment operator. The variable that appears on the left-side of the assignment operator needs to appear on the right-hand-side as well in order for you to be able to use the shorthand notation. Let’s take a look at the following code: <?php $x = 1; $x = $x + 1; echo $x; ?> PHP assigns the integer value 1 to the variable $x. In the second statement, $x + 1 is evaluated. $x contains
How Many Operators do You Want Already? PHP Array operators are operators that are used on arrays. Who would have guessed? The first operator that we’ll look at is the + operator. You would think that this operator would be used to merge two arrays, taking elements from both arrays and merging them into a new array, but that is not the case. If you were to output the result of the code above, you would quickly see that the result is: The + operator creates a new array that combines key/value pairs. Elements with numeric
A String can operate? The PHP string operator, also known as the concatenation operator, allows us to join two strings together. Strings need to be joined for various reasons, such as injecting a variable, or even splitting a long string so that it’s more readable in your IDE. Let’s take a look at the following sentence (string): “I am sick and tired of creating operator tutorials.” We can echo it out or assign it to a variable. We can also split the string up using the concatenation operator and have as many strings as there are characters in it.
