If you are new to PHP language and coding world in general, over 99% chances are that you will most probably first end up on a PHP.net official manual website via internet search. And you will definitely get confused with strange “examples” and constructs at that place. Why is PHP.net manual so complicated and hard to understand? Well, it is not complicated at all if you know PHP language well, but the writing style may be confusing to newbies / beginners.
And, as it is almost always the case, you will seek your help somewhere else with an easy-to-read examples and not too many confusing stuff in the coding sentence. However, sooner you learn to READ and UNDERSTAND php.net website, the better and beneficial it will be for your learning skills and advancement.
Welcome to PHP.net Manual for newbies:
How to use and understand PHP.net manual website examples
Let’s start with a simple example: built-in function str_replace() in this brief article: at first, you might get overwhelmed with the long and stuffed string which supposedly represents how you should use the function. However, once you learn how to properly “read” the structure of, it will make much more sense to you.
this is NOT part of the code itself at all, it is literally a simple word descriptor which only purpose is to help you understand what type of variables (string, array, boolean, integer, object, resource etc.) can be placed in the variable next to it (left-to-right or LTR reading is assumed here).
SQUARE BRACKETS [ ]
Square Brackets are probably the most confusing part of the PHP.net Manual for new users. This is because of the lack of understanding what square brackets represents / mean. Simply put, in our example above anything between [ and ] is OPTIONAL CODE. This means that it is not necessary to include anything that is written inside the square brackets for str_replace function to work.
However, sometimes you will need those extra parameters to improve your own code, and then you will use them. But, you SHOULD NOT write the actual square brackets in this (and many other similar examples) at all! Otherwise, your PHP debugger will throw you an error.
In case you decide to use an optional parameter(s), don’t forget to separate them with the comma, the same way you separate the mandatory variables. Otherwise, comma is simply left out, and this is the reason it is placed inside the [ ] brackets section.
HIDDEN DEFAULT VALUES
Many times square brackets described in PHP.net manual will contain not only optional parameters (variables or values), but also DEFAULT values. This is not part of the manual itself, but rather PHP’s way to define value in case the programmer did not specify it.
For example, in our second example with a built-in function define() we can see another optional BOOLEAN parameter $case_insensitive. If you do not specifically define it (simply said, you left it out / not use it), PHP will in that case assume that you set it to false value. Just be carefull with this, it may give you some headache because you did not pay attention.
This is it so far, hope you find these beginner’s tips useful. Happy coding!