There are many functions available relating to PHP arrays, and one of them is count, to get the total number of elements within an array.
Instructor: [0:00] There are many functions available relating to PHP arrays. It's time to replace our numberOfPosts variable with a dynamic value that uses our new Posts array. This is an easy one. We will just replace this hard-coded 10 with a call to the count() function, passing in the value of Posts. [0:22] When we save and refresh our page, we will see that the value dynamically updated to the count of our Posts array. We'll also notice that if we remove one of these array elements from the Post variable and save and update the page, this one value is correct, but it says posts instead of 1 post.
[0:44] Let's replace the contents of this numPostDisplay variable with a dynamic value. First, we will create a new variable that contains our post's texts. Let's call this postText, and we will set it equal to a ternary.
[1:02] You can think of ternaries as a shorthand if-else statement that's a lot more elegant and easier to read. This will be a condition which starts off with numPosts equal and only equal 1, followed by a question mark, and then what to return if this value is true.
[1:22] If there is one post, we want to return "post." This will follow with a colon, which means otherwise we will return "posts." Again, if numPost === 1, we will return "post," otherwise we will return "posts."
[1:40] Then we will update this numPostDisplay contents to equal numPosts, a space, and then postText. When we go ahead and save this page and refresh, it will say, "1 post." If we go ahead and add in that index for MySQL and save the page and refresh, it will say, "2 posts."
[2:09] You can use ternaries whenever your if-else condition isn't too complex, and you are usually just wanting to output data based on a single truthy or false result.
[2:20] Be sure to check out the many available built-in functions available for PHP arrays as these really start to make arrays much more powerful than any other regular variables.