To get better at coding, you often need to get out of your comfort zone. Sometimes that doesn’t have to be such a hard thing to do.
It is often enough to just get a new piece of information regarding some simple concept in a programming language.
In this article, we’ll look at over 100 gotchas in PHP.
1. You can embed HTML in PHP conditionals
You can mix HTML and PHP in many different ways. Here’s an example of adding HTML directly to PHP conditionals:
As can be seen in the code above, sometimes we don’t have to use the echo language construct at all!
2. Falsy values in PHP
Here is a list of falsy values in PHP:
The keywords FALSE and NULL
Zero (0, 0.0, ‘0’, “0”)
An empty string (‘’,””)
An array with zero elements (contrary to JS!)
A SimpleXML object created from empty tags
All the above values are falsy in PHP. Everything else is true.
The following values are always true:
3. Four ways to create an array in PHP
There are several ways to create and update arrays in PHP:
4. Adding months to the alt attribute of the img HTML element with PHP
5. Echoing a PHP array to a web page as an unordered list
Unlike JS, arrays in PHP are not converted to strings automatically. To do that, you need to use the implode function.
Even better way to do this is using a loop:
What’s the difference between using implode or a loop? With implode you can only specify a separator, but in a loop, you can add any additional code you like. For example:
6. You can’t use an associative array variable in a double-quoted string
Let’s say we have an associative array like this:
Let’s also say we want to echo out a member of this associative array, with the following code:
Doing this would throw an error. So how do we fix it? We need to re-write our code like this:
7. Use in_array to find array elements easily
Alternatively, you could use the following code:
8. Iterating over sub-arrays
Let’s say we have the following images:
Let’s also say that we need to show different images based on the month of the year. So if months are Dec, Jan, or Feb, we’ll show the feature_winter.jpg image.
Here’s how we’d do it:
The features array contains headlines for each season. What we need to do is find the current season so we can display the current tagline and the correct image. That’s what the $seasons array is for. Each array key has the name of the season, by the value is another array. And this is what’s known as a multidimensional array. It’s an array of arrays. Just think of it as 4 arrays, but instead of each of them stored in a variable, it’s identified by a string. So we have an array for winter, another for spring, and so on. Each array consists of the first three letters of a month in that season. To get the first three letters of the current month, and to convert the entire result to lowercase, we use this syntax:
So now we can use $monthname to loop over these arrays:
Now we can simply echo out the tagline in our HTML:
9. Gathering Form Input: POST
Here’s a form in HTML:
10. How is the query string created in the browser?