PHP For Beginners: How to read and understand PHP.net Manual website

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.

PHP Logo

PHP Logo

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.

PHP.net Manual Explained for Newbies 1

PHP.net Manual Explained for Newbies
(click to enlarge)

MIXED

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.

Once we clear descriptions and optional part we get str_replace() simplest form

Once we clear descriptions and optional part we get str_replace() in familiar simplest form

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.

Square brackets are also used in defining PHP arrays where they have different purpose entirely. That application of square brackets is in programming context, not writing manual (text formatting) context. In another words, square brackets actually do have a special meaning in arrays variables.

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.

PHP.net Manual Explained for Newbies 2

PHP.net Manual Explained for Newbies 2
(click to enlarge)

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.

CONCLUSION

This is it so far, hope you find these beginner’s tips useful. Happy coding!

 

Comments


  1. comments

    3 Comments

    Add Your Comment
  2. 1. Md. Abdul Karim

    Super!!!

  3. 2. Joe

    Thanks a lot. This should be at first page in PHP manual.

  4. 3. Guest

    Great!!

Post A Comment

I have read and consent to Privacy Policy and Terms and Conditions